Last active
March 23, 2022 05:14
-
-
Save SegFaultAX/9427306 to your computer and use it in GitHub Desktop.
Functional programming Java
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 com.segfaultax; | |
public interface Function<Param, Result> { | |
public Result apply(Param in); | |
} |
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 com.segfaultax; | |
public interface Function2<Param1, Param2, Result> { | |
public Result apply(Param1 a, Param2 b); | |
} |
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 com.segfaultax; | |
import java.util.ArrayList; | |
import java.util.List; | |
public class Functional { | |
public static <T, U, V extends U> U reduce(final Function2<V, ? super T, V> fn, List<? extends T> l, V init) { | |
for (T elem : l) { | |
init = fn.apply(init, elem); | |
} | |
return init; | |
} | |
public static <T> T reduce(Function2<T, ? super T, T> fn, List<? extends T> l) { | |
return reduce(fn, l.subList(1, l.size()), l.get(0)); | |
} | |
public static <T, U> List<U> map(final Function<T, U> fn, List<T> l) { | |
Function2<List<U>, T, List<U>> reducer = new Function2<List<U>, T, List<U>>() { | |
public List<U> apply(List<U> memo, T elem) { | |
memo.add(fn.apply(elem)); | |
return memo; | |
} | |
}; | |
return reduce(reducer, l, new ArrayList<U>()); | |
} | |
public static <T> List<T> filter(final Function<T, Boolean> fn, List<T> l) { | |
Function2<List<T>, T, List<T>> reducer = new Function2<List<T>, T, List<T>>() { | |
public List<T> apply(List<T> memo, T elem) { | |
if (fn.apply(elem)) { | |
memo.add(elem); | |
} | |
return memo; | |
} | |
}; | |
return reduce(reducer, l, new ArrayList<T>()); | |
} | |
public static <T, U> Function<T, List<U>> juxt(final Function<T, U>... fns) { | |
return new Function<T, List<U>>() { | |
public List<U> apply(T arg) { | |
List<U> vals = new ArrayList<U>(); | |
for (Function<T, U> fn : fns) { | |
vals.add(fn.apply(arg)); | |
} | |
return vals; | |
} | |
}; | |
} | |
} |
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 com.segfaultax; | |
import java.util.List; | |
import java.util.ArrayList; | |
public class Main { | |
public static List<Integer> range(int start, int end) { | |
List<Integer> acc = new ArrayList<Integer>(); | |
for (int i = start; i < end; i++) { | |
acc.add(i); | |
} | |
return acc; | |
} | |
public static void main(String[] args) { | |
Function<Integer, Integer> doubleNum = new Function<Integer, Integer>() { | |
public Integer apply(Integer n) { | |
return n * 2; | |
} | |
}; | |
Function<Integer, Integer> inc = new Function<Integer, Integer>() { | |
public Integer apply(Integer n) { | |
return n + 1; | |
} | |
}; | |
Function<Integer, Boolean> even = new Function<Integer, Boolean>() { | |
public Boolean apply(Integer n) { | |
return n % 2 == 0; | |
} | |
}; | |
Function2<Integer, Integer, Integer> add = new Function2<Integer, Integer, Integer>() { | |
public Integer apply(Integer a, Integer b) { | |
return a + b; | |
} | |
}; | |
final List<Integer> nums = range(1, 10); | |
System.out.println("numbers: " + nums); | |
System.out.println("map (double): " + Functional.map(doubleNum, nums)); | |
System.out.println("filter (even): " + Functional.filter(even, nums)); | |
System.out.println("reduce (without init): " + Functional.reduce(add, nums)); | |
System.out.println("reduce (with init): " + Functional.reduce(add, nums, 10)); | |
System.out.println("juxt (double, inc)" + Functional.juxt(doubleNum, inc).apply(10)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment