Created by Mark Perry, @mprry, G+, Blog, LinkedIn, GitHub, maperry78@yahoo.com.au
![]() |
![]() |
![]() |
// imperative factorial
static int factorial(int n) {
int i = n;
int result = 1;
while (i > 1) {
result = result * i;
i = i - 1;
}
return result;
}
static int factorial(int n) {
return (n <= 1) ? 1 : n * factorial(n - 1);
}
// factorial(5) = 5 * factorial(4) = 5 * (4 * factorial(3)) = ...
// tail call version
static int fact(int n) {
return fact(n, 1);
}
static int fact(int n, int acc) {
return (n <= 1) ? acc : fact(n - 1, n * acc);
}
// fact(5, 1) = fact(4, 5 * 1) = fact(3, 4 * 5) = ...
public interface F<A, B> {
B f(A a);
}
void func(F<Integer, Integer> f) {...}
// Java 8
func(x -> x + 1);
// Java 7
func(new F<Integer, Integer>(){
public Integer f(Integer i) {
return i + 1;
}
});
static int abs(int x) { ... }
static int factorial(int n) { ... }
static String formatResult(String name, int n, F<Integer, Integer> f) {
String msg = "The %s of %d is %d.";
return msg.format(name, n, f.f(n));
}
public static void main(String [] args) {
out.println(formatResult("absolute value", -42, x -> abs(x)))
out.println(formatResult("factorial", 7, ClassName::factorial))
}
public static void main(String [] args) {
out.println(formatResult("increment1", 7, (int x) -> x + 1));
out.println(formatResult("increment2", 7, (x) -> x + 1));
out.println(formatResult("increment3", 7, x -> x + 1);
out.println(formatResult("increment4", 7, x -> {
int r = x + 1;
return r;
}));
}
static <A> boolean isSorted(List<A> as, F2<A, A, Boolean> f) {...}
static <A, B> B fold(List<A> as, F2<B, A, B> f, B b) {...}
static <A, B, C> F<B, C> partial1(A a, F2<A, B, C> f) {...}
static <A, B, C> F<A, F<B, C>> curry(F2<A, B, C> f) {...}
static <A, B, C> F<A, C> andThen(F<A, B> f, F<B, C> g) {...}
Functional Programming in Scala, Chiusano and Bjarnason, Chapter 2, Getting Started
![]() |
![]() |
![]() |
Created by Mark Perry, @mprry, G+, Blog, LinkedIn, GitHub, maperry78@yahoo.com.au