函数式编程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)
两种方式所用的到方法不一样,可以体会一下函数式编程
posted @ 2021-04-26 19:38  国服第一李师师  阅读(242)  评论(0编辑  收藏  举报