Skip to content

Instantly share code, notes, and snippets.

View msfroh's full-sized avatar

Michael Froh msfroh

  • Amazon Web Services
  • Seattle, WA
View GitHub Profile
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;
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;
}
@msfroh
msfroh / Curry_Function2.java
Created December 9, 2011 15:17
Functional Programming in Java Part 1
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
@msfroh
msfroh / Currying_Example.java
Created December 12, 2011 23:25
Functional Programming in Java Part 1
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));
@msfroh
msfroh / Function0.java
Created March 19, 2012 21:50
Functional Programming in Java Part 3
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
@msfroh
msfroh / Curryable.java
Created March 28, 2012 21:10
Functional Programming in Java Parts 2 and 3
public abstract class Curryable<T, F> {
public abstract Function1<F, T> curry();
public F apply(T t) {
return curry().apply(t);
}
}
@msfroh
msfroh / ImmutableList.java
Created April 13, 2012 23:41
Immutable Lists
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);
}
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;
@msfroh
msfroh / AugmentedIterable.java
Created April 18, 2012 22:02
Higher-order collection methods
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.
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;