zoukankan      html  css  js  c++  java
  • 函数式编程compose 与Box

    const compose = (...fns) =>fns.reduce((a, b) =>(...x)=> b(a(x)));
            const trim1 = str => {
                console.log(str)
                let s = str.trim()
                return s;
            }
            const toNumber = str =>{
                console.log('3333')
                return parseInt(str)
            } 
            const nextNumber = number =>{
                console.log('2222')
                return number + 1;
            } 
            const createStr = number =>{
                console.log('1111')
                return String.fromCharCode(number);
            } 
            const nextChartFromNumberString = compose(toNumber,nextNumber,createStr);
            const result = nextChartFromNumberString(' 64');
            console.log(result)
     
     
    const Box = x => ({
                super: f => Box(f(x)),
                inspect: () => `${x}`
            })
            const nextChartFromNumberString = str => Box(str)
            .super(s => s.trim()).super(r => parseInt(r)).super(i => i + 1).super(i => String.fromCharCode(i))
            const result = nextChartFromNumberString(' 64').inspect()
            console.log(result)
    两种方式所用的到方法不一样,可以体会一下函数式编程
  • 相关阅读:
    Codeforces Round #239(Div. 2) 做后扯淡玩
    hdu 3507 Print Article
    prufer序列
    POJ 2778 DNA Sequence
    Codeforces Round #237 (Div. 2)
    poj3352
    图论知识
    POJ 2186
    Codeforces Round #236 (Div. 2)
    POJ 2823 Sliding Window
  • 原文地址:https://www.cnblogs.com/MDGE/p/14706176.html
Copyright © 2011-2022 走看看