Last active
March 7, 2020 07:11
Revisions
-
timyates revised this gist
Nov 27, 2013 . 1 changed file with 45 additions and 0 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,45 @@ package java8tests; import java.util.function.IntBinaryOperator ; import java.util.function.IntFunction ; import java.util.function.IntUnaryOperator ; public class NativeIntCurrying { public void currying() { // Create a function that adds 2 ints IntBinaryOperator adder = ( a, b ) -> a + b ; // And a function that takes an integer and returns a function IntFunction<IntUnaryOperator> currier = a -> b -> adder.applyAsInt( a, b ) ; // Call apply 4 to currier (to get a function back) IntUnaryOperator curried = currier.apply( 4 ) ; // Results System.out.printf( "int curry : %d\n", curried.applyAsInt( 3 ) ) ; // ( 4 + 3 ) } public void composition() { // A function that adds 3 IntUnaryOperator add3 = (a) -> a + 3 ; // And a function that multiplies by 2 IntUnaryOperator times2 = (a) -> a * 2 ; // Compose add with times IntUnaryOperator composedA = add3.compose( times2 ) ; // And compose times with add IntUnaryOperator composedB = times2.compose( add3 ) ; // Results System.out.printf( "int times then add: %d\n", composedA.applyAsInt( 6 ) ) ; // ( 6 * 2 ) + 3 System.out.printf( "int add then times: %d\n", composedB.applyAsInt( 6 ) ) ; // ( 6 + 3 ) * 2 } public static void main( String[] args ) { new NativeIntCurrying().currying() ; new NativeIntCurrying().composition() ; } } -
timyates created this gist
Nov 27, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,44 @@ package java8tests ; import java.util.function.BiFunction ; import java.util.function.Function ; public class Currying { public void currying() { // Create a function that adds 2 integers BiFunction<Integer,Integer,Integer> adder = ( a, b ) -> a + b ; // And a function that takes an integer and returns a function Function<Integer,Function<Integer,Integer>> currier = a -> b -> adder.apply( a, b ) ; // Call apply 4 to currier (to get a function back) Function<Integer,Integer> curried = currier.apply( 4 ) ; // Results System.out.printf( "Curry : %d\n", curried.apply( 3 ) ) ; // ( 4 + 3 ) } public void composition() { // A function that adds 3 Function<Integer,Integer> add3 = (a) -> a + 3 ; // And a function that multiplies by 2 Function<Integer,Integer> times2 = (a) -> a * 2 ; // Compose add with times Function<Integer,Integer> composedA = add3.compose( times2 ) ; // And compose times with add Function<Integer,Integer> composedB = times2.compose( add3 ) ; // Results System.out.printf( "Times then add: %d\n", composedA.apply( 6 ) ) ; // ( 6 * 2 ) + 3 System.out.printf( "Add then times: %d\n", composedB.apply( 6 ) ) ; // ( 6 + 3 ) * 2 } public static void main( String[] args ) { new Currying().currying() ; new Currying().composition() ; } }