l**********n 发帖数: 8443 | 1 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 adder = ( a, b ) -> a + b ;
// And a function that takes an integer and returns a function
Function> currier = a -> b ->
adder.apply( a, b ) ;
// Call apply 4 to currier (to get a function back)
Function curried = currier.apply( 4 ) ;
// Results
System.out.printf( "Curry : %dn", curried.apply( 3 ) ) ; // ( 4 + 3 )
}
public void composition() {
// A function that adds 3
Function add3 = (a) -> a + 3 ;
// And a function that multiplies by 2
Function times2 = (a) -> a * 2 ;
// Compose add with times
Function composedA = add3.compose( times2 ) ;
// And compose times with add
Function composedB = times2.compose( add3 ) ;
// Results
System.out.printf( "Times then add: %dn", composedA.apply( 6 ) ) ;
// ( 6 * 2 ) + 3
System.out.printf( "Add then times: %dn", composedB.apply( 6 ) ) ;
// ( 6 + 3 ) * 2
}
public static void main( String[] args ) {
new Currying().currying() ;
new Currying().composition() ;
}
} |
p*****2 发帖数: 21240 | |
N*****m 发帖数: 42603 | 3 这个比scala还差
【在 p*****2 的大作中提到】 : 还不够噁心的
|
l**********n 发帖数: 8443 | 4 scala的type很复杂。
【在 N*****m 的大作中提到】 : 这个比scala还差
|
b*******s 发帖数: 5216 | 5 java is too difficult to hug new things |
b***e 发帖数: 17 | 6 JVM was designed a long time ago and some of the design choices were made in
restricted ways such that some certain modern features are difficult to
extend. It's a dilemma given that Java needs to maintain its backward
compatibility. To this point, Java's doing much better than Ruby or Python,
which are not always backward compatible.
【在 b*******s 的大作中提到】 : java is too difficult to hug new things
|
b*******s 发帖数: 5216 | 7 Java主要是做企业应用的,不后向兼容是不可接受的
in
Python,
【在 b***e 的大作中提到】 : JVM was designed a long time ago and some of the design choices were made in : restricted ways such that some certain modern features are difficult to : extend. It's a dilemma given that Java needs to maintain its backward : compatibility. To this point, Java's doing much better than Ruby or Python, : which are not always backward compatible.
|