Giter VIP home page Giter VIP logo

Comments (25)

liuxinqiong avatar liuxinqiong commented on May 2, 2024 10

不觉明厉,成长的路任重道远

from blog.

xuchaobei avatar xuchaobei commented on May 2, 2024 9

给个redux中的实现,很简洁:

function compose(...funcs) {
  if (funcs.length === 0) {
    return arg => arg
  }

  if (funcs.length === 1) {
    return funcs[0]
  }

  return funcs.reduce((a, b) => (...args) => a(b(...args)))
}

from blog.

eltonchan avatar eltonchan commented on May 2, 2024 4

const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));

from blog.

Aisen60 avatar Aisen60 commented on May 2, 2024 2
const compose = (...args) => value => args.reverse().reduce((acc, fn) => fn(acc), value)

from blog.

liuestc avatar liuestc commented on May 2, 2024 1

虽然很优雅,但是我觉得要是实际业务中用ramda的话,不熟的人接手会一脸蒙逼啊

from blog.

lonfger avatar lonfger commented on May 2, 2024 1

最近这几篇深深的感受到脑子不够用了

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@liuxinqiong 对呀,我也是这种感叹!😂

from blog.

yangchongduo avatar yangchongduo commented on May 2, 2024

高阶函数呢? 你有项目是使用函数式编程写的吗?

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@yangchongduo 柯里化就是用高阶函数实现的呀,我没有在项目中用到函数式编程,这主要是因为我对于函数式编程掌握的依然不熟练,对于更深层次的如 Monad 依然没有掌握

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@xuchaobei 感谢补充~

from blog.

xue1234jun avatar xue1234jun commented on May 2, 2024

image

from blog.

mqyqingfeng avatar mqyqingfeng commented on May 2, 2024

@xue1234jun 感谢指出,已经更改~

from blog.

 avatar commented on May 2, 2024

看Learning React时突然想起这个教程,里面有个用ES6语法写的compose函数,想了半天突然发现就是这个教程里的这个概念啊 😎

const compose = (...fns) =>  
  (arg) =>  
    fns.reduce(  
      (composed, f) => f(compose),  
      arg  
    )  

from blog.

aikeProject avatar aikeProject commented on May 2, 2024

脑袋疼

from blog.

uuidzyl avatar uuidzyl commented on May 2, 2024
let compose = (...fns) => (arg) => {
    return dispatch(0)
    function dispatch(index) {
        arg = fns[index](arg)
        if (index === fns.length - 1) return arg
        else return dispatch(++index)
    }
}

from blog.

18355166248 avatar 18355166248 commented on May 2, 2024

看了半天 这个 函数式编程**很不错 compose结合柯里化很有意思 学习了

from blog.

HuangQiii avatar HuangQiii commented on May 2, 2024

补充一个非常常见的场景,react中的HOC组合

调用很多HOC时,代码如下:

withRoute(observer(inject('Store')(Index)))

可以预想,当HOC更多时会变得非常难以维护,可以用compose进行一定的阅读性提升

const enhance = compose(withRoute, observer, inject('Store'));

enhance(Index);

因为HOC的本质就是接受一个组件并且返回一个组件的函数!

当然如果用装饰器就更方便了~

@inject('Store')
@withRoute
@observer
export default class Index extends Component {
  ...
  ...
}

from blog.

shmmly avatar shmmly commented on May 2, 2024

@HuangQiii 如果使用hooks 就没法用装饰器了 还是老老实实的hoc吧 =。= 悲剧

from blog.

yadongxie150 avatar yadongxie150 commented on May 2, 2024

专题很优秀,学到不少知识,感觉楼主的分享,期待其他分类

from blog.

xch1029 avatar xch1029 commented on May 2, 2024

这篇知识量有点大,在lodash中有类似compose的函数吗

from blog.

Vuact avatar Vuact commented on May 2, 2024

compose这么写更好些,注意用reduceRight

const compose = (...funcs) => {
  return (val) => {
    return funcs.reduceRight((a, b) => b(a), val);
  };
};

from blog.

anjina avatar anjina commented on May 2, 2024

函数式编程 感觉更适合 用在底层都一些框架 ,库之类的编写上, 对于业务来讲,代码不太好看懂

from blog.

qiugu avatar qiugu commented on May 2, 2024

pointfree这个概念,大佬从哪里看来的,厉害呀

from blog.

justorez avatar justorez commented on May 2, 2024

感觉从右到左有点反直觉啊。评论里很多 reduce 实现,需要注意处理 this

from blog.

justorez avatar justorez commented on May 2, 2024

pointfree这个概念,大佬从哪里看来的,厉害呀

https://www.ruanyifeng.com/blog/2017/03/pointfree.html

from blog.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.