Skip to content

Commit

Permalink
add pair extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasForst committed Aug 4, 2020
1 parent acc3bec commit 1ddd2c3
Showing 1 changed file with 37 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/kotlin/pw/forst/tools/katlib/PairExtensions.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package pw.forst.tools.katlib

/**
* Applies [block] on left part of pair in List.
*/
inline fun <T, NT, V> Pair<List<T>, V>.mapLeft(block: (T) -> NT): Pair<List<NT>, V> =
Pair(this.first.map(block), this.second)

/**
* Applies [block] on right part of pair in List.
*/
inline fun <T, V, NV> Pair<T, List<V>>.mapRight(block: (V) -> NV): Pair<T, List<NV>> =
Pair(this.first, this.second.map(block))

/**
* Applies [leftBlock] on left part and [rightBlock] on right part.
*/
inline fun <T, V, NT, NV> Pair<List<T>, List<V>>.mapPair(leftBlock: (T) -> NT, rightBlock: (V) -> NV): Pair<List<NT>, List<NV>> =
Pair(this.first.map(leftBlock), this.second.map(rightBlock))

/**
* Applies [block] on left part of pair.
*/
inline fun <T, NT, V> Pair<T, V>.letLeft(block: (T) -> NT): Pair<NT, V> =
letPair(block, { it })

/**
* Applies [block] on right part of pair.
*/
inline fun <T, V, NV> Pair<T, V>.letRight(block: (V) -> NV): Pair<T, NV> =
letPair({ it }, block)

/**
* Applies [leftBlock] on left part and [rightBlock] on right part.
*/
inline fun <T, V, NT, NV> Pair<T, V>.letPair(leftBlock: (T) -> NT, rightBlock: (V) -> NV): Pair<NT, NV> =
Pair(leftBlock(this.first), rightBlock(this.second))

0 comments on commit 1ddd2c3

Please sign in to comment.