public
Last active

Nice post titled "List Out of Lambda" [1] written by Steve Losh, implemented here in Java 8. Building blocks of lists: 1) Functions (lambdas) 2) `true` and `false` for empty lists .... [1] http://stevelosh.com/blog/2013/03/list-out-of-lambda/

  • Download Gist
ListOutOfLambdas.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
package java8.func.abstraction;
/**
* Nice post titled "List Out of Lambda" [1] written by Steve Losh,
* implemented here in Java 8.
*
* Building blocks of lists:
* * Functions (lambdas)
* * `true` and `false` for empty lists
*
* [1] http://stevelosh.com/blog/2013/03/list-out-of-lambda/
*/
 
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.function.Predicate;
 
public class ListOutOfLambdas {
 
@FunctionalInterface
interface SelectorFunc<T> {
T apply(Object h, ListFunc t, Boolean e);
}
 
@FunctionalInterface
interface ListFunc<T> {
T apply(SelectorFunc selector);
}
 
// Function signatures
static ListFunc emptyList;
static BiFunction<Object,ListFunc,ListFunc> prepend;
static Function<ListFunc,Object> head;
static Function<ListFunc<ListFunc>,ListFunc> tail;
static Predicate<ListFunc<Boolean>> isEmpty;
static Function<Boolean,Boolean> not;
static BiFunction<Boolean,Boolean,Boolean> and;
static BiFunction<Boolean,Boolean,Boolean> or;
static BiFunction<Function<Object,Object>, ListFunc, ListFunc<ListFunc>> map;
static BiFunction<Predicate<Object>, ListFunc, ListFunc<ListFunc>> filter;
static ListFunc zero;
static Function<ListFunc,ListFunc> inc;
static Function<ListFunc,ListFunc> dec;
static ListFunc one;
static Predicate<ListFunc> isZero;
static BiFunction<ListFunc,ListFunc,ListFunc> add;
static BiFunction<ListFunc,ListFunc,ListFunc> sub;
static BiFunction<ListFunc,ListFunc,ListFunc> mul;
static BiFunction<ListFunc,ListFunc,ListFunc> div;
static BiFunction<ListFunc,ListFunc,ListFunc> pow;
static BiFunction<ListFunc,ListFunc,ListFunc> rem;
static BiFunction<ListFunc,ListFunc,Boolean> isEqual;
static BiFunction<ListFunc,ListFunc,Boolean> lessThan;
static BiFunction<ListFunc,ListFunc,Boolean> greaterThan;
static BiFunction<ListFunc,ListFunc,Object> nth;
static BiFunction<ListFunc,ListFunc,ListFunc> drop;
static BiFunction<ListFunc,ListFunc,ListFunc> take;
static Function<ListFunc,Function<ListFunc,Function<ListFunc,ListFunc>>> slice;
static Function<ListFunc,ListFunc> length;
 
static {
// -- List constructs
emptyList = selector -> selector.apply(null, null, true);
 
prepend = (el, list) ->
selector -> selector.apply(el, list, false);
 
head = list -> list.apply((h, t, e) -> h);
 
tail = list -> list.apply((h, t, e) -> t);
 
isEmpty = list -> list.apply((h, t, e) -> e);
// --
 
// -- Boolean constructs
not = x -> !x;
and = (a, b) -> a ? b : false;
or = (a, b) -> a ? true : b;
// --
 
// -- Some common list functions
map = (fn, l) -> {
if (isEmpty.test(l)) {
return emptyList;
} else {
return prepend.apply(fn.apply(head.apply(l)),
map.apply(fn, tail.apply(l)));
}
};
 
filter = (fn, l) -> {
if (isEmpty.test(l)) {
return emptyList;
} else if (fn.test(head.apply(l))) {
return prepend.apply(head.apply(l),
filter.apply(fn, tail.apply(l)));
} else {
return filter.apply(fn, tail.apply(l));
}
};
// --
 
// -- Number constructs
zero = emptyList;
 
inc = n -> prepend.apply(emptyList, n);
dec = tail::apply;
 
one = inc.apply(zero);
 
isZero = isEmpty::test;
 
add = (a, b) -> isZero.test(b)
? a : add.apply(inc.apply(a), dec.apply(b));
sub = (a, b) -> isZero.test(b)
? a : add.apply(dec.apply(a), dec.apply(b));
mul = (a, b) -> isZero.test(b)
? zero : add.apply(a, mul.apply(a, dec.apply(b)));
div = (a, b) -> lessThan.apply(a, b)
? zero : inc.apply(div.apply(sub.apply(a, b), b));
pow = (a, b) -> isZero.test(b)
? one : mul.apply(a, pow.apply(a, dec.apply(b)));
rem = (a, b) -> lessThan.apply(a, b)
? a : rem.apply(sub.apply(a, b), b);
 
isEqual = (n, m) -> {
if (and.apply(isZero.test(n), isZero.test(m))) {
return true;
} else if (or.apply(isZero.test(n), isZero.test(m))) {
return false;
} else {
return isEqual.apply(dec.apply(n), dec.apply(m));
}
};
 
lessThan = (a, b) -> {
if (and.apply(isZero.test(a), isZero.test(b))) {
return false;
} else if (isZero.test(a)) {
return true;
} else if (isZero.test(b)) {
return false;
} else {
return lessThan.apply(dec.apply(a), dec.apply(b));
}
};
 
greaterThan = (a, b) -> lessThan.apply(b, a);
// --
 
// -- List functions that use numbers
nth = (l, n) -> isZero.test(n)
? head.apply(l) : nth.apply(tail.apply(l), dec.apply(n));
 
drop = (l, n) -> isZero.test(n)
? l : drop.apply(tail.apply(l), dec.apply(n));
 
take = (l, n) -> isZero.test(n)
? emptyList
: prepend.apply(head.apply(l),
take.apply(tail.apply(l), dec.apply(n)));
 
slice = l -> // For fun, created a curried version
start ->
end ->
take.apply(drop.apply(l, start), sub.apply(end, start));
 
length = l -> isEmpty.test(l)
? zero : inc.apply(length.apply(tail.apply(l)));
// --
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.