<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                [TOC] ## 函數飼養 - 兩個函數組合之后返回了一個新函數 - 在`compose`的定義中,`g`將先于`f`執行,因此就創建了一個從右到左的數據流。這樣做的可讀性遠遠高于嵌套一大堆的函數調用 - 讓代碼從右向左運行,而不是由內而外運行,我覺得可以稱之為“左傾”,盡管我們可以定義一個從左向右的版本,但是從右向左執行更加能夠反映數學上的含義 js版 ``` var compose =function(f,g){ return function(x){ return f(g(x)) } } var append1=x=>x+"_1" var append2=x=>x+"_2" var c = compose(append1,append2) console.log(c("ads")); //ads_2_1 ``` 通用 funcs compose ``` const compose =function (...funcs) { return function (arg) { var tmp=arg for (let i = funcs.length-1; i >=0; i--) { tmp = funcs[i]( tmp); } return tmp } } const add1=x=>x+"_1" const add2=x=>x+"_2" const add3=x=>x+"_3" const c = compose(add1,add2,add3) console.log(c("hello")); // hello_3_2_1 ``` golang 版 ``` type composeFun func(string)string func compose(g composeFun,f composeFun) composeFun { return func(a string) string { return g(f(a)) } } func main() { var a = func(a string) string{ return a+"_1" } var b = func(a string) string{ return a+"_2" } c :=compose(a,b) fmt.Printf("%+v\n", c("hello")) // hello_2_1 } ``` 如果不使用compose ``` var shout = function(x){ return exclaim(toUpperCase(x)); }; ``` ## 結合律 ``` var associative = compose(f, compose(g, h)) == compose(compose(f, g), h); ``` 如 ``` compose(toUpperCase, compose(head, reverse)); // 或者 compose(compose(toUpperCase, head), reverse); var loudLastUpper = compose(exclaim, toUpperCase, head, reverse); // 或 var last = compose(head, reverse); var loudLastUpper = compose(exclaim, toUpperCase, last); // 或 var last = compose(head, reverse); var angry = compose(exclaim, toUpperCase); var loudLastUpper = compose(angry, last); // 更多變種... ``` ## pointfree pointfree 模式指的是,永遠不必說出你的數據,無須提及將要操作的數據是什么樣的 ``` // 非 pointfree,因為提到了數據:word var snakeCase = function (word) { return word.toLowerCase().replace(/\s+/ig, '_'); }; // pointfree var snakeCase = compose(replace(/\s+/ig, '_'), toLowerCase); ``` 另一個例子 ``` // 非 pointfree,因為提到了數據:name var initials = function (name) { return name.split(' ').map(compose(toUpperCase, head)).join('. '); }; // pointfree var initials = compose(join('. '), map(compose(toUpperCase, head)), split(' ')); initials("hunter stockton thompson"); // 'H. S. T' ``` ## debug ``` var trace = curry(function(tag, x){ console.log(tag, x); return x; }); var dasherize = compose(join('-'), toLower, split(' '), replace(/\s{2,}/ig, ' ')); dasherize('The world is a vampire'); // TypeError: Cannot read property 'apply' of undefined var dasherize = compose(join('-'), toLower, trace("after split"), split(' '), replace(/\s{2,}/ig, ' ')); // after split [ 'The', 'world', 'is', 'a', 'vampire' ] // toLower 的參數是一個數組,所以需要先用 map 調用一下它 // 修復 var dasherize = compose(join('-'), map(toLower), split(' '), replace(/\s{2,}/ig, ' ')); dasherize('The world is a vampire'); // 'the-world-is-a-vampire' ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看