Skip to content

Commit

Permalink
Make all measurements lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
jcazevedo committed Nov 28, 2022
1 parent c7f4f35 commit fa7d9c3
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 36 deletions.
62 changes: 32 additions & 30 deletions src/main/scala/net/jcazevedo/phalange/Digit.scala
Expand Up @@ -2,18 +2,18 @@ package net.jcazevedo.phalange

private[phalange] sealed abstract class Digit[V, A](implicit measured: Measured[A, V]) extends Iterable[A] {
private[phalange] def fold[B](
one: (V, A) => B,
two: (V, A, A) => B,
three: (V, A, A, A) => B,
four: (V, A, A, A, A) => B
one: (Lazy[V], A) => B,
two: (Lazy[V], A, A) => B,
three: (Lazy[V], A, A, A) => B,
four: (Lazy[V], A, A, A, A) => B
): B

final def measure: V =
fold(
one = (m, _) => m,
two = (m, _, _) => m,
three = (m, _, _, _) => m,
four = (m, _, _, _, _) => m
one = (lm, _) => lm.value,
two = (lm, _, _) => lm.value,
three = (lm, _, _, _) => lm.value,
four = (lm, _, _, _, _) => lm.value
)

final def iterator: Iterator[A] =
Expand Down Expand Up @@ -57,35 +57,35 @@ private[phalange] object Digit {
private[phalange] def apply[V, A](a: A)(implicit measured: Measured[A, V]): Digit[V, A] =
new Digit[V, A] {
def fold[B](
one: (V, A) => B,
two: (V, A, A) => B,
three: (V, A, A, A) => B,
four: (V, A, A, A, A) => B
one: (Lazy[V], A) => B,
two: (Lazy[V], A, A) => B,
three: (Lazy[V], A, A, A) => B,
four: (Lazy[V], A, A, A, A) => B
): B =
one(measured.apply(a), a)
one(Lazy.delay(measured.apply(a)), a)
}

private[phalange] def apply[V, A](a: A, b: A)(implicit measured: Measured[A, V]): Digit[V, A] =
new Digit[V, A] {
def fold[B](
one: (V, A) => B,
two: (V, A, A) => B,
three: (V, A, A, A) => B,
four: (V, A, A, A, A) => B
one: (Lazy[V], A) => B,
two: (Lazy[V], A, A) => B,
three: (Lazy[V], A, A, A) => B,
four: (Lazy[V], A, A, A, A) => B
): B =
two(measured.append(measured.apply(a), measured.apply(b)), a, b)
two(Lazy.delay(measured.append(measured.apply(a), measured.apply(b))), a, b)
}

private[phalange] def apply[V, A](a: A, b: A, c: A)(implicit measured: Measured[A, V]): Digit[V, A] =
new Digit[V, A] {
def fold[B](
one: (V, A) => B,
two: (V, A, A) => B,
three: (V, A, A, A) => B,
four: (V, A, A, A, A) => B
one: (Lazy[V], A) => B,
two: (Lazy[V], A, A) => B,
three: (Lazy[V], A, A, A) => B,
four: (Lazy[V], A, A, A, A) => B
): B =
three(
measured.append(measured.apply(a), measured.append(measured.apply(b), measured.apply(c))),
Lazy.delay(measured.append(measured.apply(a), measured.append(measured.apply(b), measured.apply(c)))),
a,
b,
c
Expand All @@ -95,15 +95,17 @@ private[phalange] object Digit {
private[phalange] def apply[V, A](a: A, b: A, c: A, d: A)(implicit measured: Measured[A, V]): Digit[V, A] =
new Digit[V, A] {
def fold[B](
one: (V, A) => B,
two: (V, A, A) => B,
three: (V, A, A, A) => B,
four: (V, A, A, A, A) => B
one: (Lazy[V], A) => B,
two: (Lazy[V], A, A) => B,
three: (Lazy[V], A, A, A) => B,
four: (Lazy[V], A, A, A, A) => B
): B =
four(
measured.append(
measured.apply(a),
measured.append(measured.apply(b), measured.append(measured.apply(c), measured.apply(d)))
Lazy.delay(
measured.append(
measured.apply(a),
measured.append(measured.apply(b), measured.append(measured.apply(c), measured.apply(d)))
)
),
a,
b,
Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/net/jcazevedo/phalange/Node.scala
@@ -1,10 +1,10 @@
package net.jcazevedo.phalange

private[phalange] sealed abstract class Node[V, A](implicit measured: Measured[A, V]) extends Iterable[A] {
private[phalange] def fold[B](node2: (V, A, A) => B, node3: (V, A, A, A) => B): B
private[phalange] def fold[B](node2: (Lazy[V], A, A) => B, node3: (Lazy[V], A, A, A) => B): B

final def measure: V =
fold(node2 = (m, _, _) => m, node3 = (m, _, _, _) => m)
fold(node2 = (lm, _, _) => lm.value, node3 = (lm, _, _, _) => lm.value)

final def iterator: Iterator[A] =
fold(node2 = (_, a, b) => Iterator(a, b), node3 = (_, a, b, c) => Iterator(a, b, c))
Expand All @@ -16,15 +16,15 @@ private[phalange] sealed abstract class Node[V, A](implicit measured: Measured[A
private[phalange] object Node {
private[phalange] def apply[V, A](a: A, b: A)(implicit measured: Measured[A, V]): Node[V, A] =
new Node[V, A] {
def fold[B](node2: (V, A, A) => B, node3: (V, A, A, A) => B): B =
node2(measured.append(measured.apply(a), measured.apply(b)), a, b)
def fold[B](node2: (Lazy[V], A, A) => B, node3: (Lazy[V], A, A, A) => B): B =
node2(Lazy.delay(measured.append(measured.apply(a), measured.apply(b))), a, b)
}

private[phalange] def apply[V, A](a: A, b: A, c: A)(implicit measured: Measured[A, V]): Node[V, A] =
new Node[V, A] {
def fold[B](node2: (V, A, A) => B, node3: (V, A, A, A) => B): B =
def fold[B](node2: (Lazy[V], A, A) => B, node3: (Lazy[V], A, A, A) => B): B =
node3(
measured.append(measured.apply(a), measured.append(measured.apply(b), measured.apply(c))),
Lazy.delay(measured.append(measured.apply(a), measured.append(measured.apply(b), measured.apply(c)))),
a,
b,
c
Expand Down

0 comments on commit fa7d9c3

Please sign in to comment.