Skip to content

Commit

Permalink
renamed traverse to mapAllValues following conventions
Browse files Browse the repository at this point in the history
Signed-off-by: Uberto Barbini <uberto.gama@gmail.com>
  • Loading branch information
uberto committed Jul 14, 2023
1 parent 1a45669 commit 0888b34
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,11 @@ fun <T, Tʹ, E> Iterable<T>.foldResult(
): Result<Tʹ, E> =
fold(initial) { acc, el -> acc.flatMap { accVal -> operation(accVal, el) } }

fun <T, Tʹ, E> Iterable<T>.traverse(f: (T) -> Result<Tʹ, E>): Result<List<Tʹ>, E> =
fun <T, Tʹ, E> Iterable<T>.mapAllValues(f: (T) -> Result<Tʹ, E>): Result<List<Tʹ>, E> =
foldResult(Success(mutableListOf())) { acc, e ->
f(e).map { acc.add(it); acc }
}

fun <T, E> Iterable<Result<T, E>>.extractList(): Result<List<T>, E> =
traverse { it }

fun <T, Tʹ, E> Sequence<T>.foldResult(
initial: Result<Tʹ, E>,
operation: (acc: Tʹ, T) -> Result<Tʹ, E>
Expand All @@ -50,10 +47,10 @@ fun <T, Tʹ, E> Sequence<T>.foldResult(
return loop(initial)
}

fun <T, Tʹ, E> Sequence<T>.traverse(f: (T) -> Result<Tʹ, E>): Result<List<Tʹ>, E> =
fun <T, Tʹ, E> Sequence<T>.mapAllValues(f: (T) -> Result<Tʹ, E>): Result<List<Tʹ>, E> =
foldResult(Success(mutableListOf())) { acc, e ->
f(e).map { acc.add(it); acc }
}

fun <T, E> Sequence<Result<T, E>>.extractList(): Result<List<T>, E> =
traverse { it }
fun <T, E> Sequence<Result<T, E>>.allValues(): Result<List<T>, E> =
mapAllValues { it }
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,14 @@ class TraverseIterableTests {
fun `returns the first failure or the mapping of iterable as success`() {
val list = generateRandomList()
assertEquals(Success(list.map { it * 2 }),
list.traverse { i -> Success(i * 2) })
list.mapAllValues { i -> Success(i * 2) })
}

@Test
fun `returns the first failure or the iterable as success`() {
val list = generateRandomList().map { Success(it) }
assertEquals(Success(list.map { it.value }),
list.extractList())
list.allValues())
}

@Test
Expand All @@ -83,14 +83,14 @@ class TraverseIterableTests {
val list = generateRandomList()
val failingFunction: (Int) -> Result<Int, String> = { _ -> Failure("Test error") }
assertEquals(Failure("Test error"),
list.traverse(failingFunction))
list.mapAllValues(failingFunction))
}

@Test
fun `failure is returned if the iterable contained a failure`() {
val list = listOf(Success(randomPositiveInt()), Failure("Test error"))
assertEquals(Failure("Test error"),
list.extractList())
list.allValues())
}
}

Expand All @@ -109,15 +109,15 @@ class TraverseSequenceTests {
val list = generateRandomList()
val sequence = list.asSequence()
assertEquals(Success(list.map { it * 2 }),
sequence.traverse { i -> Success(i * 2) })
sequence.mapAllValues { i -> Success(i * 2) })
}

@Test
fun `returns the first failure or the mapped sequence as success`() {
val list = generateRandomList()
val sequence = list.map { Success(it) }.asSequence()
assertEquals(Success(list),
sequence.extractList())
sequence.allValues())
}

@Test
Expand All @@ -133,13 +133,13 @@ class TraverseSequenceTests {
val sequence = generateSequence { randomPositiveInt() }.take(5)
val failingFunction: (Int) -> Result<Int, String> = { _ -> Failure("Test error") }
assertEquals(Failure("Test error"),
sequence.traverse(failingFunction))
sequence.mapAllValues(failingFunction))
}

@Test
fun `failure is returned if the sequence contained a failure`() {
val sequence = generateSequence { Success(randomPositiveInt()) }.take(5) + sequenceOf(Failure("Test error"))
assertEquals(Failure("Test error"),
sequence.extractList())
sequence.allValues())
}
}

0 comments on commit 0888b34

Please sign in to comment.