Skip to content

Commit 50066eb

Browse files
authored
Merge pull request #41 from rcardin/38-add-a-ziporaccumulate-variant-that-accumulates-on-a-custom-type
Add a ziporaccumulate variant that accumulates on a custom type
2 parents 15245fe + 29610d6 commit 50066eb

File tree

3 files changed

+754
-32
lines changed

3 files changed

+754
-32
lines changed

src/main/scala/in/rcard/raise4s/Fold.scala

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package in.rcard.raise4s
22

3-
import scala.annotation.targetName
4-
53
private[raise4s] def _mapOrAccumulate[Error, A, B](iterable: Iterable[A])(
64
transform: Raise[Error] ?=> A => B
75
)(using r: Raise[List[Error]]): List[B] =
@@ -23,7 +21,7 @@ private[raise4s] def _mapOrAccumulate[Error, A, B](
2321
)(
2422
transform: Raise[Error] ?=> A => B
2523
)(using r: Raise[Error]): List[B] =
26-
var errors = collection.mutable.ArrayBuffer.empty[Error]
24+
val errors = collection.mutable.ArrayBuffer.empty[Error]
2725
val results = collection.mutable.ArrayBuffer.empty[B]
2826
iterable.foreach(a =>
2927
Raise.fold(
@@ -94,3 +92,48 @@ private[raise4s] def _zipOrAccumulate[Error, A, B, C, D, E, F, G, H, I, J](
9492
if errors.isEmpty then block(a, b, c, d, e, f, g, h, i)
9593
else r.raise(errors.toList)
9694
}
95+
96+
private[raise4s] def _zipOrAccumulate[Error, A, B, C, D, E, F, G, H, I, J](
97+
combine: (Error, Error) => Error
98+
)(
99+
action1: Raise[Error] ?=> A,
100+
action2: Raise[Error] ?=> B,
101+
action3: Raise[Error] ?=> C,
102+
action4: Raise[Error] ?=> D,
103+
action5: Raise[Error] ?=> E,
104+
action6: Raise[Error] ?=> F,
105+
action7: Raise[Error] ?=> G,
106+
action8: Raise[Error] ?=> H,
107+
action9: Raise[Error] ?=> I
108+
)(block: (A, B, C, D, E, F, G, H, I) => J)(using r: Raise[Error]): J = {
109+
val errors = collection.mutable.ArrayBuffer.empty[Error]
110+
val a: A = Raise.recover(action1) { newError =>
111+
errors += newError; null.asInstanceOf[A]
112+
}
113+
val b: B = Raise.recover(action2) { newError =>
114+
errors += newError; null.asInstanceOf[B]
115+
}
116+
val c: C = Raise.recover(action3) { newError =>
117+
errors += newError; null.asInstanceOf[C]
118+
}
119+
val d: D = Raise.recover(action4) { newError =>
120+
errors += newError; null.asInstanceOf[D]
121+
}
122+
val e: E = Raise.recover(action5) { newError =>
123+
errors += newError; null.asInstanceOf[E]
124+
}
125+
val f: F = Raise.recover(action6) { newError =>
126+
errors += newError; null.asInstanceOf[F]
127+
}
128+
val g: G = Raise.recover(action7) { newError =>
129+
errors += newError; null.asInstanceOf[G]
130+
}
131+
val h: H = Raise.recover(action8) { newError =>
132+
errors += newError; null.asInstanceOf[H]
133+
}
134+
val i: I = Raise.recover(action9) { newError =>
135+
errors += newError; null.asInstanceOf[I]
136+
}
137+
if errors.isEmpty then block(a, b, c, d, e, f, g, h, i)
138+
else r.raise(errors.reduce(combine))
139+
}

0 commit comments

Comments
 (0)