001 package fj.data.fingertrees; 002 003 import fj.data.vector.V4; 004 import fj.F; 005 006 /** 007 * A four-element prefix or suffix of a finger tree. 008 */ 009 public final class Four<V, A> extends Digit<V, A> { 010 private final V4<A> as; 011 012 Four(final Measured<V, A> m, final V4<A> as) { 013 super(m); 014 this.as = as; 015 } 016 017 /** 018 * @see fj.data.fingertrees.Digit#foldRight(fj.F, Object) 019 */ 020 public <B> B foldRight(final F<A, F<B, B>> aff, final B z) { 021 return aff.f(as._1()).f(aff.f(as._2()).f(aff.f(as._3()).f(aff.f(as._4()).f(z)))); 022 } 023 024 /** 025 * @see fj.data.fingertrees.Digit#foldLeft(fj.F, Object) 026 */ 027 public <B> B foldLeft(final F<B, F<A, B>> bff, final B z) { 028 return as.toStream().foldLeft(bff, z); 029 } 030 031 /** 032 * @see fj.data.fingertrees.Digit#match(fj.F, fj.F, fj.F, fj.F) 033 */ 034 @Override public <B> B match( 035 final F<One<V, A>, B> one, final F<Two<V, A>, B> two, final F<Three<V, A>, B> three, 036 final F<Four<V, A>, B> four) { 037 return four.f(this); 038 } 039 040 /** 041 * Returns the elements of this digit as a vector. 042 * 043 * @return the elements of this digit as a vector. 044 */ 045 public V4<A> values() { 046 return as; 047 } 048 }