This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ImmutableBinaryTree<T extends Comparable<T>> implements ImmutableSet<T> { | |
private final ImmutableBinaryTree<T> left; | |
private final ImmutableBinaryTree<T> right; | |
private final T value; | |
public ImmutableBinaryTree(ImmutableBinaryTree<T> left, | |
ImmutableBinaryTree<T> right, T value) { | |
this.left = left; | |
this.right = right; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final class DualBranch<T extends Comparable<T>> extends NonEmptyTree<T> { | |
private final ImmutableBinaryTree<T> leftChild; | |
private final ImmutableBinaryTree<T> rightChild; | |
public DualBranch(final T value, final ImmutableBinaryTree<T> left, final ImmutableBinaryTree<T> right) { | |
super(value); | |
leftChild = left; | |
rightChild = right; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Function2<R, T1, T2> { | |
/* ... Previous declaration of apply ... */ | |
public final Function1<Function1<R, T2>, T1> curry() { | |
return new Function1<Function1<R, T2>, T1>() { // The Function1 that we're returning | |
public Function1<R, T2> apply(final T1 i1) { // Capture first parameter | |
return new Function1<R, T2>() { // The function of n-1 parameters | |
@Override | |
public R apply(final T2 i2) { // Take second parameter | |
// Return result from original function |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final Function2<Integer, Integer, Integer> add = | |
new Function2<Integer, Integer, Integer>() { | |
@Override | |
public Integer apply(Integer i1, Integer i2) { | |
return i1 + i2; | |
} | |
}; | |
// The next two lines output the same thing | |
System.out.println("4 + 5 = " + add.apply(4, 5)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package functions; | |
public abstract class Function0<R> { | |
private R value = null; | |
/** | |
* The get() method should be synchronized, to ensure that we don't call | |
* evaluate unnecessarily in a multithreaded setting. | |
* | |
* @return the result of evaluating -- results will be cached |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Curryable<T, F> { | |
public abstract Function1<F, T> curry(); | |
public F apply(T t) { | |
return curry().apply(t); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class ImmutableList<T> implements Iterable<T> { | |
public abstract T head(); | |
public abstract ImmutableList<T> tail(); | |
public abstract boolean isEmpty(); | |
public ImmutableList<T> prepend(T element) { | |
return new NonEmptyList<T>(element, this); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Option<T> { | |
public abstract T get(); | |
public abstract boolean isDefined(); | |
// Factory method to return the singleton None instance | |
@SuppressWarnings({"unchecked"}) | |
public static <T> Option<T> none() { | |
return NONE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface AugmentedIterable<T> extends Iterable<T> { | |
// Apply function f to each element in the collection (in order), collecting | |
// the results in a new collection, which is returned. | |
<R> AugmentedIterable<R> map(Function1<R, T> f); | |
// Apply function f to the seed value and the first element in the | |
// collection, then apply f to the result and the second element in | |
// the collection, then apply f to that result and the third element in | |
// the collection, etc. returning the final computed result. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LoggingFunction1<R, T1> extends Function1<R, T1>{ | |
private final Function1<R, T1> wrappedFunction; | |
private final PrintStream logStream; | |
private final String name; | |
private LoggingFunction1(final Function1<R, T1> wrappedFunction, | |
final PrintStream logStream, final String name) { | |
this.wrappedFunction = wrappedFunction; | |
this.logStream = logStream; | |
this.name = name; |
OlderNewer