the right of focus. */ def next: Option[Zipper[A]] = rights match { case Stream.Empty => None case r #:: rs => Some(zipper(Stream.cons(focus, lefts), r, rs)) } /** * Possibly moves to next element to the right of focus. */ def nextOr[AA >: A](z: => Zipper[AA]): Zipper[AA] = next getOrElse z /** * Possibly moves to the previous element to the left of focus. */ def previous: Option[Zipper[A]] = lefts match { case Stream.Empty => None case l #:: ls => Some(zipper(ls, l, Stream.cons(focus, rights))) } /** * Possibly moves to previous element to the left of focus. */ def previousOr[AA >: A](z: => Zipper[AA]): Zipper[AA] = previous getOrElse z /** * Moves focus n elements in the zipper, or None if there is no such element. * * @param n number of elements to move (positive is forward, negative is backwards) */ def move(n: Int): Option[Zipper[A]] = { @tailrec def move0(z: Option[Zipper[A]], n: Int): Option[Zipper[A]] =
if (n > 0 && rights.isEmpty || n < 0 && lefts.isEmpty) None else { if (n == 0) z else if (n > 0) move0(z flatMap ((_: Zipper[A]).next), n - 1) else move0(z flatMap ((_: Zipper[A]).previous), n + 1) } move0(Some(this), n) } /** * Moves focus to the start of the zipper. */ def start: Zipper[A] = { val rights = this.lefts.reverse ++ focus #:: this.rights this.copy(Stream.Empty, rights.head, rights.tail) } /** * Moves focus to the end of the zipper. */ def end: Zipper[A] = { val lefts = this.rights.reverse ++ focus #:: this.lefts this.copy(lefts.tail, lefts.head, Stream.empty) } /** * Moves focus to the nth element of the zipper, or the default if there is no such element. */ def moveOr[AA >: A](n: Int, z: => Zipper[AA]): Zipper[AA] = move(n) getOrElse z ...
start,end,move,next,previous移动方式都齐了。还有定位函数:
... /** * Moves focus to the nearest element matching the given predicate, preferring the left, * or None if no element matches. */ def findZ(p: A => Boolean): Option[Zipper[A]] =
if (p(focus)) Some(this) else { val c = this.positions std.stream.interleave(c.lefts, c.rights).find((x => p(x.focus))) } /** * Moves focus to the nearest element matching the given predicate, preferring the left, * or the default if no element matches. */ def findZor[AA >: A](p: A => Boolean, z: => Zipper[AA]): Zipper[AA] = findZ(p) getOrElse z /** * Given a traversal function, find the first element along the traversal that matches a given predicate. */ def findBy[AA >: A](f: Zipper[AA] => Option[Zipper[AA]])(p: AA => Boolean): Option[Zipper[AA]] = { @tailrec def go(zopt: Option[Zipper[AA]]): Option[Zipper[AA]] = { zopt match { case Some(z) => if (p(z.focus)) Some(z) else go(f(z)) case None => None } } go(f(this)) } /** * Moves focus to the nearest element on the right that matches the given predicate, * or None if there is no such element. */ def findNext(p: A => Boolean): Option[Zipper[A]] = findBy((z: Zipper[A]) => z.next)(p) /** * Moves focus to the previous element on the left that matches the given predicate, * or None if there is no such element. */ def findPrevious(p: A => Boolean): Option[Zipper[A]] = findBy((z: Zipper[A]) => z.previous)(p) ...
操作函数如下:
... /** * An alias for insertRight */ def insert[AA >: A]: (AA => Zipper[AA]) = insertRight(_: AA) /** * Inserts an element to the left of focus and focuses on the new element. */ def insertLeft[AA >: A](y: AA): Zipper[AA] = zipper(lefts, y, focus #:: rights) /** * Inserts an element to the right of focus and focuses on the new element. */ def insertRight[AA & |