-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #223 from robstoll/feature/mapFirst-second
Pair.mapFirst/mapSecond
- Loading branch information
Showing
3 changed files
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package ch.tutteli.kbox | ||
|
||
/** | ||
* Maps the first element of this [Pair] with the given function [transform]. | ||
* | ||
* @return A new pair with the result of the transformation as first and the existing second as second element. | ||
* | ||
* @since 1.3.0 | ||
*/ | ||
fun <FirstT, SecondT, R> Pair<FirstT, SecondT>.mapFirst(transform: (FirstT) -> R): Pair<R, SecondT> = | ||
Pair(transform(first), second) | ||
|
||
/** | ||
* Maps the second element of this [Pair] with the given function [transform]. | ||
* | ||
* @return A new pair with the existing first element as first and the result of the transformation as second element. | ||
* | ||
* @since 1.3.0 | ||
*/ | ||
fun <FirstT, SecondT, R> Pair<FirstT, SecondT>.mapSecond(transform: (SecondT) -> R): Pair<FirstT, R> = | ||
Pair(first, transform(second)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package ch.tutteli.kbox | ||
|
||
import ch.tutteli.atrium.api.fluent.en_GB.first | ||
import ch.tutteli.atrium.api.fluent.en_GB.second | ||
import ch.tutteli.atrium.api.fluent.en_GB.toBeTheInstance | ||
import ch.tutteli.atrium.api.fluent.en_GB.toEqual | ||
import ch.tutteli.atrium.api.verbs.expect | ||
import kotlin.test.Test | ||
|
||
class MapTupleTest { | ||
@Test | ||
fun mapFirst_identity_returns_equal_pair() { | ||
expect(Pair(1, "hello").mapFirst(::identity)).toEqual(Pair(1, "hello")) | ||
} | ||
|
||
@Test | ||
fun mapFirst_returns_transformation_does_not_touch_second_element() { | ||
val obj = listOf('h', 'e', 'l', 'l', 'o') | ||
expect(Pair(1, obj).mapFirst { it + 1 }) { | ||
toEqual(Pair(2, obj)) | ||
second.toBeTheInstance(obj) | ||
} | ||
} | ||
|
||
@Test | ||
fun mapSecond_identity_returns_equal_pair() { | ||
expect(Pair(1, "hello").mapSecond(::identity)).toEqual(Pair(1, "hello")) | ||
} | ||
|
||
@Test | ||
fun mapSecond_returns_transformation_does_not_touch_first_element() { | ||
val obj = listOf('h', 'e', 'l', 'l', 'o') | ||
expect(Pair(obj, 1).mapSecond { it + 1 }) { | ||
toEqual(Pair(obj, 2)) | ||
first.toBeTheInstance(obj) | ||
} | ||
} | ||
} |