java - Lambda expressions and higher-order functions -


how can write java 8 closures support method take argument function , return function value?

in java lambda api main class java.util.function.function.

you can use reference interface in same way other references: create variable, return result of computation , on.

here quite simple example might you:

    public class higherorder {          public static void main(string[] args) {             function<integer, long> addone = add(1l);              system.out.println(addone.apply(1)); //prints 2              arrays.aslist("test", "new")                     .parallelstream()  // suggestion execution strategy                     .map(camelize)     // call static reference                     .foreach(system.out::println);         }          private static function<integer, long> add(long l) {             return (integer i) -> l + i;         }          private static function<string, string> camelize = (str) -> str.substring(0, 1).touppercase() + str.substring(1);     } 

if need pass more 1 parameter, please take compose method, usage quite tricky.

in general opinion closures , lambdas in java syntax-sugar, , seem not have capabilities of functional programming.


Comments

Popular posts from this blog

ruby - Trying to change last to "x"s to 23 -

jquery - Clone last and append item to closest class -

c - Unrecognised emulation mode: elf_i386 on MinGW32 -