Finito Capitolo 2 del libro

This commit is contained in:
Fabio Scotto di Santolo
2020-01-23 15:44:29 +01:00
parent 0c60cbf447
commit 39f49cd0d3
6 changed files with 133 additions and 12 deletions

View File

@@ -4,6 +4,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>functional-programming-java</artifactId>
<description>Questo modulo contiene gli esercizi e le prove dal libro "Functional Programmin in Java"</description>
<packaging>jar</packaging>
<parent>

View File

@@ -1,4 +1,11 @@
package org.gym.fp.fpjava;
import org.gym.fp.fpjava.demo.FunctionDemo;
public class Main {
public static void main(String[] args) {
FunctionDemo.run();
}
}

View File

@@ -0,0 +1,49 @@
package org.gym.fp.fpjava.demo;
import org.gym.fp.fpjava.type.BinaryOperator;
import org.gym.fp.fpjava.type.Function;
import org.gym.fp.fpjava.type.Tuple;
public class FunctionDemo {
// Es. 2.12
public static final Function<Integer, Integer> factorial =
n -> n <= 1 ? n : n * FunctionDemo.factorial.apply(n - 1);
public static void run() {
// function composition
Function<Integer, Integer> triple = n -> n * 3;
Function<Integer, Integer> square = n -> n * n;
Function<Integer, Integer> compose = square.compose(triple);
System.out.println(compose.apply(2));
System.out.println(square.andThen(triple).apply(2));
// function currying
BinaryOperator add = x -> y -> x + y;
System.out.println(add.apply(3).apply(5));
System.out.println(func().apply("ciao").apply("mondo").apply("questo").apply("currying"));
// partial function
Function<Double, Function<Double, Double>> addTax = x -> y -> y + y / 100 * x;
Function<Double, Double> add9PercentTax = addTax.apply(9.0);
System.out.println(add9PercentTax.apply(90.0));
// recursive function
System.out.println(factorial.apply(5));
}
// ES. 2.9
private static <A, B, C, D> Function<A, Function<B, Function<C, Function<D, String>>>> func() {
return a -> b -> c -> d -> String.format("%s %s %s %s", a, b, c, d);
}
// ES. 2.10
private static <A, B, C> Function<A, Function<B, C>> curry(Function<Tuple<A, B>, C> fn) {
return a -> b -> fn.apply(new Tuple<>(a, b));
}
// ES. 2.11
private static <T, U, V> Function<U, Function<T, V>> swap(Function<T, Function<U, V>> fn) {
return u -> t -> fn.apply(t).apply(u);
}
}

View File

@@ -0,0 +1,4 @@
package org.gym.fp.fpjava.type;
public interface BinaryOperator extends Function<Integer, Function<Integer, Integer>> {
}

View File

@@ -0,0 +1,49 @@
package org.gym.fp.fpjava.type;
public interface Function<T, U> {
U apply(T arg);
default <V> Function<V, U> compose(Function<V, T> f) {
return x -> apply(f.apply(x));
}
default <V> Function<T, V> andThen(Function<U, V> f) {
return x -> f.apply(apply(x));
}
default <R> Function<U, R> partialA(T a, Function<T, Function<U, R>> f) {
return f.apply(a);
}
default <R> Function<T, R> partialB(U b, Function<T, Function<U, R>> f) {
return a -> f.apply(a).apply(b);
}
static <T> Function<T, T> identity() {
return x -> x;
}
static <T, U, V> Function<V, U> compose(Function<T, U> f, Function<V, T> g) {
return x -> f.apply(g.apply(x));
}
static <T, U, V> Function<T, V> andThen(Function<T, U> f, Function<U, V> g) {
return x -> g.apply(f.apply(x));
}
static <T, U, V> Function<Function<T, U>, Function<Function<U, V>, Function<T, V>>> compose() {
return x -> y -> y.compose(x);
}
static <T, U, V> Function<Function<T, U>, Function<Function<V, T>, Function<V, U>>> andThen() {
return x -> y -> y.andThen(x);
}
static <T, U, V> Function<Function<T, U>, Function<Function<U, V>, Function<T, V>>> higherAndThen() {
return x -> y -> z -> y.apply(x.apply(z));
}
static <T, U, V> Function<Function<U, V>, Function<Function<T, U>, Function<T, V>>> higherCompose() {
return (Function<U, V> x) -> (Function<T, U> y) -> (T z) -> x.apply(y.apply(z));
}
}

View File

@@ -0,0 +1,11 @@
package org.gym.fp.fpjava.type;
public class Tuple<A, B> {
private final A _1;
private final B _2;
public Tuple(A first, B second) {
this._1 = first;
this._2 = second;
}
}