é¢æ°ãè¿ãé¢æ°ãä½ã
ScalaByExample - Chapter 5.2 Curryingãããé¢æ°ãæ»ãå¤ã¨ãã¦è¿ãé¢æ°ãä½ã£ã¦ã¿ã¾ãã
- å¼æ°ã§æ¸¡ãå ´åã¨åæ§ã«ãæ»ãå¤åãã<ä»®å¼æ°å>:<å¼æ°å..> => <æ»ãå¤å>ãã¨ãããã¨ã§é¢æ°ãè¿ããã¨ã示ãã¾ãã
- å¾ã¯æ»ãå¤ã¨ãã¦é¢æ°ãè¿ãã°OK
// é¢æ°ãè¿ãé¢æ°ã®å®ç¾© // æ»ãå¤åãã<å¼æ°å..> => <æ»ãå¤å>ãã¨ããã(å¼æ°ã§æ¸¡ãã¨ãã¨ä¸ç·) def sum( i:int ):(int)=>int = { def _sum( j:int ) = i+j _sum } println( sum(3)(5) ) // 8 // å¤æ°ãå®æ°ã§è¦ãã¦ãããã¨ãã§ãã var sumVar = sum(3) val sumVal = sum(3) println( sumVar(5) ) // 8 println( sumVal(5) ) // 8
å®è¡çµæã§ãã
8 8 8
âã¨ãããã¨ã§ããsum(3)(5)ãã¨å¼ã³åºããé¢æ°ãå®ç¾©ã§ãã¾ãã
// âã¨ãããã¨ã§ããsum(3)(5)ãã¨å¼ã³åºããé¢æ°ãå®ç¾©ã§ããã // é¢æ°ã¯è¿ãã¦ãªããã©ã def sum( i:int )( j:int ):int = { i+j } println( sum(3)(5) ) // 8
å®è¡çµæã§ãã
8