Skip to content

Instantly share code, notes, and snippets.

@bodiam
Forked from timyates/Currying.java
Created November 27, 2013 12:19
Show Gist options
  • Save bodiam/7674774 to your computer and use it in GitHub Desktop.
Save bodiam/7674774 to your computer and use it in GitHub Desktop.
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() ;
}
}
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() ;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment