This file contains hidden or 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
import java.util.function.Function; | |
import static java.lang.String.format; | |
class Scratch { | |
public static void main(String[] args) { | |
//instantiate as an object | |
Function<Integer, String> stringifyInteger = stringifyInteger(); | |
System.out.println(format("Output is %s", stringifyInteger.apply(3))); |
This file contains hidden or 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
import java.math.BigDecimal; | |
import java.util.function.Function; | |
import static java.lang.String.format; | |
class ComposingFunctions { | |
public static void main(String[] args) { | |
Function<BigDecimal, Integer> asInteger = a -> a.intValue(); |
This file contains hidden or 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
import java.util.function.Function; | |
import static java.lang.String.format; | |
class SamplePartialApplication { | |
public static void main(String[] args) { | |
Fx2<Integer, Integer, Integer> add = (x, y) -> x + y; | |
System.out.println(format("Fully applied function of 2 arity result: %s", add.apply(2, 3))); |
This file contains hidden or 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
import java.util.function.Function; | |
import static java.lang.String.format; | |
class HigherOrderFunctions { | |
public static void main(String[] args) { | |
Function<Integer, Integer> addOne = describeOperationFor(1); | |
Function<Integer, Integer> multiplyOne = a -> a * 1; |
This file contains hidden or 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
import java.util.function.Function; | |
import static java.lang.String.format; | |
class SampleCurrying { | |
public static void main(String[] args) { | |
Fx2<Integer, Integer, Integer> add = (x, y) -> x + y; | |
System.out.println(format("Non curried add function of 2 arity result: %s", add.apply(2, 3))); |
This file contains hidden or 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
import java.math.BigDecimal; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.Collections; | |
import java.util.List; | |
import static java.lang.String.format; | |
import static java.util.Arrays.asList; | |
class SampleFolding { |
This file contains hidden or 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
import java.math.BigDecimal; | |
import java.util.function.Function; | |
import static java.lang.String.format; | |
class SampleLazyEvaluation { | |
public static void main(String[] args) { | |
Function<String, BigDecimal> asBigDecimal = BigDecimal::new; |
This file contains hidden or 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
import lombok.AllArgsConstructor; | |
import lombok.Getter; | |
import java.math.BigDecimal; | |
import java.util.function.Function; | |
import static java.lang.String.format; | |
class SampleTuples { |
This file contains hidden or 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
import java.util.List; | |
import java.util.function.Function; | |
import static java.util.stream.Collectors.collectingAndThen; | |
import static java.util.stream.Collectors.toList; | |
/** | |
* A Monad like construct (e.g. {@code Optional}) that handles | |
* whether the operation is successful or not | |
*/ |
This file contains hidden or 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
import java.util.function.Function; | |
/** | |
* AKA function monad | |
* @param <R> values to be passed | |
* @param <A> resulting value upon application of instance of R | |
*/ | |
public final class Reader<R, A> { | |
private final Function<R, A> g; |
OlderNewer