Skip to content

Commit d3db69f

Browse files
committed
[base] Implement id versions of the FromSplices Repeateds
1 parent a27d08d commit d3db69f

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

Base/src/main/scala/typeclass/Repeated.scala

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,37 @@ object Repeated extends VersionSpecificRepeated with LowPrioRepeated {
145145
)
146146
}
147147

148+
/**
149+
* @param newAccumulator a Function0 that creates a new Builder
150+
* @version 0.1.1
151+
*/
152+
def idFromSplicesUsingBuilder[A, Z](
153+
newAccumulator: () => Builder[A, Z],
154+
): Repeated[SplicePiece[Id, A], Z] = {
155+
final class FromSplicesUsingBuilder extends Repeated[SplicePiece[Id, A], Z] {
156+
type Acc = Builder[A, Z]
157+
def init(): Acc = newAccumulator()
158+
def append(acc: Acc, piece: SplicePiece[Id, A]): Acc = {
159+
piece match {
160+
case SplicePiece.Zero() =>
161+
acc
162+
case SplicePiece.One(elem) =>
163+
acc.+=(elem)
164+
case SplicePiece.Many(iter) =>
165+
acc.++=(iter)
166+
}
167+
}
168+
def result(acc: Acc): Z = acc.result()
169+
}
170+
new FromSplicesUsingBuilder()
171+
}
172+
173+
/**
174+
* @version 0.1.1
175+
*/
176+
implicit def idFromSplicesToList[A]: Repeated[SplicePiece[Id, A], List[A]] =
177+
idFromSplicesUsingBuilder(() => List.newBuilder)
178+
148179
/**
149180
* @version 0.1.1
150181
*/

Base/src/test/scala/typeclass/RepeatedTest.scala

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,27 @@ final class IdConcatenateString extends BaseInterpolatorSuite {
1919
assertParseSuccess(dut, ("ab" :: "cd" :: "ef" :: Nil, "12" :: "34" :: Nil), "ab12cd34ef")
2020
}
2121
}
22+
23+
final class IdFromSplicesToList extends BaseInterpolatorSuite {
24+
import Repeated.SplicePiece
25+
val dut = (ofType[SplicePiece[Id, Int]])
26+
.repeat()(using Repeated.idFromSplicesToList)
27+
.andThen(end)
28+
29+
test ("Nil") {
30+
assertParseSuccess(dut, ("" :: Nil, Nil), Nil)
31+
}
32+
test ("Zero") {
33+
assertParseSuccess(dut, ("" :: "" :: Nil, SplicePiece.Zero[Id]() :: Nil), Nil)
34+
}
35+
test ("Scalar") {
36+
assertParseSuccess(dut, ("" :: "" :: Nil, SplicePiece.One[Id, Int](42) :: Nil), 42 :: Nil)
37+
}
38+
test ("Splice") {
39+
assertParseSuccess(dut, ("" :: "" :: Nil, SplicePiece.Many[Id, Int](3 to 6) :: Nil), 3 :: 4 :: 5 :: 6 :: Nil)
40+
}
41+
test ("Mix") {
42+
assertParseSuccess(dut, ("" :: "" :: "" :: "" :: Nil,
43+
SplicePiece.Many[Id, Int](4 to 2 by -1) :: SplicePiece.Zero[Id]() :: SplicePiece.One[Id, Int](151) :: Nil), 4 :: 3 :: 2 :: 151 :: Nil)
44+
}
45+
}

0 commit comments

Comments
 (0)