lower order function 可以用higher order function嘅variable,咁我當佢係一個scoping問題。
statement
1. declare statement(即係const,var,let)用const同let,因為var會較容易有side effect(畀人改到)。
2. conditional statement(if/switch)最好就有加返個else/default,咁個function就一定有嘢會retrun。
3. loop statement 應該避免直接用for loop 因為會比較容易有side-effect。 用一pure function去代替會比較好(example forEach)。
4. disruptive statement(break, continue, skip, return)但通常只係會用return。
不過關於呢啲咁嘅用法, 我覺得就應該唔會用到咁details。 因為應該冇乜機會寫純functional programming。
Recursion
關於呢part我會暫時skip先, 因為呢一part係重視algorithm。 例如depth first search, breath first search(finding shortest path)呢一啲recusive algo我淨係識講,但係唔識點樣運用。
pipeline
呢個好似middleware咁,pass一個function result入落另一個function度。
normal function可以 import pipe from lodash 去 link up啲functions
如果係object 都可以用dot operator 去 link up
example
object.filter(func1).map(func2)
currying
可以 import curry from lodash
係一個clourse function + higher order function嘅混合體。即係一個function會return 第二個function。
curry function係得unary function(only one parameter)
partial application
大至上類似curry,不過唯一分別partial application容許function可以多過一個parameters去做passing。
Day 17
我研究了幾天functor。但是,我好多source去幫助我理解它的概念。因此,今天的內容可能會有錯,同埋都唔係好知點樣去解釋。如果您發現任何錯誤,請糾正我。
咁我嘅理解係:
Functor is an object that can be mapped over。
同埋要滿足兩個rules。
Functors must preserve identity morphisms
Functors preserve composition of morphisms
即係似composition-functions
https://www.math24.net/composition-functions
咁JavaScipt 有嘅Functor應該係得Array
https://ithelp.ithome.com.tw/articles/10240162?sc=rss.qu
https://www.codingame.com/playgrounds/2980/practical-introduction-to-functional-programming-with-js/functors-and-monads
所以我跟咗個網搵埋Haskell。咁Haskell因為會有多幾個type, e.g. Either, Maybe
咁睇 [] 是 Functor
fmap :: (a -> b) -> [a] -> [b]
如果我冇記錯
fmap -- name of function
(a-> b) -> [a] -- input parameter type
(a->b) -- input function
[a] -- input array
[b] -- output array
https://blog.csdn.net/zwvista/article/details/54766541
如果有啲師兄知道一個正確嘅解釋,麻煩你下面回覆。