Mercurial > hg > Members > tatsuki > functionaljava-master > core
changeset 1:8ed7d71e8617
minner change
author | tatsuki |
---|---|
date | Sat, 21 Mar 2015 05:32:16 +0900 |
parents | fe80c1edf1be |
children | d2b4440b2cc0 |
files | src/main/java/fj/Ord.java src/main/java/fj/P.java src/main/java/fj/data/List.java src/main/java/fj/data/Set.java src/main/java/fj/data/TreeMap.java |
diffstat | 5 files changed, 872 insertions(+), 873 deletions(-) [+] |
line wrap: on
line diff
--- a/src/main/java/fj/Ord.java Fri Mar 20 21:04:03 2015 +0900 +++ b/src/main/java/fj/Ord.java Sat Mar 21 05:32:16 2015 +0900 @@ -21,665 +21,667 @@ * @version %build.number% */ public final class Ord<A> { - private final F<A, F<A, Ordering>> f; + private final F<A, F<A, Ordering>> f; - private Ord(final F<A, F<A, Ordering>> f) { - this.f = f; - } + private Ord(final F<A, F<A, Ordering>> f) { + this.f = f; + } - /** - * First-class ordering. - * - * @return A function that returns an ordering for its arguments. - */ - public F<A, F<A, Ordering>> compare() { - return f; - } + /** + * First-class ordering. + * + * @return A function that returns an ordering for its arguments. + */ + public F<A, F<A, Ordering>> compare() { + return f; + } - /** - * Returns an ordering for the given arguments. - * - * @param a1 An instance to compare for ordering to another. - * @param a2 An instance to compare for ordering to another. - * @return An ordering for the given arguments. - */ - public Ordering compare(final A a1, final A a2) { - return f.f(a1).f(a2); - } + /** + * Returns an ordering for the given arguments. + * + * @param a1 An instance to compare for ordering to another. + * @param a2 An instance to compare for ordering to another. + * @return An ordering for the given arguments. + */ + public Ordering compare(final A a1, final A a2) { + F<A, Ordering> f1 = f.f(a1); + return f1.f(a2); + } + - /** - * Returns <code>true</code> if the given arguments are equal, <code>false</code> otherwise. - * - * @param a1 An instance to compare for equality to another. - * @param a2 An instance to compare for equality to another. - * @return <code>true</code> if the given arguments are equal, <code>false</code> otherwise. - */ - public boolean eq(final A a1, final A a2) { - return compare(a1, a2) == Ordering.EQ; - } + /** + * Returns <code>true</code> if the given arguments are equal, <code>false</code> otherwise. + * + * @param a1 An instance to compare for equality to another. + * @param a2 An instance to compare for equality to another. + * @return <code>true</code> if the given arguments are equal, <code>false</code> otherwise. + */ + public boolean eq(final A a1, final A a2) { + return compare(a1, a2) == Ordering.EQ; + } - /** - * Returns an <code>Equal</code> for this order. - * - * @return An <code>Equal</code> for this order. - */ - public Equal<A> equal() { - return Equal.equal(curry(new F2<A, A, Boolean>() { - public Boolean f(final A a1, final A a2) { - return eq(a1, a2); - } - })); - } + /** + * Returns an <code>Equal</code> for this order. + * + * @return An <code>Equal</code> for this order. + */ + public Equal<A> equal() { + return Equal.equal(curry(new F2<A, A, Boolean>() { + public Boolean f(final A a1, final A a2) { + return eq(a1, a2); + } + })); + } - /** - * Maps the given function across this ord as a contra-variant functor. - * - * @param f The function to map. - * @return A new ord. - */ - public <B> Ord<B> comap(final F<B, A> f) { - return ord(F1Functions.o(F1Functions.o(F1Functions.<B, A, Ordering>andThen(f), this.f), f)); - } + /** + * Maps the given function across this ord as a contra-variant functor. + * + * @param f The function to map. + * @return A new ord. + */ + public <B> Ord<B> comap(final F<B, A> f) { + return ord(F1Functions.o(F1Functions.o(F1Functions.<B, A, Ordering>andThen(f), this.f), f)); + } - /** - * Returns <code>true</code> if the first given argument is less than the second given argument, - * <code>false</code> otherwise. - * - * @param a1 An instance to compare for ordering to another. - * @param a2 An instance to compare for ordering to another. - * @return <code>true</code> if the first given argument is less than the second given argument, - * <code>false</code> otherwise. - */ - public boolean isLessThan(final A a1, final A a2) { - return compare(a1, a2) == Ordering.LT; - } + /** + * Returns <code>true</code> if the first given argument is less than the second given argument, + * <code>false</code> otherwise. + * + * @param a1 An instance to compare for ordering to another. + * @param a2 An instance to compare for ordering to another. + * @return <code>true</code> if the first given argument is less than the second given argument, + * <code>false</code> otherwise. + */ + public boolean isLessThan(final A a1, final A a2) { + return compare(a1, a2) == Ordering.LT; + } - /** - * Returns <code>true</code> if the first given argument is greater than the second given - * argument, <code>false</code> otherwise. - * - * @param a1 An instance to compare for ordering to another. - * @param a2 An instance to compare for ordering to another. - * @return <code>true</code> if the first given argument is greater than the second given - * argument, <code>false</code> otherwise. - */ - public boolean isGreaterThan(final A a1, final A a2) { - return compare(a1, a2) == Ordering.GT; - } + /** + * Returns <code>true</code> if the first given argument is greater than the second given + * argument, <code>false</code> otherwise. + * + * @param a1 An instance to compare for ordering to another. + * @param a2 An instance to compare for ordering to another. + * @return <code>true</code> if the first given argument is greater than the second given + * argument, <code>false</code> otherwise. + */ + public boolean isGreaterThan(final A a1, final A a2) { + return compare(a1, a2) == Ordering.GT; + } - /** - * Returns a function that returns true if its argument is less than the argument to this method. - * - * @param a A value to compare against. - * @return A function that returns true if its argument is less than the argument to this method. - */ - public F<A, Boolean> isLessThan(final A a) { - return new F<A, Boolean>() { - public Boolean f(final A a2) { - return compare(a2, a) == Ordering.LT; - } - }; - } + /** + * Returns a function that returns true if its argument is less than the argument to this method. + * + * @param a A value to compare against. + * @return A function that returns true if its argument is less than the argument to this method. + */ + public F<A, Boolean> isLessThan(final A a) { + return new F<A, Boolean>() { + public Boolean f(final A a2) { + return compare(a2, a) == Ordering.LT; + } + }; + } - /** - * Returns a function that returns true if its argument is greater than than the argument to this method. - * - * @param a A value to compare against. - * @return A function that returns true if its argument is greater than the argument to this method. - */ - public F<A, Boolean> isGreaterThan(final A a) { - return new F<A, Boolean>() { - public Boolean f(final A a2) { - return compare(a2, a) == Ordering.GT; - } - }; - } + /** + * Returns a function that returns true if its argument is greater than than the argument to this method. + * + * @param a A value to compare against. + * @return A function that returns true if its argument is greater than the argument to this method. + */ + public F<A, Boolean> isGreaterThan(final A a) { + return new F<A, Boolean>() { + public Boolean f(final A a2) { + return compare(a2, a) == Ordering.GT; + } + }; + } - /** - * Returns the greater of its two arguments. - * - * @param a1 A value to compare with another. - * @param a2 A value to compare with another. - * @return The greater of the two values. - */ - public A max(final A a1, final A a2) { - return isGreaterThan(a1, a2) ? a1 : a2; - } + /** + * Returns the greater of its two arguments. + * + * @param a1 A value to compare with another. + * @param a2 A value to compare with another. + * @return The greater of the two values. + */ + public A max(final A a1, final A a2) { + return isGreaterThan(a1, a2) ? a1 : a2; + } - /** - * Returns the lesser of its two arguments. - * - * @param a1 A value to compare with another. - * @param a2 A value to compare with another. - * @return The lesser of the two values. - */ - public A min(final A a1, final A a2) { - return isLessThan(a1, a2) ? a1 : a2; - } - - /** - * A function that returns the greater of its two arguments. - */ - public final F<A, F<A, A>> max = curry(new F2<A, A, A>() { - public A f(final A a, final A a1) { - return max(a, a1); + /** + * Returns the lesser of its two arguments. + * + * @param a1 A value to compare with another. + * @param a2 A value to compare with another. + * @return The lesser of the two values. + */ + public A min(final A a1, final A a2) { + return isLessThan(a1, a2) ? a1 : a2; } - }); + + /** + * A function that returns the greater of its two arguments. + */ + public final F<A, F<A, A>> max = curry(new F2<A, A, A>() { + public A f(final A a, final A a1) { + return max(a, a1); + } + }); - /** - * A function that returns the lesser of its two arguments. - */ - public final F<A, F<A, A>> min = curry(new F2<A, A, A>() { - public A f(final A a, final A a1) { - return min(a, a1); + /** + * A function that returns the lesser of its two arguments. + */ + public final F<A, F<A, A>> min = curry(new F2<A, A, A>() { + public A f(final A a, final A a1) { + return min(a, a1); + } + }); + + /** + * Returns an order instance that uses the given equality test and ordering function. + * + * @param f The order function. + * @return An order instance. + */ + public static <A> Ord<A> ord(final F<A, F<A, Ordering>> f) { + return new Ord<A>(f); } - }); - - /** - * Returns an order instance that uses the given equality test and ordering function. - * - * @param f The order function. - * @return An order instance. - */ - public static <A> Ord<A> ord(final F<A, F<A, Ordering>> f) { - return new Ord<A>(f); - } - /** - * An order instance for the <code>boolean</code> type. - */ - public static final Ord<Boolean> booleanOrd = new Ord<Boolean>( - new F<Boolean, F<Boolean, Ordering>>() { - public F<Boolean, Ordering> f(final Boolean a1) { - return new F<Boolean, Ordering>() { - public Ordering f(final Boolean a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>boolean</code> type. + */ + public static final Ord<Boolean> booleanOrd = new Ord<Boolean>( + new F<Boolean, F<Boolean, Ordering>>() { + public F<Boolean, Ordering> f(final Boolean a1) { + return new F<Boolean, Ordering>() { + public Ordering f(final Boolean a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>byte</code> type. - */ - public static final Ord<Byte> byteOrd = new Ord<Byte>( - new F<Byte, F<Byte, Ordering>>() { - public F<Byte, Ordering> f(final Byte a1) { - return new F<Byte, Ordering>() { - public Ordering f(final Byte a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>byte</code> type. + */ + public static final Ord<Byte> byteOrd = new Ord<Byte>( + new F<Byte, F<Byte, Ordering>>() { + public F<Byte, Ordering> f(final Byte a1) { + return new F<Byte, Ordering>() { + public Ordering f(final Byte a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>char</code> type. - */ - public static final Ord<Character> charOrd = new Ord<Character>( - new F<Character, F<Character, Ordering>>() { - public F<Character, Ordering> f(final Character a1) { - return new F<Character, Ordering>() { - public Ordering f(final Character a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>char</code> type. + */ + public static final Ord<Character> charOrd = new Ord<Character>( + new F<Character, F<Character, Ordering>>() { + public F<Character, Ordering> f(final Character a1) { + return new F<Character, Ordering>() { + public Ordering f(final Character a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>double</code> type. - */ - public static final Ord<Double> doubleOrd = new Ord<Double>( - new F<Double, F<Double, Ordering>>() { - public F<Double, Ordering> f(final Double a1) { - return new F<Double, Ordering>() { - public Ordering f(final Double a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>double</code> type. + */ + public static final Ord<Double> doubleOrd = new Ord<Double>( + new F<Double, F<Double, Ordering>>() { + public F<Double, Ordering> f(final Double a1) { + return new F<Double, Ordering>() { + public Ordering f(final Double a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>float</code> type. - */ - public static final Ord<Float> floatOrd = new Ord<Float>( - new F<Float, F<Float, Ordering>>() { - public F<Float, Ordering> f(final Float a1) { - return new F<Float, Ordering>() { - public Ordering f(final Float a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>float</code> type. + */ + public static final Ord<Float> floatOrd = new Ord<Float>( + new F<Float, F<Float, Ordering>>() { + public F<Float, Ordering> f(final Float a1) { + return new F<Float, Ordering>() { + public Ordering f(final Float a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>int</code> type. - */ - public static final Ord<Integer> intOrd = new Ord<Integer>( - new F<Integer, F<Integer, Ordering>>() { - public F<Integer, Ordering> f(final Integer a1) { - return new F<Integer, Ordering>() { - public Ordering f(final Integer a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>int</code> type. + */ + public static final Ord<Integer> intOrd = new Ord<Integer>( + new F<Integer, F<Integer, Ordering>>() { + public F<Integer, Ordering> f(final Integer a1) { + return new F<Integer, Ordering>() { + public Ordering f(final Integer a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>BigInteger</code> type. - */ - public static final Ord<BigInteger> bigintOrd = new Ord<BigInteger>( - new F<BigInteger, F<BigInteger, Ordering>>() { - public F<BigInteger, Ordering> f(final BigInteger a1) { - return new F<BigInteger, Ordering>() { - public Ordering f(final BigInteger a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>BigInteger</code> type. + */ + public static final Ord<BigInteger> bigintOrd = new Ord<BigInteger>( + new F<BigInteger, F<BigInteger, Ordering>>() { + public F<BigInteger, Ordering> f(final BigInteger a1) { + return new F<BigInteger, Ordering>() { + public Ordering f(final BigInteger a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>BigDecimal</code> type. - */ - public static final Ord<BigDecimal> bigdecimalOrd = new Ord<BigDecimal>( - new F<BigDecimal, F<BigDecimal, Ordering>>() { - public F<BigDecimal, Ordering> f(final BigDecimal a1) { - return new F<BigDecimal, Ordering>() { - public Ordering f(final BigDecimal a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>BigDecimal</code> type. + */ + public static final Ord<BigDecimal> bigdecimalOrd = new Ord<BigDecimal>( + new F<BigDecimal, F<BigDecimal, Ordering>>() { + public F<BigDecimal, Ordering> f(final BigDecimal a1) { + return new F<BigDecimal, Ordering>() { + public Ordering f(final BigDecimal a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>long</code> type. - */ - public static final Ord<Long> longOrd = new Ord<Long>( - new F<Long, F<Long, Ordering>>() { - public F<Long, Ordering> f(final Long a1) { - return new F<Long, Ordering>() { - public Ordering f(final Long a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>long</code> type. + */ + public static final Ord<Long> longOrd = new Ord<Long>( + new F<Long, F<Long, Ordering>>() { + public F<Long, Ordering> f(final Long a1) { + return new F<Long, Ordering>() { + public Ordering f(final Long a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the <code>short</code> type. - */ - public static final Ord<Short> shortOrd = new Ord<Short>( - new F<Short, F<Short, Ordering>>() { - public F<Short, Ordering> f(final Short a1) { - return new F<Short, Ordering>() { - public Ordering f(final Short a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the <code>short</code> type. + */ + public static final Ord<Short> shortOrd = new Ord<Short>( + new F<Short, F<Short, Ordering>>() { + public F<Short, Ordering> f(final Short a1) { + return new F<Short, Ordering>() { + public Ordering f(final Short a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the {@link Ordering} type. - */ - public static final Ord<Ordering> orderingOrd = new Ord<Ordering>(curry(new F2<Ordering, Ordering, Ordering>() { - public Ordering f(final Ordering o1, final Ordering o2) { - return o1 == o2 ? - Ordering.EQ : - o1 == Ordering.LT ? - Ordering.LT : - o2 == Ordering.LT ? - Ordering.GT : - o1 == Ordering.EQ ? - Ordering.LT : - Ordering.GT; - } - })); + /** + * An order instance for the {@link Ordering} type. + */ + public static final Ord<Ordering> orderingOrd = new Ord<Ordering>(curry(new F2<Ordering, Ordering, Ordering>() { + public Ordering f(final Ordering o1, final Ordering o2) { + return o1 == o2 ? + Ordering.EQ : + o1 == Ordering.LT ? + Ordering.LT : + o2 == Ordering.LT ? + Ordering.GT : + o1 == Ordering.EQ ? + Ordering.LT : + Ordering.GT; + } + })); - /** - * An order instance for the {@link String} type. - */ - public static final Ord<String> stringOrd = new Ord<String>( - new F<String, F<String, Ordering>>() { - public F<String, Ordering> f(final String a1) { - return new F<String, Ordering>() { - public Ordering f(final String a2) { - final int x = a1.compareTo(a2); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); + /** + * An order instance for the {@link String} type. + */ + public static final Ord<String> stringOrd = new Ord<String>( + new F<String, F<String, Ordering>>() { + public F<String, Ordering> f(final String a1) { + return new F<String, Ordering>() { + public Ordering f(final String a2) { + final int x = a1.compareTo(a2); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); - /** - * An order instance for the {@link StringBuffer} type. - */ - public static final Ord<StringBuffer> stringBufferOrd = - new Ord<StringBuffer>(new F<StringBuffer, F<StringBuffer, Ordering>>() { - public F<StringBuffer, Ordering> f(final StringBuffer a1) { - return new F<StringBuffer, Ordering>() { - public Ordering f(final StringBuffer a2) { - return stringOrd.compare(a1.toString(), a2.toString()); - } - }; - } - }); + /** + * An order instance for the {@link StringBuffer} type. + */ + public static final Ord<StringBuffer> stringBufferOrd = + new Ord<StringBuffer>(new F<StringBuffer, F<StringBuffer, Ordering>>() { + public F<StringBuffer, Ordering> f(final StringBuffer a1) { + return new F<StringBuffer, Ordering>() { + public Ordering f(final StringBuffer a2) { + return stringOrd.compare(a1.toString(), a2.toString()); + } + }; + } + }); - /** - * An order instance for the {@link StringBuffer} type. - */ - public static final Ord<StringBuilder> stringBuilderOrd = - new Ord<StringBuilder>(new F<StringBuilder, F<StringBuilder, Ordering>>() { - public F<StringBuilder, Ordering> f(final StringBuilder a1) { - return new F<StringBuilder, Ordering>() { - public Ordering f(final StringBuilder a2) { - return stringOrd.compare(a1.toString(), a2.toString()); - } - }; - } - }); + /** + * An order instance for the {@link StringBuffer} type. + */ + public static final Ord<StringBuilder> stringBuilderOrd = + new Ord<StringBuilder>(new F<StringBuilder, F<StringBuilder, Ordering>>() { + public F<StringBuilder, Ordering> f(final StringBuilder a1) { + return new F<StringBuilder, Ordering>() { + public Ordering f(final StringBuilder a2) { + return stringOrd.compare(a1.toString(), a2.toString()); + } + }; + } + }); - /** - * An order instance for the {@link Option} type. - * - * @param oa Order across the element of the option. - * @return An order instance for the {@link Option} type. - */ - public static <A> Ord<Option<A>> optionOrd(final Ord<A> oa) { - return new Ord<Option<A>>(new F<Option<A>, F<Option<A>, Ordering>>() { - public F<Option<A>, Ordering> f(final Option<A> o1) { - return new F<Option<A>, Ordering>() { - public Ordering f(final Option<A> o2) { - return o1.isNone() ? - o2.isNone() ? - Ordering.EQ : - Ordering.LT : - o2.isNone() ? - Ordering.GT : - oa.f.f(o1.some()).f(o2.some()); - } - }; - } - }); - } + /** + * An order instance for the {@link Option} type. + * + * @param oa Order across the element of the option. + * @return An order instance for the {@link Option} type. + */ + public static <A> Ord<Option<A>> optionOrd(final Ord<A> oa) { + return new Ord<Option<A>>(new F<Option<A>, F<Option<A>, Ordering>>() { + public F<Option<A>, Ordering> f(final Option<A> o1) { + return new F<Option<A>, Ordering>() { + public Ordering f(final Option<A> o2) { + return o1.isNone() ? + o2.isNone() ? + Ordering.EQ : + Ordering.LT : + o2.isNone() ? + Ordering.GT : + oa.f.f(o1.some()).f(o2.some()); + } + }; + } + }); + } - /** - * An order instance for the {@link Either} type. - * - * @param oa Order across the left side of {@link Either}. - * @param ob Order across the right side of {@link Either}. - * @return An order instance for the {@link Either} type. - */ - public static <A, B> Ord<Either<A, B>> eitherOrd(final Ord<A> oa, final Ord<B> ob) { - return new Ord<Either<A, B>>(new F<Either<A, B>, F<Either<A, B>, Ordering>>() { - public F<Either<A, B>, Ordering> f(final Either<A, B> e1) { - return new F<Either<A, B>, Ordering>() { - public Ordering f(final Either<A, B> e2) { - return e1.isLeft() ? - e2.isLeft() ? - oa.f.f(e1.left().value()).f(e2.left().value()) : - Ordering.LT : - e2.isLeft() ? - Ordering.GT : - ob.f.f(e1.right().value()).f(e2.right().value()); - } - }; - } - }); - } + /** + * An order instance for the {@link Either} type. + * + * @param oa Order across the left side of {@link Either}. + * @param ob Order across the right side of {@link Either}. + * @return An order instance for the {@link Either} type. + */ + public static <A, B> Ord<Either<A, B>> eitherOrd(final Ord<A> oa, final Ord<B> ob) { + return new Ord<Either<A, B>>(new F<Either<A, B>, F<Either<A, B>, Ordering>>() { + public F<Either<A, B>, Ordering> f(final Either<A, B> e1) { + return new F<Either<A, B>, Ordering>() { + public Ordering f(final Either<A, B> e2) { + return e1.isLeft() ? + e2.isLeft() ? + oa.f.f(e1.left().value()).f(e2.left().value()) : + Ordering.LT : + e2.isLeft() ? + Ordering.GT : + ob.f.f(e1.right().value()).f(e2.right().value()); + } + }; + } + }); + } - /** - * An order instance for the {@link Validation} type. - * - * @param oa Order across the failing side of {@link Validation}. - * @param ob Order across the succeeding side of {@link Validation}. - * @return An order instance for the {@link Validation} type. - */ - public static <A, B> Ord<Validation<A, B>> validationOrd(final Ord<A> oa, final Ord<B> ob) { - return eitherOrd(oa, ob).comap(Validation.<A, B>either()); - } + /** + * An order instance for the {@link Validation} type. + * + * @param oa Order across the failing side of {@link Validation}. + * @param ob Order across the succeeding side of {@link Validation}. + * @return An order instance for the {@link Validation} type. + */ + public static <A, B> Ord<Validation<A, B>> validationOrd(final Ord<A> oa, final Ord<B> ob) { + return eitherOrd(oa, ob).comap(Validation.<A, B>either()); + } - /** - * An order instance for the {@link List} type. - * - * @param oa Order across the elements of the list. - * @return An order instance for the {@link List} type. - */ - public static <A> Ord<List<A>> listOrd(final Ord<A> oa) { - return new Ord<List<A>>(new F<List<A>, F<List<A>, Ordering>>() { - public F<List<A>, Ordering> f(final List<A> l1) { - return new F<List<A>, Ordering>() { - public Ordering f(final List<A> l2) { - if (l1.isEmpty()) - return l2.isEmpty() ? Ordering.EQ : Ordering.LT; - else if (l2.isEmpty()) - return l1.isEmpty() ? Ordering.EQ : Ordering.GT; - else { - final Ordering c = oa.compare(l1.head(), l2.head()); - return c == Ordering.EQ ? listOrd(oa).f.f(l1.tail()).f(l2.tail()) : c; + /** + * An order instance for the {@link List} type. + * + * @param oa Order across the elements of the list. + * @return An order instance for the {@link List} type. + */ + public static <A> Ord<List<A>> listOrd(final Ord<A> oa) { + return new Ord<List<A>>(new F<List<A>, F<List<A>, Ordering>>() { + public F<List<A>, Ordering> f(final List<A> l1) { + return new F<List<A>, Ordering>() { + public Ordering f(final List<A> l2) { + if (l1.isEmpty()) + return l2.isEmpty() ? Ordering.EQ : Ordering.LT; + else if (l2.isEmpty()) + return l1.isEmpty() ? Ordering.EQ : Ordering.GT; + else { + final Ordering c = oa.compare(l1.head(), l2.head()); + return c == Ordering.EQ ? listOrd(oa).f.f(l1.tail()).f(l2.tail()) : c; + } + } + }; } - } - }; - } - }); - } + }); + } - /** - * An order instance for the {@link NonEmptyList} type. - * - * @param oa Order across the elements of the non-empty list. - * @return An order instance for the {@link NonEmptyList} type. - */ - public static <A> Ord<NonEmptyList<A>> nonEmptyListOrd(final Ord<A> oa) { - return listOrd(oa).comap(NonEmptyList.<A>toList_()); - } + /** + * An order instance for the {@link NonEmptyList} type. + * + * @param oa Order across the elements of the non-empty list. + * @return An order instance for the {@link NonEmptyList} type. + */ + public static <A> Ord<NonEmptyList<A>> nonEmptyListOrd(final Ord<A> oa) { + return listOrd(oa).comap(NonEmptyList.<A>toList_()); + } - /** - * An order instance for the {@link Stream} type. - * - * @param oa Order across the elements of the stream. - * @return An order instance for the {@link Stream} type. - */ - public static <A> Ord<Stream<A>> streamOrd(final Ord<A> oa) { - return new Ord<Stream<A>>(new F<Stream<A>, F<Stream<A>, Ordering>>() { - public F<Stream<A>, Ordering> f(final Stream<A> s1) { - return new F<Stream<A>, Ordering>() { - public Ordering f(final Stream<A> s2) { - if (s1.isEmpty()) - return s2.isEmpty() ? Ordering.EQ : Ordering.LT; - else if (s2.isEmpty()) - return s1.isEmpty() ? Ordering.EQ : Ordering.GT; - else { - final Ordering c = oa.compare(s1.head(), s2.head()); - return c == Ordering.EQ ? streamOrd(oa).f.f(s1.tail()._1()).f(s2.tail()._1()) : c; + /** + * An order instance for the {@link Stream} type. + * + * @param oa Order across the elements of the stream. + * @return An order instance for the {@link Stream} type. + */ + public static <A> Ord<Stream<A>> streamOrd(final Ord<A> oa) { + return new Ord<Stream<A>>(new F<Stream<A>, F<Stream<A>, Ordering>>() { + public F<Stream<A>, Ordering> f(final Stream<A> s1) { + return new F<Stream<A>, Ordering>() { + public Ordering f(final Stream<A> s2) { + if (s1.isEmpty()) + return s2.isEmpty() ? Ordering.EQ : Ordering.LT; + else if (s2.isEmpty()) + return s1.isEmpty() ? Ordering.EQ : Ordering.GT; + else { + final Ordering c = oa.compare(s1.head(), s2.head()); + return c == Ordering.EQ ? streamOrd(oa).f.f(s1.tail()._1()).f(s2.tail()._1()) : c; + } + } + }; } - } - }; - } - }); - } + }); + } - /** - * An order instance for the {@link Array} type. - * - * @param oa Order across the elements of the array. - * @return An order instance for the {@link Array} type. - */ - public static <A> Ord<Array<A>> arrayOrd(final Ord<A> oa) { - return new Ord<Array<A>>(new F<Array<A>, F<Array<A>, Ordering>>() { - public F<Array<A>, Ordering> f(final Array<A> a1) { - return new F<Array<A>, Ordering>() { - public Ordering f(final Array<A> a2) { - int i = 0; - //noinspection ForLoopWithMissingComponent - for (; i < a1.length() && i < a2.length(); i++) { - final Ordering c = oa.compare(a1.get(i), a2.get(i)); - if (c == Ordering.GT || c == Ordering.LT) - return c; + /** + * An order instance for the {@link Array} type. + * + * @param oa Order across the elements of the array. + * @return An order instance for the {@link Array} type. + */ + public static <A> Ord<Array<A>> arrayOrd(final Ord<A> oa) { + return new Ord<Array<A>>(new F<Array<A>, F<Array<A>, Ordering>>() { + public F<Array<A>, Ordering> f(final Array<A> a1) { + return new F<Array<A>, Ordering>() { + public Ordering f(final Array<A> a2) { + int i = 0; + //noinspection ForLoopWithMissingComponent + for (; i < a1.length() && i < a2.length(); i++) { + final Ordering c = oa.compare(a1.get(i), a2.get(i)); + if (c == Ordering.GT || c == Ordering.LT) + return c; + } + return i == a1.length() ? + i == a2.length() ? + Ordering.EQ : + Ordering.LT : + i == a1.length() ? + Ordering.EQ : + Ordering.GT; + } + }; } - return i == a1.length() ? - i == a2.length() ? - Ordering.EQ : - Ordering.LT : - i == a1.length() ? - Ordering.EQ : - Ordering.GT; - } - }; - } - }); - } + }); + } - /** - * An order instance for the {@link Set} type. - * - * @param oa Order across the elements of the set. - * @return An order instance for the {@link Set} type. - */ - public static <A> Ord<Set<A>> setOrd(final Ord<A> oa) { - return streamOrd(oa).comap(new F<Set<A>, Stream<A>>() { - public Stream<A> f(final Set<A> as) { - return as.toStream(); - } - }); - } + /** + * An order instance for the {@link Set} type. + * + * @param oa Order across the elements of the set. + * @return An order instance for the {@link Set} type. + */ + public static <A> Ord<Set<A>> setOrd(final Ord<A> oa) { + return streamOrd(oa).comap(new F<Set<A>, Stream<A>>() { + public Stream<A> f(final Set<A> as) { + return as.toStream(); + } + }); + } - /** - * An order instance for the {@link Unit} type. - */ - public static final Ord<Unit> unitOrd = ord(curry(new F2<Unit, Unit, Ordering>() { - public Ordering f(final Unit u1, final Unit u2) { - return Ordering.EQ; - } - })); + /** + * An order instance for the {@link Unit} type. + */ + public static final Ord<Unit> unitOrd = ord(curry(new F2<Unit, Unit, Ordering>() { + public Ordering f(final Unit u1, final Unit u2) { + return Ordering.EQ; + } + })); - /** - * An order instance for a product-1. - * - * @param oa Order across the produced type. - * @return An order instance for a product-1. - */ - public static <A> Ord<P1<A>> p1Ord(final Ord<A> oa) { - return oa.comap(P1.<A>__1()); - } + /** + * An order instance for a product-1. + * + * @param oa Order across the produced type. + * @return An order instance for a product-1. + */ + public static <A> Ord<P1<A>> p1Ord(final Ord<A> oa) { + return oa.comap(P1.<A>__1()); + } - /** - * An order instance for a product-2, with the first factor considered most significant. - * - * @param oa An order instance for the first factor. - * @param ob An order instance for the second factor. - * @return An order instance for a product-2, with the first factor considered most significant. - */ - public static <A, B> Ord<P2<A, B>> p2Ord(final Ord<A> oa, final Ord<B> ob) { - return ord(curry(new F2<P2<A, B>, P2<A, B>, Ordering>() { - public Ordering f(final P2<A, B> a, final P2<A, B> b) { - return oa.eq(a._1(), b._1()) ? ob.compare(a._2(), b._2()) : oa.compare(a._1(), b._1()); - } - })); - } + /** + * An order instance for a product-2, with the first factor considered most significant. + * + * @param oa An order instance for the first factor. + * @param ob An order instance for the second factor. + * @return An order instance for a product-2, with the first factor considered most significant. + */ + public static <A, B> Ord<P2<A, B>> p2Ord(final Ord<A> oa, final Ord<B> ob) { + return ord(curry(new F2<P2<A, B>, P2<A, B>, Ordering>() { + public Ordering f(final P2<A, B> a, final P2<A, B> b) { + return oa.eq(a._1(), b._1()) ? ob.compare(a._2(), b._2()) : oa.compare(a._1(), b._1()); + } + })); + } - /** - * An order instance for a product-3, with the first factor considered most significant. - * - * @param oa An order instance for the first factor. - * @param ob An order instance for the second factor. - * @param oc An order instance for the third factor. - * @return An order instance for a product-3, with the first factor considered most significant. - */ - public static <A, B, C> Ord<P3<A, B, C>> p3Ord(final Ord<A> oa, final Ord<B> ob, final Ord<C> oc) { - return ord(curry(new F2<P3<A, B, C>, P3<A, B, C>, Ordering>() { - public Ordering f(final P3<A, B, C> a, final P3<A, B, C> b) { - return oa.eq(a._1(), b._1()) ? - p2Ord(ob, oc).compare(P.p(a._2(), a._3()), P.p(b._2(), b._3())) - : oa.compare(a._1(), b._1()); - } - })); - } + /** + * An order instance for a product-3, with the first factor considered most significant. + * + * @param oa An order instance for the first factor. + * @param ob An order instance for the second factor. + * @param oc An order instance for the third factor. + * @return An order instance for a product-3, with the first factor considered most significant. + */ + public static <A, B, C> Ord<P3<A, B, C>> p3Ord(final Ord<A> oa, final Ord<B> ob, final Ord<C> oc) { + return ord(curry(new F2<P3<A, B, C>, P3<A, B, C>, Ordering>() { + public Ordering f(final P3<A, B, C> a, final P3<A, B, C> b) { + return oa.eq(a._1(), b._1()) ? + p2Ord(ob, oc).compare(P.p(a._2(), a._3()), P.p(b._2(), b._3())) + : oa.compare(a._1(), b._1()); + } + })); + } - /** - * An order instance for the <code>Natural</code> type. - */ - public static final Ord<Natural> naturalOrd = bigintOrd.comap(Natural.bigIntegerValue); + /** + * An order instance for the <code>Natural</code> type. + */ + public static final Ord<Natural> naturalOrd = bigintOrd.comap(Natural.bigIntegerValue); - /** - * An order instance for the <code>Comparable</code> interface. - * - * @return An order instance for the <code>Comparable</code> interface. - */ - public static <A extends Comparable<A>> Ord<A> comparableOrd() { - return ord(new F<A, F<A, Ordering>>() { - public F<A, Ordering> f(final A a1) { - return new F<A, Ordering>() { - public Ordering f(final A a2) { - return Ordering.fromInt(a1.compareTo(a2)); - } - }; - } - }); - } + /** + * An order instance for the <code>Comparable</code> interface. + * + * @return An order instance for the <code>Comparable</code> interface. + */ + public static <A extends Comparable<A>> Ord<A> comparableOrd() { + return ord(new F<A, F<A, Ordering>>() { + public F<A, Ordering> f(final A a1) { + return new F<A, Ordering>() { + public Ordering f(final A a2) { + return Ordering.fromInt(a1.compareTo(a2)); + } + }; + } + }); + } - /** - * An order instance that uses {@link Object#hashCode()} for computing the order and equality, - * thus objects returning the same hashCode are considered to be equals (check {@link #hashEqualsOrd()} - * for an additional check on {@link Object#equals(Object)}). - * - * @return An order instance that is based on {@link Object#hashCode()}. - * @see #hashEqualsOrd() - */ - public static <A> Ord<A> hashOrd() { - return Ord.<A> ord(new F<A, F<A, Ordering>>() { - @Override - public F<A, Ordering> f(final A a) { - return new F<A, Ordering>() { - @Override - public Ordering f(final A a2) { - final int x = a.hashCode() - a2.hashCode(); - return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; - } - }; - } - }); - } + /** + * An order instance that uses {@link Object#hashCode()} for computing the order and equality, + * thus objects returning the same hashCode are considered to be equals (check {@link #hashEqualsOrd()} + * for an additional check on {@link Object#equals(Object)}). + * + * @return An order instance that is based on {@link Object#hashCode()}. + * @see #hashEqualsOrd() + */ + public static <A> Ord<A> hashOrd() { + return Ord.<A>ord(new F<A, F<A, Ordering>>() { + @Override + public F<A, Ordering> f(final A a) { + return new F<A, Ordering>() { + @Override + public Ordering f(final A a2) { + final int x = a.hashCode() - a2.hashCode(); + return x < 0 ? Ordering.LT : x == 0 ? Ordering.EQ : Ordering.GT; + } + }; + } + }); + } - /** - * An order instance that uses {@link Object#hashCode()} and {@link Object#equals} for computing - * the order and equality. First the hashCode is compared, if this is equal, objects are compared - * using {@link Object#equals}. - * - * @return An order instance that is based on {@link Object#hashCode()} and {@link Object#equals}. - */ - public static <A> Ord<A> hashEqualsOrd() { - return Ord.<A> ord(new F<A, F<A, Ordering>>() { - @Override - public F<A, Ordering> f(final A a) { - return new F<A, Ordering>() { - @Override - public Ordering f(final A a2) { - final int x = a.hashCode() - a2.hashCode(); - return x < 0 ? Ordering.LT : x == 0 && a.equals(a2) ? Ordering.EQ : Ordering.GT; - } - }; - } - }); - } + /** + * An order instance that uses {@link Object#hashCode()} and {@link Object#equals} for computing + * the order and equality. First the hashCode is compared, if this is equal, objects are compared + * using {@link Object#equals}. + * + * @return An order instance that is based on {@link Object#hashCode()} and {@link Object#equals}. + */ + public static <A> Ord<A> hashEqualsOrd() { + return Ord.<A>ord(new F<A, F<A, Ordering>>() { + @Override + public F<A, Ordering> f(final A a) { + return new F<A, Ordering>() { + @Override + public Ordering f(final A a2) { + final int x = a.hashCode() - a2.hashCode(); + return x < 0 ? Ordering.LT : x == 0 && a.equals(a2) ? Ordering.EQ : Ordering.GT; + } + }; + } + }); + } }
--- a/src/main/java/fj/P.java Fri Mar 20 21:04:03 2015 +0900 +++ b/src/main/java/fj/P.java Sat Mar 21 05:32:16 2015 +0900 @@ -1,5 +1,7 @@ package fj; +import fj.data.Option; + import static fj.Unit.unit; /** @@ -123,7 +125,7 @@ }; } - public static <A, B, C, D, E, F> P6<A, B, C, D, E, F> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, P1<E> pe, P1<F> pf) { + public static <A, B, C, D, E, F> P6<A, B, C, D, E, F> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, final P1<E> pe, final P1<F> pf) { return new P6<A, B, C, D, E, F>() { @Override public A _1() { @@ -155,7 +157,7 @@ }; } - public static <A, B, C, D, E, F, G> P7<A, B, C, D, E, F, G> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, P1<E> pe, P1<F> pf, P1<G> pg) { + public static <A, B, C, D, E, F, G> P7<A, B, C, D, E, F, G> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, final P1<E> pe, final P1<F> pf, final P1<G> pg) { return new P7<A, B, C, D, E, F, G>() { @Override public A _1() { @@ -192,7 +194,7 @@ }; } - public static <A, B, C, D, E, F, G, H> P8<A, B, C, D, E, F, G, H> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, P1<E> pe, P1<F> pf, P1<G> pg, P1<H> ph) { + public static <A, B, C, D, E, F, G, H> P8<A, B, C, D, E, F, G, H> lazy(final P1<A> pa, final P1<B> pb, final P1<C> pc, final P1<D> pd, final P1<E> pe, final P1<F> pf, final P1<G> pg, final P1<H> ph) { return new P8<A, B, C, D, E, F, G, H>() { @Override public A _1() { @@ -736,7 +738,7 @@ }; } - public static <A, B, C, D, E> P5<A, B, C, D, E> lazy(F<Unit, A> fa, F<Unit, B> fb, F<Unit, C> fc, F<Unit, D> fd, F<Unit, E> fe) { + public static <A, B, C, D, E> P5<A, B, C, D, E> lazy(final F<Unit, A> fa, final F<Unit, B> fb, final F<Unit, C> fc, final F<Unit, D> fd, final F<Unit, E> fe) { return new P5<A, B, C, D, E>() { @Override public A _1() {
--- a/src/main/java/fj/data/List.java Fri Mar 20 21:04:03 2015 +0900 +++ b/src/main/java/fj/data/List.java Sat Mar 21 05:32:16 2015 +0900 @@ -1329,14 +1329,15 @@ final D groupingIdentity, final F2<C, D, D> groupingAcc, final Ord<B> keyOrd) { - return this.foldLeft(map -> element -> { - final B key = keyFunction.f(element); - final C value = valueFunction.f(element); - return map.set(key, map.get(key) - .map(existing -> groupingAcc.f(value, existing)) - .orSome(groupingAcc.f(value, groupingIdentity))); - }, TreeMap.<B, D>empty(keyOrd) - ); + return null; + // this.foldLeft(map -> element -> { + // final B key = keyFunction.f(element); + // final C value = valueFunction.f(element); + // return map.set(key, map.get(key) + // .map(existing -> groupingAcc.f(value, existing)) + // .orSome(groupingAcc.f(value, groupingIdentity))); + // }, TreeMap.<B, D>empty(keyOrd) + // ); }
--- a/src/main/java/fj/data/Set.java Fri Mar 20 21:04:03 2015 +0900 +++ b/src/main/java/fj/data/Set.java Sat Mar 21 05:32:16 2015 +0900 @@ -36,7 +36,6 @@ public final boolean isEmpty() { return this instanceof Empty; } - @SuppressWarnings({ "ClassEscapesDefinedScope" }) abstract Color color();
--- a/src/main/java/fj/data/TreeMap.java Fri Mar 20 21:04:03 2015 +0900 +++ b/src/main/java/fj/data/TreeMap.java Sat Mar 21 05:32:16 2015 +0900 @@ -1,12 +1,6 @@ package fj.data; -import fj.F; -import fj.F1Functions; -import fj.Ordering; -import fj.P; -import fj.P2; -import fj.P3; -import fj.Ord; +import fj.*; import java.util.Iterator; import java.util.Map; @@ -21,272 +15,273 @@ * An immutable, in-memory map, backed by a red-black tree. */ public final class TreeMap<K, V> implements Iterable<P2<K, V>> { - private final Set<P2<K, Option<V>>> tree; + private final Set<P2<K, V>> tree; - private TreeMap(final Set<P2<K, Option<V>>> tree) { - this.tree = tree; - } + private TreeMap(final Set<P2<K, V>> tree) { + this.tree = tree; + } - private static <K, V> Ord<P2<K, V>> ord(final Ord<K> keyOrd) { - return keyOrd.comap(P2.<K, V> __1()); - } + private static <K, V> Ord<P2<K, V>> ord(final Ord<K> keyOrd) { + return keyOrd.comap(P2.<K, V>__1()); + } - /** - * Constructs an empty tree map. - * - * @param keyOrd - * An order for the keys of the tree map. - * @return an empty TreeMap with the given key order. - */ - public static <K, V> TreeMap<K, V> empty(final Ord<K> keyOrd) { - return new TreeMap<K, V>(Set.empty(TreeMap.<K, Option<V>> ord(keyOrd))); - } + /** + * Constructs an empty tree map. + * + * @param keyOrd An order for the keys of the tree map. + * @return an empty TreeMap with the given key order. + */ + public static <K, V> TreeMap<K, V> empty(final Ord<K> keyOrd) { + return new TreeMap<K, V>(Set.empty(TreeMap.<K, V>ord(keyOrd))); + } - /** - * Returns a potential value that the given key maps to. - * - * @param k - * The key to look up in the tree map. - * @return A potential value for the given key. - */ + /** + * Returns a potential value that the given key maps to. + * + * @param k The key to look up in the tree map. + * @return A potential value for the given key. + */ public Option<V> get(final K k) { - Option<V> op = Option.<V> none(); - Option<P2<K, Option<V>>> attribute = tree.mapGet(P.p(k, op)); - return attribute.bind(P2.<K, Option<V>> __2()); - // P3<Set<P2<K, Option<V>>>, Option<P2<K, Option<V>>>, Set<P2<K, - // Option<V>>>> splitTree = tree.split(P.p(k, op)); - // final Option<P2<K, Option<V>>> x = splitTree._2(); - // - // System.out.println("aaaa"); - // return x.bind(P2.<K, Option<V>>__2()); +// Option<V> op = Option.<V> none(); +// Option<P2<K, Option<V>>> attribute = tree.mapGet(P.p(k, op)); +// return attribute.bind(P2.<K, Option<V>> __2()); +// // P3<Set<P2<K, Option<V>>>, Option<P2<K, Option<V>>>, Set<P2<K, +// // Option<V>>>> splitTree = tree.split(P.p(k, op)); +// // final Option<P2<K, Option<V>>> x = splitTree._2(); +// // +// // System.out.println("aaaa"); +// // return x.bind(P2.<K, Option<V>>__2()); + return null; } + public Option<V> getLoop(final K k) { + Set<P2<K, V>> cur = tree; + Option<V> op = Option.<V>none(); - public Option<V> getLoop(final K k) { - Set<P2<K, Option<V>>> cur = tree; - Option<V> op = Option.<V> none(); - - while (!cur.isEmpty()) { - P2<K, Option<V>> h = cur.head(); - Ordering i = cur.ord.compare(P.p(k,op),h); - if (i == Ordering.LT) - cur = cur.l(); - else if (i == Ordering.GT) - cur = cur.r(); - else - return h._2(); - + while (!cur.isEmpty()) { + Ord<P2<K, V>> ttt = cur.ord(); +// P2<K, V> head = cur.head(); + K h = cur.head()._1_()._1(); + int i = h.hashCode() - k.hashCode(); + // Ord<P2<K, V>> ord = cur.ord(); + // Ordering i = ord.compare(P.p(k,null), cur.head()); + if (i > 0) + cur = cur.l(); + else if (i < 0) + cur = cur.r(); + else + return Option.<V>some(cur.head()._2()); + + + } + return Option.<V>none(); } - return Option.<V>none(); - } - /** - * Inserts the given key and value association into the tree map. If the given - * key is already mapped to a value, the old value is replaced with the given - * one. - * - * @param k - * The key to insert. - * @param v - * The value to insert. - * @return A new tree map with the given value mapped to the given key. - */ - public TreeMap<K, V> set(final K k, final V v) { - return new TreeMap<K, V>(tree.insert(P.p(k, Option.some(v)))); - } + /** + * Inserts the given key and value association into the tree map. If the given + * key is already mapped to a value, the old value is replaced with the given + * one. + * + * @param k The key to insert. + * @param v The value to insert. + * @return A new tree map with the given value mapped to the given key. + */ + public TreeMap<K, V> set(final K k, final V v) { + return new TreeMap<K, V>(tree.insert(P.p(k, v))); + } - /** - * Deletes the entry in the tree map that corresponds to the given key. - * - * @param k - * The key to delete from this tree map. - * @return A new tree map with the entry corresponding to the given key - * removed. - */ - public TreeMap<K, V> delete(final K k) { - return new TreeMap<K, V>(tree.delete(P.p(k, Option.<V> none()))); - } - - /** - * Returns the number of entries in this tree map. - * - * @return The number of entries in this tree map. - */ - public int size() { - return tree.size(); - } + /** + * Deletes the entry in the tree map that corresponds to the given key. + * + * @param k The key to delete from this tree map. + * @return A new tree map with the entry corresponding to the given key + * removed. + */ + public TreeMap<K, V> delete(final K k) { + Option<V> op = Option.<V>none(); + P2<K, V> p = P.p(k, null); + return new TreeMap<K, V>(tree.delete(p)); + } - /** - * Determines if this tree map has any entries. - * - * @return <code>true</code> if this tree map has no entries, - * <code>false</code> otherwise. - */ - public boolean isEmpty() { - return tree.isEmpty(); - } - - /** - * Returns all values in this tree map. - * - * @return All values in this tree map. - */ - public List<V> values() { - return iterableList(join(tree.toList().map(compose(IterableW.<V, Option<V>> wrap(), P2.<K, Option<V>> __2())))); - } + /** + * Returns the number of entries in this tree map. + * + * @return The number of entries in this tree map. + */ + public int size() { + return tree.size(); + } - /** - * Returns all keys in this tree map. - * - * @return All keys in this tree map. - */ - public List<K> keys() { - return tree.toList().map(P2.<K, Option<V>> __1()); - } + /** + * Determines if this tree map has any entries. + * + * @return <code>true</code> if this tree map has no entries, + * <code>false</code> otherwise. + */ + public boolean isEmpty() { + return tree.isEmpty(); + } - /** - * Determines if the given key value exists in this tree map. - * - * @param k - * The key value to look for in this tree map. - * @return <code>true</code> if this tree map contains the given key, - * <code>false</code> otherwise. - */ - public boolean contains(final K k) { - return tree.member(P.p(k, Option.<V> none())); - } + /** + * Returns all values in this tree map. + * + * @return All values in this tree map. + */ +// public List<V> values() { +// return iterableList(join(tree.toList().map(compose(IterableW.<V, Option<V>> wrap(), P2.<K, Option<V>> __2())))); +// } + + /** + * Returns all keys in this tree map. + * + * @return All keys in this tree map. + */ + public List<K> keys() { + return tree.toList().map(P2.<K, V>__1()); + } - /** - * Returns an iterator for this map's key-value pairs. This method exists to - * permit the use in a <code>for</code>-each loop. - * - * @return A iterator for this map's key-value pairs. - */ - public Iterator<P2<K, V>> iterator() { - return join( - tree.toStream().map(P2.<K, Option<V>, IterableW<V>> map2_(IterableW.<V, Option<V>> wrap())) - .map(P2.tuple(compose(IterableW.<V, P2<K, V>> map(), P.<K, V> p2())))).iterator(); - } + /** + * Determines if the given key value exists in this tree map. + * + * @param k + * The key value to look for in this tree map. + * @return <code>true</code> if this tree map contains the given key, + * <code>false</code> otherwise. + */ +// public boolean contains(final K k) { +// return tree.member(P.p(k, Option.<V> none())); +// } - /** - * A mutable map projection of this tree map. - * - * @return A new mutable map isomorphic to this tree map. - */ - public Map<K, V> toMutableMap() { - final Map<K, V> m = new java.util.TreeMap<K, V>(); - for (final P2<K, V> e : this) { - m.put(e._1(), e._2()); + /** + * Returns an iterator for this map's key-value pairs. This method exists to + * permit the use in a <code>for</code>-each loop. + * + * @return A iterator for this map's key-value pairs. + */ + public Iterator<P2<K, V>> iterator() { + return null; +// join( +// tree.toStream().map(P2.<K, Option<V>, IterableW<V>> map2_(IterableW.<V, Option<V>> wrap())) +// .map(P2.tuple(compose(IterableW.<V, P2<K, V>> map(), P.<K, V> p2())))).iterator(); } - return m; - } - /** - * An immutable projection of the given mutable map. - * - * @param ord - * An order for the map's keys. - * @param m - * A mutable map to project to an immutable one. - * @return A new immutable tree map isomorphic to the given mutable map. - */ - public static <K, V> TreeMap<K, V> fromMutableMap(final Ord<K> ord, final Map<K, V> m) { - TreeMap<K, V> t = empty(ord); - for (final Map.Entry<K, V> e : m.entrySet()) { - t = t.set(e.getKey(), e.getValue()); + /** + * A mutable map projection of this tree map. + * + * @return A new mutable map isomorphic to this tree map. + */ + public Map<K, V> toMutableMap() { + final Map<K, V> m = new java.util.TreeMap<K, V>(); + for (final P2<K, V> e : this) { + m.put(e._1(), e._2()); + } + return m; } - return t; - } - /** - * Returns a first-class version of the get method for this TreeMap. - * - * @return a functional representation of this TreeMap. - */ - public F<K, Option<V>> get() { - return new F<K, Option<V>>() { - public Option<V> f(final K k) { - return get(k); - } - }; - } + /** + * An immutable projection of the given mutable map. + * + * @param ord An order for the map's keys. + * @param m A mutable map to project to an immutable one. + * @return A new immutable tree map isomorphic to the given mutable map. + */ + public static <K, V> TreeMap<K, V> fromMutableMap(final Ord<K> ord, final Map<K, V> m) { + TreeMap<K, V> t = empty(ord); + for (final Map.Entry<K, V> e : m.entrySet()) { + t = t.set(e.getKey(), e.getValue()); + } + return t; + } + + /** + * Returns a first-class version of the get method for this TreeMap. + * + * @return a functional representation of this TreeMap. + */ + public F<K, Option<V>> get() { + return new F<K, Option<V>>() { + public Option<V> f(final K k) { + return getLoop(k); + } + }; + } - /** - * Modifies the value for the given key, if present, by applying the given - * function to it. - * - * @param k - * The key for the value to modify. - * @param f - * A function with which to modify the value. - * @return A new tree map with the value for the given key transformed by the - * given function, paired with True if the map was modified, otherwise - * False. - */ - public P2<Boolean, TreeMap<K, V>> update(final K k, final F<V, V> f) { - final P2<Boolean, Set<P2<K, Option<V>>>> up = tree.update(p(k, Option.<V> none()), - P2.<K, Option<V>, Option<V>> map2_(Option.<V, V> map().f(f))); - return P.p(up._1(), new TreeMap<K, V>(up._2())); - } + /** + * Modifies the value for the given key, if present, by applying the given + * function to it. + * + * @param k + * The key for the value to modify. + * @param f + * A function with which to modify the value. + * @return A new tree map with the value for the given key transformed by the + * given function, paired with True if the map was modified, otherwise + * False. + */ +// public P2<Boolean, TreeMap<K, V>> update(final K k, final F<V, V> f) { +// final P2<Boolean, Set<P2<K, Option<V>>>> up = tree.update(p(k, Option.<V> none()), +// P2.<K, Option<V>, Option<V>> map2_(Option.<V, V> map().f(f))); +// return P.p(up._1(), new TreeMap<K, V>(up._2())); +// } - /** - * Modifies the value for the given key, if present, by applying the given - * function to it, or inserts the given value if the key is not present. - * - * @param k - * The key for the value to modify. - * @param f - * A function with which to modify the value. - * @param v - * A value to associate with the given key if the key is not already - * present. - * @return A new tree map with the value for the given key transformed by the - * given function. - */ - public TreeMap<K, V> update(final K k, final F<V, V> f, final V v) { - final P2<Boolean, TreeMap<K, V>> up = update(k, f); - return up._1() ? up._2() : set(k, v); - } + /** + * Modifies the value for the given key, if present, by applying the given + * function to it, or inserts the given value if the key is not present. + * + * @param k + * The key for the value to modify. + * @param f + * A function with which to modify the value. + * @param v + * A value to associate with the given key if the key is not already + * present. + * @return A new tree map with the value for the given key transformed by the + * given function. + */ +// public TreeMap<K, V> update(final K k, final F<V, V> f, final V v) { +// final P2<Boolean, TreeMap<K, V>> up = update(k, f); +// return up._1() ? up._2() : set(k, v); +// } - /** - * Splits this TreeMap at the given key. Returns a triple of: - * <ul> - * <li>A set containing all the values of this map associated with keys less - * than the given key.</li> - * <li>An option of a value mapped to the given key, if it exists in this map, - * otherwise None. - * <li>A set containing all the values of this map associated with keys - * greater than the given key.</li> - * </ul> - * - * @param k - * A key at which to split this map. - * @return Two sets and an optional value, where all elements in the first set - * are mapped to keys less than the given key in this map, all the - * elements in the second set are mapped to keys greater than the - * given key, and the optional value is the value associated with the - * given key if present, otherwise None. - */ - public P3<Set<V>, Option<V>, Set<V>> split(final K k) { - final F<Set<P2<K, Option<V>>>, Set<V>> getSome = F1Functions.mapSet( - F1Functions.o(Option.<V> fromSome(), P2.<K, Option<V>> __2()), - tree.ord().comap(F1Functions.o(P.<K, Option<V>> p2().f(k), Option.<V> some_()))); - return tree.split(p(k, Option.<V> none())).map1(getSome).map3(getSome) - .map2(F1Functions.o(Option.<V> join(), F1Functions.mapOption(P2.<K, Option<V>> __2()))); - } + /** + * Splits this TreeMap at the given key. Returns a triple of: + * <ul> + * <li>A set containing all the values of this map associated with keys less + * than the given key.</li> + * <li>An option of a value mapped to the given key, if it exists in this map, + * otherwise None. + * <li>A set containing all the values of this map associated with keys + * greater than the given key.</li> + * </ul> + * + * @param k + * A key at which to split this map. + * @return Two sets and an optional value, where all elements in the first set + * are mapped to keys less than the given key in this map, all the + * elements in the second set are mapped to keys greater than the + * given key, and the optional value is the value associated with the + * given key if present, otherwise None. + */ +// public P3<Set<V>, Option<V>, Set<V>> split(final K k) { +// final F<Set<P2<K, Option<V>>>, Set<V>> getSome = F1Functions.mapSet( +// F1Functions.o(Option.<V> fromSome(), P2.<K, Option<V>> __2()), +// tree.ord().comap(F1Functions.o(P.<K, Option<V>> p2().f(k), Option.<V> some_()))); +// return tree.split(p(k, Option.<V> none())).map1(getSome).map3(getSome) +// .map2(F1Functions.o(Option.<V> join(), F1Functions.mapOption(P2.<K, Option<V>> __2()))); +// } - /** - * Maps the given function across the values of this TreeMap. - * - * @param f - * A function to apply to the values of this TreeMap. - * @return A new TreeMap with the values transformed by the given function. - */ - @SuppressWarnings({ "unchecked" }) - public <W> TreeMap<K, W> map(final F<V, W> f) { - final F<P2<K, Option<V>>, P2<K, Option<W>>> g = P2.map2_(F1Functions.mapOption(f)); - final F<K, P2<K, Option<V>>> coord = flip(P.<K, Option<V>> p2()).f(Option.<V> none()); - final Ord<K> o = tree.ord().comap(coord); - return new TreeMap<K, W>(tree.map(TreeMap.<K, Option<W>> ord(o), g)); - } + /** + * Maps the given function across the values of this TreeMap. + * + * @param f + * A function to apply to the values of this TreeMap. + * @return A new TreeMap with the values transformed by the given function. + */ +// @SuppressWarnings({ "unchecked" }) +// public <W> TreeMap<K, W> map(final F<V, W> f) { +// final F<P2<K, Option<V>>, P2<K, Option<W>>> g = P2.map2_(F1Functions.mapOption(f)); +// final F<K, P2<K, Option<V>>> coord = flip(P.<K, Option<V>> p2()).f(Option.<V> none()); +// final Ord<K> o = tree.ord().comap(coord); +// return new TreeMap<K, W>(tree.map(TreeMap.<K, Option<W>> ord(o), g)); +// } }