Created
July 27, 2013 09:40
-
-
Save ankurpshah/6094404 to your computer and use it in GitHub Desktop.
Combinator Functions
This file contains hidden or 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 datastructures2; | |
... | |
public class ListModule { | |
public static interface List<T> { | |
... | |
public | |
List<T> filter(Function1<T,Boolean> f); | |
public <T2> List<T2> map(Function1<T,T2> f); | |
public <T2> T2 foldLeft (T2 seed, Function2<T2,T,T2> f); | |
public <T2> T2 foldRight (T2 seed, Function2<T,T2,T2> f); | |
public void foreach(Function1Void<T> f); | |
} | |
public static final class NonEmptyList<T> implements List<T> { | |
... | |
public List<T> filter (Function1<T,Boolean> f) { | |
if (f.apply(head())) { | |
return list(head(), tail().filter(f)); | |
} else { | |
return tail().filter(f); | |
} | |
} | |
public <T2> List<T2> map (Function1<T,T2> f) { | |
return list(f.apply(head()), tail().map(f)); | |
} | |
public <T2> T2 foldLeft (T2 seed, Function2<T2,T,T2> f) { | |
return tail().foldLeft(f.apply(seed, head()), f); | |
} | |
public <T2> T2 foldRight (T2 seed, Function2<T,T2,T2> f) { | |
return f.apply(head(), tail().foldRight(seed, f)); | |
} | |
} | |
public void foreach (Function1Void<T> f) { | |
f.apply(head()); | |
tail().foreach(f); | |
} | |
public static final List<? extends Object> EMPTY = new List<Object>() { | |
... | |
public | |
List<Object> filter (Function1<Object,Boolean> f) { return this; } | |
public <T2> List<T2> map (Function1<Object,T2> f) { return emptyList(); } | |
public <T2> T2 foldLeft (T2 seed, Function2<T2,Object,T2> f) { return seed; } | |
public <T2> T2 foldRight (T2 seed, Function2<Object,T2,T2> f) { return seed; } | |
public void foreach (Function1Void<Object> f) {} | |
}; | |
} |
This file contains hidden or 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
List<Integer> listI =list(1, list(2, list(3, list(4, emptyList())))); | |
listI.foldLeft(0, new Function2<Integer, Integer, Integer>() { | |
public Integer apply(Integer seed, Integer item) { return seed + item; } | |
}); | |
//((((0 + 1) + 2) + 3) + 4) == 10 |
This file contains hidden or 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
List<Integer> listI =list(1, list(2, list(3, list(4, emptyList())))); | |
listI.foldRight(0, new Function2<Integer, Integer, Integer>() { | |
public Integer apply(Integer item, Integer seed) { return item + seed; } | |
}); | |
//(1 + (2 + (3 + (4 + 0)))) == 10 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment