Skip to content

Instantly share code, notes, and snippets.

@ayago
Last active July 25, 2019 08:38
Show Gist options
  • Save ayago/f369f1b5016061b44cc3ed1dfddd3bb6 to your computer and use it in GitHub Desktop.
Save ayago/f369f1b5016061b44cc3ed1dfddd3bb6 to your computer and use it in GitHub Desktop.
Composing Functions in Java
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