From 4da811ed2eaa64b19ef5ae265b389ad40e25d053 Mon Sep 17 00:00:00 2001 From: rcardin Date: Mon, 22 Apr 2024 08:16:04 +0200 Subject: [PATCH] Curried the mapOrAccumulate function --- src/main/scala/in/rcard/raise4s/Fold.scala | 3 +- src/main/scala/in/rcard/raise4s/Raise.scala | 53 +++++++++++-------- .../scala/in/rcard/raise4s/FoldSpec.scala | 34 +++++------- 3 files changed, 45 insertions(+), 45 deletions(-) diff --git a/src/main/scala/in/rcard/raise4s/Fold.scala b/src/main/scala/in/rcard/raise4s/Fold.scala index ebb3ce3..ae0e6f9 100644 --- a/src/main/scala/in/rcard/raise4s/Fold.scala +++ b/src/main/scala/in/rcard/raise4s/Fold.scala @@ -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] diff --git a/src/main/scala/in/rcard/raise4s/Raise.scala b/src/main/scala/in/rcard/raise4s/Raise.scala index 849a02b..2a4ff6e 100644 --- a/src/main/scala/in/rcard/raise4s/Raise.scala +++ b/src/main/scala/in/rcard/raise4s/Raise.scala @@ -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`. - * - *

Example

- * {{{ - * 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 + * + *

Example

+ * {{{ + * 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) } diff --git a/src/test/scala/in/rcard/raise4s/FoldSpec.scala b/src/test/scala/in/rcard/raise4s/FoldSpec.scala index fdae234..f82a174 100644 --- a/src/test/scala/in/rcard/raise4s/FoldSpec.scala +++ b/src/test/scala/in/rcard/raise4s/FoldSpec.scala @@ -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, @@ -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, @@ -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,