Last active
July 25, 2019 08:38
-
-
Save ayago/f369f1b5016061b44cc3ed1dfddd3bb6 to your computer and use it in GitHub Desktop.
Composing Functions in Java
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(); | |
Function<Integer, String> describe = a -> format("Number is %s", a); | |
//manual invocation of functions | |
System.out.println(format("Manual fx output is %s", | |
describe.apply(asInteger.apply(new BigDecimal("3.00"))))); | |
Function<BigDecimal, String> composedDescribe = describe.compose(asInteger); | |
//invoking functions via composition | |
System.out.println(format("Composed fx output is %s", | |
composedDescribe.apply(new BigDecimal("3.00")))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment