Skip to content

Commit

Permalink
#4 - First steps at deque implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
alf239 committed May 4, 2019
1 parent f075b28 commit d23b935
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
27 changes: 24 additions & 3 deletions src/main/scala/finger/Fingers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ object Fingers {
case class Deep[A](l: Digit[A], down: FingerTree[Node[A]], r: Digit[A]) extends FingerTree[A]

// ==========================================================================================
sealed trait Node[A]
sealed trait Node[+A]

case class Node2[A](a: A, b: A) extends Node[A]

Expand Down Expand Up @@ -56,13 +56,34 @@ object Fingers {
override def reducer[A, B](fn: (A, B) => B): (FingerTree[A], B) => B = (fta, z) => fta match {
case Empty => z
case Single(a) => fn(a, z)
case Deep(pr, m, sf) => ???
case Deep(pr, m, sf) =>
val fn1 = listReduce.reducer(fn)
val fn2 = fingerTreeReduce.reducer(nodeReduce.reducer(fn))
fn1(pr, fn2(m, fn1(sf, z)))
}

override def reducel[A, B](fn: (B, A) => B): (B, FingerTree[A]) => B = (z, fta) => fta match {
case Empty => z
case Single(a) => fn(z, a)
case Deep(pr, m, sf) => ???
case Deep(pr, m, sf) =>
val fn1 = listReduce.reducel(fn)
val fn2 = fingerTreeReduce.reducel(nodeReduce.reducel(fn))
fn1(fn2(fn1(z, pr), m), sf)
}
}

// ==========================================================================================
def cons[A](a: A, ft: FingerTree[A]): FingerTree[A] = ft match {
case Empty => Single(a)
case Single(b) => Deep(List(a), Empty, List(b))
case Deep(List(b, c, d, e), m, sf) => Deep(List(a, b), cons(Node3(c, d, e), m), sf)
case Deep(pr, m, sf) => Deep(a :: pr, m, sf)
}

def snoc[A](ft: FingerTree[A], a: A): FingerTree[A] = ft match {
case Empty => Single(a)
case Single(b) => Deep(List(b), Empty, List(a))
case Deep(pr, m, List(b, c, d, e)) => Deep(pr, snoc(m, Node3(e, d, c)), List(a, b))
case Deep(pr, m, sf) => Deep(pr, m, a :: sf)
}
}
2 changes: 1 addition & 1 deletion src/main/scala/okasaki/maps/TrieOfTrees.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ object TrieOfTrees {
val map = implicitly[FiniteMapLike[M]].ops[K, Trie[K, Trie[K, V, M], M]]
k match {
case E => t.copy(v = Some(v))
case T(x: K, l, r) =>
case T(x, l, r) =>
val tt = Try(map.lookup(x, t.m)) getOrElse empty[K, Trie[K, V, M], M]
val t1 = Try(lookup(l, tt)) getOrElse empty[K, V, M]
val t2 = bind(r, v, t1)
Expand Down

0 comments on commit d23b935

Please sign in to comment.