Skip to content

Commit

Permalink
Curried the mapOrAccumulate function
Browse files Browse the repository at this point in the history
  • Loading branch information
rcardin committed Apr 22, 2024
1 parent 0704545 commit 4da811e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 45 deletions.
3 changes: 1 addition & 2 deletions src/main/scala/in/rcard/raise4s/Fold.scala
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ def fold[A, B, Error](
transform: (value: A) => B
): B = _fold(block, ex => throw ex, recover, transform)

private[raise4s] def mapOrAccumulate[Error, A, B](
iterable: Iterable[A],
private[raise4s] def _mapOrAccumulate[Error, A, B](iterable: Iterable[A])(
transform: Raise[Error] ?=> A => B
)(using r: Raise[List[Error]]): List[B] =
val errors = collection.mutable.ArrayBuffer.empty[Error]
Expand Down
53 changes: 30 additions & 23 deletions src/main/scala/in/rcard/raise4s/Raise.scala
Original file line number Diff line number Diff line change
Expand Up @@ -387,31 +387,38 @@ object Raise {
def asTry[A](block: Raise[Throwable] ?=> A): Try[A] = _asTry(block)

/** Accumulate the errors obtained by executing the `transform` over every element of `iterable`.
*
* <h2>Example</h2>
* {{{
* val block: List[Int] raises List[String] = Raise.mapOrAccumulate(
* List(1, 2, 3, 4, 5),
* _ + 1
* )
* val actual = Raise.fold(
* block,
* error => fail(s"An error occurred: $error"),
* identity
* )
* actual shouldBe List(2, 3, 4, 5, 6)
* }}}
*
* @param iterable The collection of elements to transform
* @param transform The transformation to apply to each element that can raise an error of type `Error`
* @param r The Raise context
* @tparam Error The type of the logical error that can be raised
* @tparam A The type of the elements in the `iterable`
* @tparam B The type of the transformed elements
* @return A list of transformed elements
*
* <h2>Example</h2>
* {{{
* val block: List[Int] raises List[String] = Raise.mapOrAccumulate(
* List(1, 2, 3, 4, 5),
* _ + 1
* )
* val actual = Raise.fold(
* block,
* error => fail(s"An error occurred: $error"),
* identity
* )
* actual shouldBe List(2, 3, 4, 5, 6)
* }}}
*
* @param iterable
* The collection of elements to transform
* @param transform
* The transformation to apply to each element that can raise an error of type `Error`
* @param r
* The Raise context
* @tparam Error
* The type of the logical error that can be raised
* @tparam A
* The type of the elements in the `iterable`
* @tparam B
* The type of the transformed elements
* @return
* A list of transformed elements
*/
def mapOrAccumulate[Error, A, B](
iterable: Iterable[A],
transform: Raise[Error] ?=> A => B
)(using r: Raise[List[Error]]): List[B] = in.rcard.raise4s.mapOrAccumulate(iterable, transform)
)(using r: Raise[List[Error]]): List[B] = _mapOrAccumulate(iterable)(transform)
}
34 changes: 14 additions & 20 deletions src/test/scala/in/rcard/raise4s/FoldSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,9 @@ class FoldSpec extends AnyFlatSpec with Matchers {
}

"mapOrAccumulate" should "map all the element of the iterable" in {
val block: List[Int] raises List[String] = Raise.mapOrAccumulate(
List(1, 2, 3, 4, 5),
val block: List[Int] raises List[String] = Raise.mapOrAccumulate(List(1, 2, 3, 4, 5)) {
_ + 1
)
}

val actual = Raise.fold(
block,
Expand All @@ -99,16 +98,13 @@ class FoldSpec extends AnyFlatSpec with Matchers {
}

it should "accumulate all the errors" in {
val block: List[Int] raises List[String] = Raise.mapOrAccumulate(
List(1, 2, 3, 4, 5),
{ value =>
if (value % 2 == 0) {
Raise.raise(value.toString)
} else {
value
}
val block: List[Int] raises List[String] = Raise.mapOrAccumulate(List(1, 2, 3, 4, 5)) { value =>
if (value % 2 == 0) {
Raise.raise(value.toString)
} else {
value
}
)
}

val actual = Raise.fold(
block,
Expand All @@ -132,15 +128,13 @@ class FoldSpec extends AnyFlatSpec with Matchers {
}

it should "accumulate all the errors on the receiver" in {
val block: List[Int] raises List[String] = List(1, 2, 3, 4, 5).mapOrAccumulate(
{ value =>
if (value % 2 == 0) {
Raise.raise(value.toString)
} else {
value
}
val block: List[Int] raises List[String] = List(1, 2, 3, 4, 5).mapOrAccumulate { value =>
if (value % 2 == 0) {
Raise.raise(value.toString)
} else {
value
}
)
}

val actual = Raise.fold(
block,
Expand Down

0 comments on commit 4da811e

Please sign in to comment.