ãããï¼æã«å»¶æã«ãªã£ãã¨ã¯ãããJava 8ãªãªã¼ã¹ãå
·ä½åãã¦ãã¾ãããã
ããããã§ãLambdaãã¯ãããã¨ããããã¨ã¯ãªãããã§ãã
ã¨ããã§ãJava 8ã§é¢æ°åã£ã½ããã¨ãã§ããããã«ãªã£ã¦ããããã®ã§ãããã¡ãã£ã¨è¨è¿°ãåé·ã§ããã¨ãããã¨ã§ãçãæ¸ãããã¾ããªãèãã¦ã¿ã¾ããã
Functionå
ãã¦ãã¾ãã¯Java 8ã§æ¨æºã§å
¥ã£ãFunctionåãã¿ã¦ã¿ã¾ããããããã±ã¼ã¸åã¾ã§å«ããã¨java.util.funciton.Functionã§ãã
ãããªæãã§ä½¿ãã¾ãã
Function<String, String> enclose = s -> "[" + s + "]";
Genericsã§ã®åæå®ã®æåãå¼æ°ããã¨ãæ»ãå¤ã®åã§ããããã§ã¯Stringãã¨ã£ã¦Stringãè¿ãé¢æ°ã¨ãã¦encloseãå®ç¾©ãã¦ãã¾ãã
ãããå¼ã³åºããã¨ããã¨ããããªæãã«ãªãã¾ãã
System.out.println( enclose.apply("foo") );
ããããã¨ã次ã®ãããªè¡¨ç¤ºã«ãªãã¾ãã
[foo]
ããã²ã¨ã¤é¢æ°ãå®ç¾©ãã¦ã¿ã¾ããããæåã®æåã大æåã«ããé¢æ°ã§ãã
Function<String, String> capitalize = s -> s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
2æåæªæºã®æååãä¸ããã¨æ»ã«ã¾ãããã³ã¼ãä¾ã£ã¦ãã¨ã§å¤§ç®ã«ã¿ã¦ãã ããã
å¼ã³åºãã¦ã¿ã¾ãã
System.out.println( capitalize.apply("foo") );
ãããªæãã®è¡¨ç¤ºã«ãªãã¾ãã
Foo
ãã®ï¼ã¤ãé ã«å¼ã³åºãã¦ãcapitalizeãã¦encloseãããã¨ããã¨ããããªæãã«ãªãã¾ããã
System.out.println( enclose.apply(capitalize.apply("foo")) );
表示ã¯ãããªãã¾ãã
[Foo]
ããããå ´åãandThenã使ãã¨é£çµãããã¨ãã§ãã¾ãã
System.out.println( capitalize.andThen(enclose).apply("foo") );
ããã¯ãé¢æ°åæã¨ãã¦ä½¿ããã¨ãã§ãã¾ãã
Function<String, String> capEnc = capitalize.andThen(enclose);
System.out.println( capEnc.apply("foo"));
Java8ã§é¢æ°åã£ã½ããã¨ãã§ãããã¨ããããã¾ãããããã¾ãããï¼
å¥åã使ã
ãã¦ãFunctionã£ã¦é·ãã®ã§ãFã¨ãçãæ¸ãããã§ããã
ãããªã¨ãã«ã¯ãinterfaceãå®ç¾©ãã¦ãã¾ãã¾ãã
interface F<T, R> extends Function<T, R> {}
F<String, String> enclose = s -> "[" + s + "]"; F<String, String> capitalize = s -> s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
ããã§å®ç¾©ããã£ãããã¾ãããã§ãå¼ã³åºãã«ã¯ããããããapplyãæ¸ãå¿
è¦ãããã¾ãã
ããã«ã次ã®é¢æ°åæã¯æããã¡ããã¾ãã
F<String, String> capEnc = capitalize.andThen(enclose);
ã©ãã«ãããªãã¨ãã
ãã¾ããªã
å¥åã ãã§ã¯ã¡ãã£ã¨ä¸ä¾¿ãªã®ã§ãapplyãandThenç¸å½ã®ã¡ã½ãããçãå®ç¾©ããªããã¾ãã
interface F<T, R> extends Function<T,R>{ default R _(T t){ return apply(t); } default <V> F<T,V> x(F<? super R, ? extends V> after){ return (T t) -> after.apply(apply(t)); } } interface C<S> extends Consumer<S>{ default void _(S s){ accept(s); } }
ããããã¨ã次ã®ããã«ãªãã¾ããConsumerãã¤ãã§ã«çãããã®ã§ããã£ã¨ãããSystem.out.printlnã«ãå¥åãã¤ããã¾ãã
C<String> println = System.out::println; F<String, String> enclose = s -> "[" + s + "]"; println._( enclose._("foo") ); F<String, String> capitalize = s -> s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(); println._( capitalize._("foo") );
ããã§ã1å¼æ°ã®Functionã ãåå®ç¾©ãããã©ã2å¼æ°ã®BiFunctionã¨ãã®ããªã¨ã¼ã·ã§ã³ãå¿
è¦ãªããããªããã¨æãããããã¾ãããã2å¼æ°ä»¥ä¸ã®é¢æ°ã¯çããªã®ã§ã1å¼æ°ã®é¢æ°ãå®ç¾©ã§ããã°åé¡ããã¾ããã
é¢æ°åæ
ããã§ãé¢æ°åæã次ã®ããã«æ¸ãã¾ãã
println._( capitalize.x(enclose)._("foo") );
ããã«ããã²ã¨ã¤é¢æ°ãç¨æãã¦æ¬¡ã®ããã«æ¸ãã¦ã¿ã¾ããã¾ããªãããããåãåºãé¢æ°ã§ãã
F<String, String> middle = s -> s.substring(s.length() / 3, s.length() * 2 / 3); println._( middle.x(capitalize).x(enclose)._("yoofoobar") );
é¢æ°åæã使ããªãã¨ã次ã®ããã«ãªãã¾ããã
println._( enclose._(capitalize._(middle._("foobaryah"))));
ãã®ããã«ãå®éã«å¼ã³åºãé ã¨è¨è¿°ãéã«ãªãã¾ããmiddleãã¦capitalizeãã¦encloseããã®ã«ãå
ã«encloseããæ¸ãå¿
è¦ãããã¾ãã
ã¾ããã«ãã³ããã¾ã£ã¦ããã®ã§ãæå¾ã«ã¾ã¨ãã¦ã«ãã³ãéããå¿
è¦ãããã¾ããã¡ãã£ã¨é¢æ°å¼ã³åºããå¢ããã¨ãéãã«ãã³ã®æ°ãããããããªããªã£ã¦ã³ã³ãã¤ã«ã¨ã©ã¼ããªããªãã¨ããã¾ã§ã)ããå¢ããããæ¸ãããããªãã¦ãã¨ãããã¾ãããã
å ã»ã©ã®Fã«æ¬¡ã®ãããªã¡ã½ããã追å ãã¦ããã¨ããã«ããã§ãã
default R ããã®ã(T t){ return _(t); } default <V> F<T,V> ãã¦(F<? super R, ? extends V> after){ return x(after); }
ãããªã£ã¦èªã¿ãããã§ããã
println._( middle.ãã¦(capitalize).ãã¦(enclose).ããã®ã("yoofoobar") );
ã«ãªã¼å
ãã¦ãï¼å¼æ°ä»¥ä¸ã®é¢æ°ã¯çãã¨æ¸ãã¾ããããå®éï¼ã¤ä»¥ä¸ã®ãã©ã¡ã¼ã¿ã渡ãããã¨ãã¯ã©ãããã°ãããã§ãããï¼
ããããã¨ãã«ä½¿ãã®ãã«ãªã¼åã§ããã«ãªã¼åã¯ãã²ã¨ã¤ã®å¼æ°ãã¨ã£ã¦é¢æ°ãè¿ããã¨ã§ãè¤æ°ã®ãã©ã¡ã¼ã¿ã«å¯¾å¿ãã¾ãã
ä¾ãã°ã¯ããæåã¨ã¯ãã¾ããæåãæå®ãã¦ãæåãæåã§ã¯ããé¢æ°ãé常ã®2å¼æ°é¢æ°ã§ãããããªã次ã®ãããªsandwichãããã¨ãã¾ãã
String sandwich(String enc, String str){
return enc + str + enc;
}
ããã1å¼æ°é¢æ°ã§ã«ãªã¼åãã¦æ¸ãã¨æ¬¡ã®ããã«ãªãã¾ãã
F<String, F<String, String>> sandwich = enc -> str -> enc + str + enc;
sandwichèªä½ã¯ãæååãã¨ã£ã¦ããæååãã¨ã£ã¦æååãè¿ãé¢æ°ããè¿ãé¢æ°ã«ãªã£ã¦ãã¾ãã
å¼ã³åºãã¯æ¬¡ã®ããã«ãªãã¾ãã
println._( sandwich._("***")._("sanded!") );
表示ã¯æ¬¡ã®ããã«ãªãã¾ãã
***sanded!***
3å¼æ°ã ã¨ãããªæãã§ããã
F<String, F<String, F<String, String>>> encloseC = pre -> post -> s -> pre + s + post;
encloseCã¯ãæååãã¨ã£ã¦ããæååãã¨ã£ã¦ããæååãã¨ã£ã¦æååãè¿ãé¢æ°ããè¿ãé¢æ°ããè¿ãé¢æ°ã«ãªã£ã¦ãã¾ãã
å¼ã³åºãã¯ãããªæãã§ã
println._( encloseC._("{")._("}")._("enclosed!") );
表示ã¯ãããªãã¾ãã
{enclosed!}
ã¨ããã§ããã®ã«ãªã¼åãããencloseCãå¼æ°ãé¨åçã«ãããã¦ãããã¨ãã§ãã¾ãã
F<String, String> encloseCurly = encloseC._("{")._("}"); println._( encloseCurly._("å²ã¾ããï¼") );
ãããã£ã¦é¨åé©ç¨ãããã¨ã§ãæ°ããé¢æ°ãä½ããããã§ãã
ã¡ãªã¿ã«Curlyã¯ãæ³¢ã«ãã³=ã«ã¼ãªã¼ãã©ã±ããã{ã}ãã®ãã¨ã§ã«ãªã¼åã¨ã¯é¢ä¿ãªãã®ã§ãããããã
ã¾ã¨ã
ãã¾ããªãé©ç¨ä»¥éã®ã½ã¼ã¹ããããªæãã§ããã¡ãã£ã¨ããJavaã ã£ã¦è¨ã£ã¦ãããã£ã¦ããããªãããã§ããã
ã¯ã¦ãªè¨æ³ã§ä¸å¿ ã>|java| ã ||<ãã£ã¦æå®ãã¦ããã§ããã©ãã»ãã¨ã«Javaèªèããããã ãããï½
C<String> println = System.out::println; F<String, String> enclose = s -> "[" + s + "]"; println._( enclose._("foo") ); F<String, String> capitalize = s -> s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase(); println._( capitalize._("foo") ); //é¢æ°åæ println._( capitalize.x(enclose)._("foo") ); F<String, String> middle = s -> s.substring(s.length() / 3, s.length() * 2 / 3); println._( middle.x(capitalize).x(enclose)._("yoofoobar") ); println._( enclose._(capitalize._(middle._("foobaryah")))); println._( middle.ãã¦(capitalize).ãã¦(enclose).ããã®ã("yoofoobar") ); //ã«ãªã¼å F<String, F<String, String>> sandwich = enc -> str -> enc + str + enc; println._( sandwich._("***")._("sanded!") ); F<String, F<String, F<String, String>>> encloseC = pre -> post -> s -> pre + s + post; println._( encloseC._("{")._("}")._("enclosed!") ); F<String, String> encloseCurly = encloseC._("{")._("}"); println._( encloseCurly._("å²ã¾ããï¼") );