é¢æ°åããã°ã©ãã³ã°æŠè«(Functional Programming)
é¢æ°åããã°ã©ãã³ã°ã«ã€ããŠã®å ±éç¥èããŸãšãã.
é¢æ°åããã°ã©ãã³ã°ãšã¯
- æ°åŠçãªæå³ã§ã®é¢æ°ãäž»ã«äœ¿ãããã°ã©ãã³ã°ã®ã¹ã¿ã€ã«((Wikipedia)
- ðŠé¢æ°åããã°ã©ãã³ã°ãšã¯ä»£å ¥æã®ãªãããã°ã©ãã³ã° - ãããããã
ç¹åŸŽ
ã²ãšããšã§èª¬æããã®ã¯é£ãã. è€æ°ã®ç¹åŸŽã§èª¬æ.
- äžå€æ§(immutability)
- ç¶æ ããããªã
- é«éé¢æ°(high)
- çŽç²é¢æ°(åç §ééæ§)
- 第äžçŽé¢æ°
é¢æ°åèšèªã®æå³ã¯å€ããã€ã€ãã
- æã¯, é«éé¢æ° ããµããŒãããèšèªãšããç·©ãå®çŸ©ã ã£ã.
- çŸä»£ã®ã¢ãã³ãªèšèª (Haskell, Scala ãªã©) ã¯,
æ°åŠççè«ãèæ¯ã«ããã°ã©ã ãèšè¿°ããèšèª
以äžã«æ°åŠçæŠå¿µãšé¢æ°åèšèªã®å¯Ÿå¿ãããããã.
èæ¯
ããŒããŠã§ã¢ã®ã¡ããŒã³ã¢, 倧容éã¡ã¢ãªåã«ãã£ãŠ, æ§èœã®ããã«ããã¯ã I/O ã§ã¯ãªããŠ, ã¢ããªã±ãŒã·ã§ã³ãšãªã£ãŠãã. ã¢ã«ãŽãªãºã ãåè² ã®äžç. ã¢ããªãããã«ããã¯ã«ãªã£ãŠãã. ãã®ãã, èšèªã¬ãã«ã§äžŠè¡ã»äžŠååŠçãæžããããèšèªãæ±ããããããã«ãªã£ã.
Cloud Computing ã«ãããŠ, ç°åžžãçºçãããå šäœããšããã®ã§ã¯ãªããŠ, äžéšãåæ¢ããŠéçšãç¶ç¶ãããå¿ èŠããã.åŸæ¥ã®äŸå€åŠçã§ã¯åŠçããã®ãè€éã«ãªã£ãŠãã.ãã®ãã, èšèªã¬ãã«ã§åæ£ã³ã³ãã¥ãŒãã£ã³ã°ãFault Tolerant ããµããŒããããããªèšèªãæ±ããããããã«ãªã£ã.
èšç®ã®èãæ¹
åœä»€åã§ã¯, èšç®ã®åºæ¬ã¯èããããŠããå€ãå€ããããš.
é¢æ°åã§ã¯, èšç®ã®åºæ¬ã¯åŒæ°ã«é¢æ°ãé©çšããããš.
ã¡ãªãã
- ã³ãŒãéãå°ãªããªã
- é«éé¢æ°ã䜿ã£ãæã䜿ãã
- æé©åããããã
- 䞊ååŠçãæžãããã
- ãã°ãã«ãã (å®çãšèšŒæ)
- ããã¥ã¡ã³ããå°ãªããªã
ãã¡ãªãã
- é¢æ°å®è¡ã®ãªãŒããããã倧ãã
- ã¡ã¢ãªå€§éæ¶è²»
- ã¹ã¿ãã¯äœ¿çšéãèŠç©ãããªã (ååž°)
ã³ã³ãã€ã« = 蚌æ
ã³ã³ãã€ã«ãéããšããããšã¯, æ£ããã蚌æããããš
é¢æ°åèšèªã§ã¯, ã³ã³ãã€ã«ãéããšãã°ãã»ãšãã©ã§ãªã. çŽç²é¢æ°ã®äžçã§ããã°ã©ãã³ã°ãããããšã«ãã£ãŠ, å®çŸã§ãã. èæ¯ã«ã¯æ°çè«çåŠããã. (Curry-Haward 察å¿)
ãã®ããšããªã倧äºããšãããš, 䞊åããã°ã©ãã³ã°ã®ãã°ãšãã¯å€§å€. ãã¹ãã§ãã¹ãŠã®ãã°ããšãããšããä¿èšŒãã§ããªã.
é¢æ°åãªãã°æ°åŠãããŒã¹ã«ããŠ, ãã°ããªãããšã蚌æããããšãã§ãã
åœä»€åããã°ã©ãã³ã°ãšé¢æ°åããã°ã©ãã³ã°ã®æ¯èŒ
impelative paradium
- ã«ãŒãã§å埩æ§é ãå®è¡
- ç°ãªãé¢æ°ã®éã§å
±æããç¶æ
ãå€æŽ
var i = 0 while (i < args.length) { if (i != 0) { print (" "); } print (args (i)); i += 1; } println ();
functional paradium
- ååž°ã§å埩æ§é ãå®è¡
- arg ã¯å€æ°ã§ã¯ãªããŠ, äžå€ãªå®æ°
args.foreach (arg => println (arg))
for (arg <- args)
println (arg)
ðé¢æ°åèšèª
ãã¹ãŠã®èšç®ãåŠçãªã©ãé¢æ°ã®å®çŸ©ã®çµã¿åãããšããŠèšè¿°ããŠããã¿ã€ãã®ããã°ã©ãã³ã°èšèª.
- åãå ¥åã«ã¯å¿ ãåãåºåãè¿ã(ðåç §ééæ§).
- é¢æ°ã®è©äŸ¡ãä»ã®é¢æ°ã«åœ±é¿ãåãŒããªã.
ãªã©æ°åŠã«ãããé¢æ°ãšäŒŒãæ§è³ªãæã£ãé¢æ°ã®å®çŸ©ãšããŠããã°ã©ãã³ã°ãè¡ã, ããã°ã©ã ã®å®è¡ã¯èšè¿°ãããé¢æ°çŸ€ã®è©äŸ¡ãšããŠè¡ããã.
åºçŸ©ã®æå³ã§ã¯, What ãã³ã³ãã¥ãŒã¿ã«ç€ºããã® (How ã瀺ããªã). ç矩ã®æå³ã§ã¯, ããã°ã©ãã³ã°ã®äžã§æ°åŠãçšãããã® (Function, Relation).
- åŒãšé¢æ°ã§ããã°ã©ã ãçµã¿äžãã (Use of MathMatics)
- é¢æ°ãå€ãšããŠæ±ãã (Higher-order programming)
- å¯äœçšãèµ·ãããªã (Impliclite State, Stateless)
é¢æ°ã第äžçŽãªããžã§ã¯ãã§ããèšèª.
- ç矩ã®æå³ã§ã¯ Lisp, XPath, Haskell,,,
- åºçŸ©ã®æå³ã§ã¯, Scheme, Clojure, ocame, F#, Scala, Smalltalk, RubyâŠ
ðé¢æ°(Function)
é¢æ°åãã©ãã€ã ã«ãããðé¢æ°ã¯Functionsãšãã°ãã.
é¢æ°ã¯, ããåã®åŒæ°ãä»ã®åã®åŒæ°ã®çµæã«å€æãã. åãšã¯, äºãã«é¢é£ããå€ã®éå.
ref: ããã°ã©ãã³ã° Haskell: Graham Hutton
æ°åŠã«çœ®ããé¢æ°ã®æŠå¿µã«è¿ã. ããéåããéåãžã®åå.
ref: é¢æ° (æ°åŠ) - Wikipedia
æ°åŠãšã®æ¥ç¹ã¯ðã©ã ãæœè±¡ã«ãŸãšãã.
äžå®æ§(Immunity)
å¯äœçšãèµ·ãããªã. ðäžå€æ§(immutable)
ðåç §ééæ§
åç §ééæ§(Referential Transparency).
åŒã®å€ã¯ãã®æ§æèŠçŽ (äŸãã°å€æ°ãé¢æ°)ã«ãã£ãŠã®ã¿å®ãŸã.
- å€æ°ã®å€ã¯æåã«å®çŸ©ããå€ãšåžžã«åã
- é¢æ°ã¯åãå€æ°ãåŒæ°ãšããŠäžããããã°åãå€ãè¿ã.
ðçŽç²é¢æ°(pure function)
åãåŒæ°ãæž¡ãéã, ã©ã®ãããªé çªã§äœåºŠåŒãã§ãåãçµæãè¿ããããªé¢æ°.
åãåŒãè©äŸ¡ãããš, ãã€ãåãçµæã«ãªãåç §ééæ§ãæã£ãŠããããš.
å¯äœçšãããé¢æ°ã®å¯ŸæŠå¿µ.
ðå¯äœçš(Side effect)
ããæ©èœãã³ã³ãã¥ãŒã¿ã® (è«ççãª) ç¶æ ãå€åãã, ãã以éã§åŸãããçµæã«åœ±é¿ãäžããããš.
ãããã¯,
- ç¶æ ãåç §ããããšã§åºåãå€åããããš
- ç¶æ ã«å€åãäžããããšã§åºåãå€åããããš
äŸãšããŠã¯,
- ç Žå£çä»£å ¥
- I/O å¶åŸ¡ (write/print ç)
ç Žå£çä»£å ¥
ä»£å ¥ãšããã®ã¯, ãå³èŸºã«ãããã®ã巊蟺ã«ä»£å ¥ããããšããæå³.
巊蟺ã«ããå€æ°å ã®ããŒã¿ãæ¶ã, æ°ããå³èŸºã«ããããŒã¿ãä»£å ¥ããããšãèšãæããããŸã. ãã®ä»çµã¿ã®ããšããç Žå£çä»£å ¥ããšãã.
Monad |ã¢ãã
(ç解äžååâŠ)
ã¢ãã (ããã°ã©ãã³ã°) - Wikipedia
以äžã®ãããªåé¡ã¯, ã¢ãããšããæŠå¿µã§èª¬æã§ããããã.
- å ¥åºåçãããããããã°ã©ã
- äŸå€ãè¿ãããã°ã©ã
- åŒæ°ã«å¯ŸããŠå€ãè¿ããªã (åæ¢ããªã) ããã°ã©ã
- åãåŒæ°ã§ãè¿ãå€ãç°ãªãå¯èœæ§ã®ããããã°ã©ã
å€ããã³ãã®å€ã䜿ãèšç®ã®äžŠã³ãšãã芳ç¹ããããã°, èšç®ãæ§é å ããæ¹æ³.
-> 詳现㯠Haskell ã®ç« ã«ç§»å.
ãªã¹ãå å è¡šèš | List Comprehensions
ãªã¹ãå å è¡šèš, List Comprehensions.
æ¢åã®éåããæ°ããéåãçæãã.
- çæåš âŠ éåããã®åãåºãããã®å®çŸ©
- ã¬ãŒã ⊠çæããæ¡ä»¶
é«éããã°ã©ãã³ã° | Higher-order programming
é«éããã°ã©ãã³ã°.
é«éé¢æ°(=procedure value) ããµããŒãããŠããèšèªã§ã®ããã°ã©ãã³ã°æè¡.
- é¢æ°ãåŒæ°ãšããŠãããèœå.
- é¢æ°ãæ»ãå€ãšããŠãããèœå.
ããããã¯ããã°ã©ããšããŠ, ããã°ã©ã ã®æ ¹åºã«ããæœè±¡ãã¿ã€ã, ãã匷åãªæœè±¡åãã§ããããã«åªããŠãªããã°ãªããªã.
é«éæç¶ãã®éèŠãã¯, ããã«ããæœè±¡ãããã°ã©ã èšèªã®èŠçŽ ããŠç¢ºãã«è¡šã, ä»ã®èšç®èŠçŽ ãšããŠæ±ããããã«ãªãç¹ã«ãã.
é¢æ°ã«ããã orderãšã¯
åž°çŽçå®çŸ©ã¯ä»¥äž.
- first order: A function whose inputs and output are not functions.
- Nth order: if its inputs and output contain a function of maximum order N.
第äžçŽãªããžã§ã¯ã(first-class object)
ããšãã°çæ, ä»£å ¥, æŒç®, (åŒæ°ã»æ»ãå€ãšããŠã®) åãæž¡ããšãã£ããã®èšèªã«ãããåºæ¬çãªæäœãå¶éãªãã«äœ¿çšã§ãã察象ã®ããš.
第äžçŽãªããžã§ã¯ã - Wikipedia
以äžã®ãããªç¹åŸŽãã〠(é¢æ°ããã°ã©ãã³ã°å®è·µå ¥é)
- ãªãã©ã«ããã
- å®è¡æã«çæã§ãã
- å€æ°ã«å ¥ããŠæ±ãã
- æç¶ããé¢æ°ã®åŒæ°ãšããŠäžããããšãã§ãã
- æç¶ããé¢æ°ã®ã®çµæãšããŠè¿ãããšãã§ãã.
é¢æ°åèšèªãšã¯, é¢æ°ã第äžçŽãªããžã§ã¯ãã§ããããš.
SICP ãã (p43)
- å€æ°ãšããŠååãã€ããããããš
- æç¶ãã«åŒæ°ãšããŠæž¡ãã
- æç¶ãã®çµæãšããŠè¿ããã
- ããŒã¿æ§é ã«çµã¿èŸŒãã
Lisp ã¯æç¶ãã«å®å šãª First Class ãæäžãã.
ð第äžçŽé¢æ°
first-class function.
é¢æ°ã第äžçŽãªããžã§ã¯ããšããŠæ±ãããã°ã©ãã³ã°èšèªã®æ§è³ª.
mapãfilterãªã©ãšé«éé¢æ°ãã€ãã£ãŠããã°ã©ã ãçµãèšèªã«ãšã£ãŠã¯åœããåãããããªãã, é¢æ°åãã©ãã€ã ããå€ãããšããã§ã¯ãªã. ããšãã°C èšèªã«ã¯é¢æ°ãã€ã³ã¿ããã. C èšèªã¯ 第äºçŽãªããžã§ã¯ã. 2 éé¢æ°.
Genericity
åŒæ°ã«é¢æ°ãåãåããã®.
declare
fun {Map F L}
case L of nil then nil
[] H|T then {F H}{Map F T}
end
end
Instantiation
æ»ãå€ã«é¢æ°ãæž¡ããã®.
declare
fun {MakeAdd A}
fun {$ X} X+A end
end
ðé«éé¢æ°
é«éé¢æ°/High-order functions.
第äžçŽé¢æ°ããµããŒãããŠããããã°ã©ãã³ã°èšèªã®ãªãã§ä»¥äžã®ãããããæºããé¢æ°.
- é¢æ°ãåŒæ°ãšããŠãããèœå.
- é¢æ°ãæ»ãå€ãšããŠãããèœå.
é«éããã°ã©ãã³ã°èšèªã ããã£ãŠæžãæ¹ã§é«éé¢æ°ã§ãªããã®ãæžãã.
map/filter/reduceäžé士
é«éé¢æ°ã¯ããããçš®é¡ãããã, ïŒå€§é«éé¢æ°ãšããã°ä»¥äž.
- map
- filter
- reduce
ðmap(é«éé¢æ°)
ãªã¹ãã®åèŠçŽ ã«é¢æ°ãé©çšãã.
Prelude> map (+1) [1,3,5,7]
[2,4,6,8]
ðfilter(é«éé¢æ°)
ãªã¹ãã®åèŠçŽ ã§æ¡ä»¶ã«äžèŽãããã®ãåãåºã.
Prelude> filter even [1..10]
[2,4,6,8,10]
ðreduce(é«éé¢æ°)
ð¡ããã°ã©ãã³ã°Clojureããé«éé¢æ°ïŒåé¡
é«éããã°ã©ãã³ã°ã§ãã䜿ãããé¢æ°, èšèªã«ãã£ãŠåŸ®åŠã«ååãéã£ãããããã®ã®, åããããªæ©èœãšååã®é¢æ°ãããã.
ðããã°ã©ãã³ã°Clojureã§ã¯, ã·ãŒã±ã³ã¹ã©ã€ãã©ãªã4åé¡ããŠãã.
- ã·ãŒã±ã³ã¹ãçæããé¢æ°(range/itreate/take)
- ã·ãŒã±ã³ã¹ããã£ã«ã¿ããé¢æ°(filter)
- ã·ãŒã±ã³ã¹ã«å¯Ÿããè¿°èª(every?)
- ã·ãŒã±ã³ã¹ãå€æããé¢æ°(map/reduce)
References
ðç¡åé¢æ°
ç¡åé¢æ°(Annonimous Functions), ååä»ããããã«å®çŸ©ãããé¢æ°.
Function Literal (é¢æ°ãªãã©ã«), å¿åé¢æ°ãšããããããšããã.
ã¡ãªããã¯,
- äžåºŠãã䜿ããªãé¢æ°ã®ååãä»ããªããŠæžã.
- ååã®è¡çªãèããªããŠæžã.
- é¢æ°ã®åŒæ°ãªã©ã«çŽæ¥æž¡ãã
examples:
- Ruby {|x, y| x + y}
- Scala (x :Int, y :Int) => x + y , (x, y) => x + y
- Haskell \ x y -> x + y
ã¯ããŒãžã£(Closure)
åŒæ°ä»¥å€ã®å€æ°ãå®è¡æã®ç°å¢ã§ã¯ãªã, èªèº«ãå®çŸ©ãããç°å¢(Static Scope)ã«ãããŠè§£æ±ºãã.
Procedure Value (Oz), Lexical Scoped Closure ãšããã.
é¢æ°ãšãããè©äŸ¡ããç°å¢ã®ãã¢ãšãèšãã.
Procedure value 㯠ãã¢ã§ã¡ã¢ãªäžã®å€ã«ãã€ã³ãããã.
- Procedure code
- Contextual environment
Javaã®ç¡åã¯ã©ã¹ãã¯ããŒãžã£ã®äžçš®. Rubyã ãš ãããã¯(do - end)ãšããè¡šçŸã§ç»å Žãã. å¿ ãããè¡šçŸãç¡åé¢æ°ã§ã¯ãªã.
Contextual environments
é¢æ°ã®å éšã§åç §ãããŠããŠ, é¢æ°ã®å€éšã§å®£èšãããŠãããã¹ãŠã®èå¥åã®éåã, ãã®é¢æ°ã® contextual environments ãšãã.
é¢æ°ãªããžã§ã¯ã
é¢æ°ããªããžã§ã¯ããšãããã®.
note: Java7以åã§ããç»å ŽããããªïŒä»ã¯ãããªããª.
cf. ã¯ããŒãžã£ã¯é¢æ°ãªããžã§ã¯ããšç°å¢ã®ãã¢.
ã©ã ãåŒ
- Ruby: lambda{|x, y| x + y}
- Scala:
- Haskell:
ããªã²ãŒã
ãªããžã§ã¯ããžã®åç §ãšé¢æ°ãªããžã§ã¯ããžã®åç §ããã¢ã«ããŠæã€ãã®.
C#, Visual Basic .NET ãªã©ã®, .NET Framework ã®ããã°ã©ãã³ã°èšèªã«ããæ©èœ.
éšåé©çš
äžéšã®åŒæ°ãåºå®åããŠæ°ããé¢æ°ãäœãåºãããšãéšåé©çšãšåŒã¶.
ã«ãªãŒå | Currying
ã«ãªãŒå. è€æ°ã®åŒæ°ããšãé¢æ°ã以äžã§ãããããªé¢æ°ã«ããããš.
- åŒæ°ããããšã®é¢æ°ã®æåã®åŒæ°ãã§
- æ»ãå€ããããšã®é¢æ°ã®æ®ãã®åŒæ°ãåãçµæãè¿ãé¢æ°ã
éšåé©çšã容æã«ããããšãå¯èœã«ãªããšããã¡ãªããããã.
- refs.
ð¡ã«ãªãŒåãšéšåé©çšã®éã
ããæ··åããããã. çããããããã«ããããšãã.
- é¢æ°ãåŒæ°1ã€ãã€ã«åå²ããŠãã¹ããããããšãã«ãªãŒåãšåŒã¶.
- äžéšã®åŒæ°ãåºå®åããŠæ°ããé¢æ°ãäœãåºãããšãéšåé©çšãšåŒã¶.
ã«ãªãŒåã¯å ã®é¢æ°ã®è¡šçŸãå€ããã«ãããªã. ã«ãªãŒãªè¡šçŸ. äžæ¹, éšåé©çšã¯ãã¯ãå¥ã®é¢æ°ãæ°ãã«çæããŠãã.
ã«ãªãŒåãéšåé©çšã¯HaskellãJavaScriptã®äŸã§æ€çŽ¢ã§ããã¿ããã. Haskellã¯ããã©ã«ãã§åŒæ°ã¯äžã€ã§ãã, ããããããè¡šçŸã®ããã«è€æ°åŒæ°ã§è¡šçŸãããšããŠãå éšã®åŠçã§ã¯ã«ãªãŒåãããŠåŠçããã(automatic currying/auto-curryingãšããïŒ).
ref: ã«ãªãŒåãšéšåé©çšã®éããšèª€çš - Togetter
x,y,z -> V ãx -> (y->(z->V)) ã«å€æããã®ãã«ãªãŒåãx,y,z-> Vã®yã«å€ãæçžããŠçµæçã«x,z->Vãšããé¢æ°ã«ãªãã®ãéšåé©çšãã©ããåããªãã ãããã
ãªãæ··åãããã¯ããäžç·ã«ç»å ŽãããããïŒã«ãªãŒã§è¡šçŸãããé¢æ°ããå¥ã®é¢æ°ãã€ãã. ãããWikipediaã«ãããµãé ç®ãšããŠãããã¯ããã. éšåé©çšãšã®æ··å.
ðã¢ãªãã£(arity)
é¢æ°ãåãããåŒæ°ã®åæ°. é¢æ°åãã©ãã€ã ãèšç®æ©ç§åŠã§ãã䜿ããã.
ðååž°çããã°ã©ãã³ã°(recursion)
Recursion, ååž°çããã°ã©ãã³ã°.
ðæ«å°Ÿååž°(tail recursion)
tail-recursion.
ãã®äžã«ãã 1 ã€ã®ååž°åŒã³åºãããã, ãã€ãã®åŒã³åºããæç¶ãæ¬äœã®æåŸã«ãããã®.
é¢æ°ãããèªèº«ãæåŸã®åŠçã§åŒã³ãã€, é¢æ°ã®ã¹ã¿ãã¯ãåå©çšããããã®.
tail-recursion ã®äŸ. Factorial
declare
fun {Fact N}
local Fact1 in
% tail-recursive ã§ãªã
% èšç®ã®ãã³ã«ã¹ã¿ãã¯ãããŸã.
fun{Fact1 N}
if N==1 then 1
else N*{Fact1 N-1}
end
end
local Aux in
% tail-recursive
% èšç®ã®ãã³ã«ã¹ã¿ãã¯ãããŸããªã.
fun {Aux N Acc}
if N==0 then Acc
else {Aux N-1 {Fact1 N}|Acc} % call Fact on N here!!!
end
end
{Aux N nil}
end
end
end
TCO(Tail Call Optimization)
æ«å°ŸåŒã³åºãæé©å
State pattern
é¢æ°åãã©ãã€ã ã§ã®å®è£
fun {While S}
if {isDone S} then S
else {While {Transform S}} end /* tail recursion */
end
æç¶ãåãã©ãã€ã ã§ã®å®è£
state whileLoop (state s) {
while (!isDone (s)) // çµäºæ¡ä»¶
s = transform (s) // ååž°
return s;
}
ðAccumulator
loopã®æ¬¡ã®åŒæ°ã«æž¡ãããå€. ããaccãšãçç¥ããããã€.
See more
Type: å
Algebraic data type: 代æ°ããŒã¿å
é¢æ°åãã©ãã€ã ã§å©çšããã.
ããããã®ä»£æ°çããŒã¿åã®å€ã«ã¯,以äžããã£ãŠãã.
- 1 å以äžã®ã³ã³ã¹ãã©ã¯ã¿
- åã³ã³ã¹ãã©ã¯ã¿ã«ã¯ 0 å以äžã®åŒæ°
2 åŒæ°ã§äžããããä»ã®ããŒã¿åã®å€ã, ã³ã³ã¹ãã©ã¯ã¿ã§å ãã ãããªãã®.
Enum: åæå
ããã°ã©ããéžãã åã ã®èå¥åããã®ãŸãŸæééåãšããŠæã€æœè±¡ããŒã¿å.
çªå·ãæããªãã«ããŽãªå€æ°. äžæã®æå.
å®è¡æã«ã¯, çªå·ãæ¯ãããããšãèŠãã, èšèªã«ãã£ãŠã¯çªå·ã¯ããã°ã©ãã«èŠããªãããšããã.
Monadic Programming
ã¢ãããäžå¿ã«ããã°ã©ã ãçµãæ¹æ³.
ã¢ãããšã¯,
- ã³ã³ãã
- ãã€ãã©ã€ã³
- ã€ã³ã¿ããªã¿
ã¢ããã«ã¯ãããããªçš®é¡ããã.
- IO ã¢ãã
- State ã¢ãã
- Future ã¢ãã
ã¢ããã®äœ¿ãæ¹ã¯é£ããã®ã ããã©ããã¿ãŒã³ãããã®ã§ãªããã°ç°¡å.
Object-Functional Programming(OFP)
ãªããã®ã»ãããŒã«åå ãããšãã®æã®ã¡ã¢ã ãªâŠ æ°æšªæµã«ãããããã£ãæ°ããã.
ãªããžã§ã¯ãæåã®ãã©ãã€ã ãšé¢æ°åã®ãã©ãã€ã ã®äž¡æ¹ãå©çšããŠããã°ã©ãã³ã°ãã.
- ãªããžã§ã¯ãæåéçºã«ããã Object-Functional Programming
- Object-Functional Programming â A Beautiful Unification or a Kitchen Sink? // Speaker Deck
äžæµå·¥çšã§ã¯, ä»ãŸã§ã©ãããªããžã§ã¯ãæåèšèšã§èããããšã«ãªã. ãŠãŒã¹ã±ãŒã¹ã§ä»ãŸã§ã©ããèŠä»¶å®çŸ©ãããŠ, ã³ã³ããŒãã³ãåå²ãŸã§ãã. ãããã, ãªããžã§ã¯ãããã¡ã³ã¯ã·ã§ã³ã®ã©ã¡ããã€ãã£ãŠè²¬åãå®çŸãã. ãªã®ã§, OOP ãš FP ã¯å ±åé¢ä¿ã«ãã.
OFP æ°äžçš®ã®ç¥åš.
- ãã¬ã€ã
- ã¢ãã
- åã¯ã©ã¹
OFP ãå°å ¥ããããšã¡ãªããã¯, 以äž.
- é«éé¢æ° ã DSL ãæžãããšã§ éçºå¹ç ãããã
- Monadic Programming ãè¡ãããšã§äžŠååŠçã®å質ãããã
ã©ãã« Functional Programming ãé©çšããã?
Functinal Programming ã§æžããš, ãã°ãåºã«ããã®ã§, Functonal Programming ã®å²åãã§ããã ãå¢ãããŠããã®ããã¹ã.
ã·ã¹ãã éçºã§ã¯, OO:FP ã®å²å㯠6:4 ãããã??
FP ã§ã€ããã®ã«é©ããéšåã¯, DSL ã®éšå. OOP ã§, Framework ãšåŒã°ããŠããéšå.
ã¢ããªéçºã¯ Java ã§ããã. ã¢ããªéçºã®åºç€ã«ãã DSL éšåã é¢æ°åã§ãã.
DSL
DSL ãšã¯,ç¹å®ã®ã¿ã¹ã¯åãã«èšèšãããã³ã³ãã¥ãŒã¿èšèª. DSL ã¯äžçš®é¡ã®ã¿ã¹ã¯ãããŸãå®è¡ããããšã«éäžãããã®.
ãããŠ, FP (ãšããããã Scala) ã¯, DSL ãæžãããšã« é©ããŠãã (Scalable language). ãªããªã, ç°¡åã«ç¬èªã®åãå¶åŸ¡æ§é ãå®çŸ©ã§ããã®ã§.
Functional Programming Patterns
Based on bellows.
- Functional Programming Patterns (BuildStuff â14)
- http://fsharpforfunandprofit.com/fppatterns/
- http://www.ndcvideos.com/#/app/video/2311
ðæ§é åãã¿ãŒã³ããã(Structural Pattern Matching)
Recursion Pattern
list åã®ããŒã¿æ§é ãæ±ããšãã®ææ³.
see more: æ«å°Ÿååž°.
Functional Design Patterns
é¢æ°ããã°ã©ãã³ã°ã§ããã¿ããããã¿ãŒã³ãClojureã§è§£èª¬.
Functional Laws
Based on Brian Lonsdorfâs Great Presentation.
- ⶠFunctional programming patterns for the non-mathematician (cut) - YouTube
- https://github.com/DrBoolean/patterns_talk
- http://www.slideshare.net/drboolean/functional-patterns-for-the-nonmathematician
- http://vimeo.com/user7981506
Composition laws
Currying
Lenses laws
A function that acts as both a getter and setter.
Fmap laws
Null checking
Error handling
Monad laws
Future values
Functor
Nesting
Applicative laws
Monoid laws
Accumulation
Monoid
Arrow laws
Combinators
Arrows
Insights
- âšãªããžã§ã¯ãæåã§ã¯ãªãé¢æ°åã§ç©çäžçããœãããŠã§ã¢æœè±¡ããã®ããŽã£ãããµããŒãµããŒãã£
- æ°åŠã§ã®é¢æ°ãšããã°ã©ãã³ã°ã§ã®é¢æ° | Futurismo
References
é¢æ°åèšèªã§ããã°ã©ãã³ã°ããããšã§, åŠçã¯, ããŒã¿ãåž°çŽçã«å®çŸ©åºæ¥ãããšã, ããããã®èå³æ·±ãã¢ããªã±ãŒã·ã§ã³ãåºæ¬çã«ããŒã¿åã®ãã¿ãŒã³ãããã䜿ã£ãŠããããšã, ã³ãŒãã¯æ¬è³ªçã«ããŒã¿ãšã¯ç°ãªãããšã, å¯äœçšãæå°éã«æããããšã§é£çµã楜ã«ãªãããšãªã©, éèŠãªèŠèãåºããŸã. ãããã¯äŸãããªãã Java ã C++ ã§ããã°ã©ãã³ã°ããã€ããã§ãã£ããšããŠãæçšãªèŠèãªã®ã§ã