Mercurial > hg > Members > tatsuki > functionaljava-master > core
diff src/main/java/fj/data/fingertrees/Four.java @ 0:fe80c1edf1be
add getLoop
author | tatsuki |
---|---|
date | Fri, 20 Mar 2015 21:04:03 +0900 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/main/java/fj/data/fingertrees/Four.java Fri Mar 20 21:04:03 2015 +0900 @@ -0,0 +1,39 @@ +package fj.data.fingertrees; + +import fj.data.vector.V4; +import fj.F; + +/** + * A four-element prefix or suffix of a finger tree. + */ +public final class Four<V, A> extends Digit<V, A> { + private final V4<A> as; + + Four(final Measured<V, A> m, final V4<A> as) { + super(m); + this.as = as; + } + + public <B> B foldRight(final F<A, F<B, B>> aff, final B z) { + return aff.f(as._1()).f(aff.f(as._2()).f(aff.f(as._3()).f(aff.f(as._4()).f(z)))); + } + + public <B> B foldLeft(final F<B, F<A, B>> bff, final B z) { + return as.toStream().foldLeft(bff, z); + } + + @Override public <B> B match( + final F<One<V, A>, B> one, final F<Two<V, A>, B> two, final F<Three<V, A>, B> three, + final F<Four<V, A>, B> four) { + return four.f(this); + } + + /** + * Returns the elements of this digit as a vector. + * + * @return the elements of this digit as a vector. + */ + public V4<A> values() { + return as; + } +}