From e3810fbf9b7d207e13b93f4d8698454abe78683f Mon Sep 17 00:00:00 2001 From: Ben Culkin Date: Sat, 21 Nov 2020 11:03:25 -0500 Subject: Add a basic fixpoint function This adds a 'fixpoint' function which allows you to create recursive lambda functions more easily --- src/example/java/bjc/functypes/FixpointExample.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 src/example/java/bjc/functypes/FixpointExample.java (limited to 'src/example/java/bjc') 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> func + = (input, self) -> { + if (input <= 1) return 1; + else return input * self.apply(input - 1); + }; + + Function factorial = Fixpoints.fix(func); + + for (int i = 0; i < 10; i++) System.out.println(factorial.apply(i)); + } +} -- cgit v1.2.3