Javaããã°ã©ãåãã«ãé«éé¢æ°ã®ã話ã§ãã
é«éé¢æ°ãšã¯ããé¢æ°ãåŒæ°ã«åãé¢æ°ãã®ããšã§ããæ¬æ¥ã¯ãé¢æ°ãè¿ãé¢æ°ãïŒã«ãªãŒåïŒãå«ãããã§ãããé«éé¢æ°ãšãããšå°ããã®æå³ã§äœ¿ãããããšãå€ãã®ã§åé¡ãªãã§ãããã
Javaã§ã¯ãã¡ãœãããšãã®åŒæ°ã®æå°åäœã¯ã¯ã©ã¹ïŒã®ã€ã³ã¹ã¿ã³ã¹ïŒãããã¯ããªããã£ããªã®ã§ãä»ã®èšèªã§ç»å Žããé¢æ°æž¡ããšãã¯ããŒãžã£ãŒãšãã¯ãJavaããç¥ããªãããã°ã©ããããããšãçžåœçé ãªååšã§ã¯ãªãããšæããŸãã
èªåããScalaãå匷ãããŸã§ã¯ãããªæãã§ããããããŠãScalaãç¥ã£ãŠããã¡ãã£ãšããè¡æãåããããã§ããã
ä»åã¯ãPythonãšJavaã察æ¯ããŠçŽ¹ä»ããããšæããŸãããªã¹ãã®å埩åŠçïŒforeachïŒãšããªã¹ãããæ°ãããªã¹ããçæããåŠçãååïŒmapïŒã®äŸã§ãããŸãã
ãŸãã¯ãPythonã³ãŒãããããŸãã¯ãååãšãªãé¢æ°ãmy_foreachãšmy_mapã宣èšããŸããã¡ãªã¿ã«ãæšæºã®CentOSã§ãã£ãŠããã®ã§ãPythonã®ããŒãžã§ã³ã¯2.4.3ã§ããã
â»my_ãä»ããŠããã®ã¯ãPythonã«ã¯æšæºã§mapé¢æ°ãããããã§ã
def my_foreach(seq, func): for s in seq: func(s) def my_map(seq, func): newSeq = [] for s in seq: newSeq.append(func(s)) return newSeq # return [func(s) for s in seq] ## äžèšã³ãŒãã¯ãããšç䟡
my_mapã¯forå å è¡šèšãå©çšããã°ãã£ãšçããªããŸãããPythonãç¥ããªããŠãããããããªè¡šèšã«ããŸããã
ç¶ããŠãåŠçã®å ãã¿ãšãªããªã¹ãã宣èšã
xs = [1, 2, 3, 4, 5]
my_mapãšmy_foreachã«é©çšããé¢æ°ã宣èšããé©çšããŠã¿ãŸããmy_mapã«ã¯èŠçŽ ã3åããŠè¿ãé¢æ°tripleãæž¡ããmy_foreachã«ã¯åçŽã«èŠçŽ ãåºåããã ãã®print_elemé¢æ°ãæž¡ããŸãã
def triple(x): return x * 3 def print_elem(x): print x, print my_map(xs, triple) # -> [3, 6, 9, 12, 15] my_foreach(xs, print_elem) # -> 1 2 3 4 5
my_mapãmy_foreachãåŒã³åºããŠããã³ãŒãã®é£ã«ã³ã¡ã³ãã§çµæãæžããŠããŸãããé¢æ°ã®åŒæ°ã«é¢æ°ãæž¡ããŠåŠçãå®è¡ããŠããŸãã
ãããªçãåŠçã«ããã¡ãã¡é¢æ°ã宣èšããã®ã¯å«ã ïŒãšããæ¹ã¯ãlambdaãå©çšããã°å¿åé¢æ°ã宣èšã§ããã®ã§ããããmy_mapããã³my_foreachã«æž¡ããŸãã
import sys print my_map(xs, (lambda x: x * 3)) # -> [3, 6, 9, 12, 15] my_foreach(xs, (lambda x: sys.stdout.write(str(x)))) # -> 12345
å
ã»ã©ã®äŸãšãåæ§ã®åããããŸãã
â»Pythonã®printã¯é¢æ°ã§ã¯ãªãæãªã®ã§ãlambdaã§å©çšã§ããªãã£ãœãã§ãâŠ
ã§ã¯ãJavaã§åãããšãããã«ã¯âŠïŒãšããããšã§èããŠã¿ãŸãããã
Javaã«ã¯é¢æ°ããšããããã¡ãœãããçŽæ¥æž¡ããããªããšã¯ã§ããŸããããããéåžžã®æ§æãšããŠããšããæå³ã§ããªãã¬ã¯ã·ã§ã³ã䜿ãã°Methodãæž¡ããããããšãããã®ã¯ããã§ã¯çœ®ããŠãããŸãã
â»ããã§ã¯ã¿ã€ãã»ãŒãã§ã¯ãªããªãã®ã§ãJavaã§ãã£ãŠããæå³ããªããªããŸã
ãŸãã¯ãé¢æ°ãè¡šãã€ã³ã¿ãŒãã§ãŒã¹ãå®çŸ©ããŸãã
public interface Function1<IN, OUT> { public OUT apply(IN in); }
1ã£ãŠäœã ïŒãšæããããããŸããããåŒæ°ã1ã€åãé¢æ°ã®æå³ã§ãâŠã2ã€åããªããFunction2ãšãä»ãããã§ããããã
ç¶ããŠãforeachã¡ãœãããšmapã¡ãœãããæã£ãã¯ã©ã¹ã宣èšããŸãã
â»ç°¡åã®ãããArrayListã§æ±ºãæã¡ã«ããŠããŸã
import java.util.*; public class FunctionalCollections { public static <T> void foreach(List<T> list, Function1<? super T, Void> func) { for (T t : list) { func.apply(t); } } public static <T, S> List<S> map(List<T> list, Function1<? super T, ? extends S> func) { List<S> newList = new ArrayList<S>(); for (T t : list) { newList.add(func.apply(t)); } return newList; } }
ãªããåŒæ°ã«ãªã£ãŠããFunction1ã®äžéå¢çã¯ã€ã«ãã«ãŒããäžéå¢çã¯ã€ã«ãã«ãŒãã¯ãgetãšputã®ååã«åã£ãŠããŸãã
Java ã®çè«ãšå®è·µ: Generics ã®ã¯ã€ã«ãã«ãŒãã䜿ãããªãã第 2 å
http://www.ibm.com/developerworks/jp/java/library/j-jtp07018.html
ç¶ããŠãforeachãšmapã®åŒæ°ãšãªãã¯ã©ã¹ã宣èšããŸãããããã¡ãã¯ãç°¡åã®ããIntegerã«æ±ºãæã¡ã«ããŸããã
class Triple implements Function1<Integer, Integer> { @Override public Integer apply(Integer in) { return in * 3; } } class PrintElem implements Function1<Integer, Void> { @Override public Void apply(Integer in) { System.out.print(in); return null; } }
ä»åã¯æ»ãå€ãIntegerã§ãããå¥ã«Stringãšãã«å€æããŠãåé¡ãããŸãããããšããžã§ããªã¯ã¹ãšvoidã£ãŠçžæ§æªãã§ããããŒã
ããã䜿ãåŽã®ã³ãŒãã¯ããããªããŸãã
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5); FunctionalCollections.foreach(list, new PrintElem()); // -> 12345 System.out.println(FunctionalCollections.map(list, new Triple())); // -> [3, 6, 9, 12, 15]
Pythonã®lambdaãã©ãã®ããšãããããšãããšããããªããŸãã
FunctionalCollections.foreach(list, new Function1<Integer, Void>() { @Override public Void apply(Integer in) { System.out.print(in); return null; } }); // -> 12345 System.out.println(FunctionalCollections.map(list, new Function1<Integer, Integer>() { @Override public Integer apply(Integer in) { return in * 3; } })); // -> [3, 6, 9, 12, 15]
âŠé·ãïŒïŒ
ã€ãŸããJavaã¯åŒæ°ã®åæž¡ãã«ã¯å¿ ãã¯ã©ã¹ã®ã€ã³ã¹ã¿ã³ã¹ãèŠæ±ãããããããããèšè¿°ã«ãªã£ãŠããŸããŸããããããããšã¯
System.out.print(in);
ãš
return in * 3;
ã®ã¯ããªã®ã«ãããã«è³ããŸã§ã®éçšãé·ãé·ãããã£ãšãµãã¯ãªæžãããããã®ã«ããšæããŸããïŒã¶ã£ã¡ãããåŠçãããã¯ã©ã¹ã®å®£èšã®æ¹ãé·ããã§ããã©âŠã
äž¡è ã®å·®ã¯ãé¢æ°ãäžäººåã®å€ïŒãªããžã§ã¯ãïŒãšããŠæ±ãããã©ããããšããç¹ã«åž°çµããŸãã
ãã ãå¥ã«Javaããã«ã«ãããããã§ã¯ãããŸãããäžèšPythonãšJavaã®ã³ãŒãã¯æ¬è³ªçã«åãã§ãããJavaã§ã¯ããããæ§é ãè¡šçŸã§ããªãããã§ããããŸãããåé¡ãªã®ã¯ãã·ã³ã¿ãã¯ã¹ãéããããã ããªã®ã§ãã
ãã£ãŠãJavaã§ã¯ããããçºæ³ãããŠããã³ãŒãã¯ããŸããªããããªã¹ããå埩åŠçããªããããšèšãããã°ããã®å Žã§forã«ãŒããIteratorã§åããããªã¹ããã®ãã®ãã¡ãœããã®åŒæ°ãšããŠæž¡ããã¡ãœããå ã§ã«ãŒããå©çšããŸããã€ãŸãã
for (T element : list) { // äœãåŠç }
ã®ãããªæ±ºãŸããã£ãã³ãŒããããªã¹ããç»å Žãã床ã«æ£åšããããšã«ãªãããã§ãããã®æ§é ãäžè¬åããŠãã// äœãåŠçãã®éšåã ããå€ããæž¡ãããšãã§ãããšãåŠçãå ±éåã§ãããšæããŸãããïŒ
ãããã§ãããšãããªã¹ããforã«ãŒãã§åãããšãããªã¹ãã®åèŠçŽ ã«åŠçãè¡ãããšãããªã¹ãã«å«ãŸããèŠçŽ ã®åèšå€ãæ±ããããšãããã«ãŒããªã©ã®å®è£ ã®è©³çŽ°ãé ãºããããããæœè±¡åºŠã®é«ã宣èšçãªã³ãŒããæžããããã«ãªããŸããä»ã«ããã³ãã¬ãŒãçãªã³ãŒãã§ãããšããªãœãŒã¹ã®ã¯ããŒãºã®ãããªåŠçããããŸãããäœã®çåãç¡ãæ¯åtryãcatchãfinallyãæžããŠãããšæããŸãããtryãcatchã®äžãå€ããæž¡ãã䜿ã£ããªãœãŒã¹ã¯èªåçã«éããããã«ããã°ãããã§ãããïŒ
ããã¯ã身è¿ãªèšèã§èšããšè¶ æ±çšçãªã³ãã³ããã¿ãŒã³ã«ä»ãªããªãã®ã§ãããJavaã§ãã®ã¢ãããŒãããã£ãŠããŸããšç²åºŠã®å°ããã¯ã©ã¹ã®å¢å ãæããŸãããŸãå è¿°ã®éãã·ã³ã¿ãã¯ã¹çã«ãéããªããã¡ãªãããããŸãäžè¬çã§ã¯ãªãããšæããŸãã
ãã ããããç¥ããšçºæ³ãåºããã®ã§é¢çœãã§ãããåããŠç解ããæã¯ãç®ããé±ãèœã¡ãæãã§ããã
ããŸã«ã¯ãJava以å€ã®èšèªã§éãã§ã¿ãŸãããâªçŽæ¥Javaã§è¡šçŸããããšã¯é£ãããããããŸããããããã°ã©ãã³ã°ã®åŒãåºããå¢ããããã®ããåºæ¿ã«ãªãããšæããŸããã