diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-11-21 11:03:25 -0500 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-11-21 11:03:25 -0500 |
| commit | e3810fbf9b7d207e13b93f4d8698454abe78683f (patch) | |
| tree | e476446abc5abcfc5e1c8b8a31813431ac9a51ff /src/example | |
| parent | 9273abd4c2df584b26841ac9cde958bda0c07769 (diff) | |
Add a basic fixpoint function
This adds a 'fixpoint' function which allows you to create recursive
lambda functions more easily
Diffstat (limited to 'src/example')
| -rw-r--r-- | src/example/java/bjc/functypes/FixpointExample.java | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/src/example/java/bjc/functypes/FixpointExample.java b/src/example/java/bjc/functypes/FixpointExample.java new file mode 100644 index 0000000..8d3e658 --- /dev/null +++ b/src/example/java/bjc/functypes/FixpointExample.java @@ -0,0 +1,17 @@ +package bjc.functypes; + +import java.util.function.*; + +public class FixpointExample { + public static void main(String[] args) { + BiFunction<Integer, Function<Integer, Integer>, Integer> func + = (input, self) -> { + if (input <= 1) return 1; + else return input * self.apply(input - 1); + }; + + Function<Integer, Integer> factorial = Fixpoints.fix(func); + + for (int i = 0; i < 10; i++) System.out.println(factorial.apply(i)); + } +} |
