Comments (25)
不觉明厉,成长的路任重道远
from blog.
给个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.
const compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));
from blog.
const compose = (...args) => value => args.reverse().reduce((acc, fn) => fn(acc), value)
from blog.
虽然很优雅,但是我觉得要是实际业务中用ramda的话,不熟的人接手会一脸蒙逼啊
from blog.
最近这几篇深深的感受到脑子不够用了
from blog.
@liuxinqiong 对呀,我也是这种感叹!😂
from blog.
高阶函数呢? 你有项目是使用函数式编程写的吗?
from blog.
@yangchongduo 柯里化就是用高阶函数实现的呀,我没有在项目中用到函数式编程,这主要是因为我对于函数式编程掌握的依然不熟练,对于更深层次的如 Monad 依然没有掌握
from blog.
@xuchaobei 感谢补充~
from blog.
from blog.
@xue1234jun 感谢指出,已经更改~
from blog.
看Learning React时突然想起这个教程,里面有个用ES6语法写的compose函数,想了半天突然发现就是这个教程里的这个概念啊 😎
const compose = (...fns) =>
(arg) =>
fns.reduce(
(composed, f) => f(compose),
arg
)
from blog.
脑袋疼
from blog.
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.
看了半天 这个 函数式编程**很不错 compose结合柯里化很有意思 学习了
from blog.
补充一个非常常见的场景,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.
@HuangQiii 如果使用hooks 就没法用装饰器了 还是老老实实的hoc吧 =。= 悲剧
from blog.
专题很优秀,学到不少知识,感觉楼主的分享,期待其他分类
from blog.
这篇知识量有点大,在lodash中有类似compose的函数吗
from blog.
compose这么写更好些,注意用reduceRight
const compose = (...funcs) => {
return (val) => {
return funcs.reduceRight((a, b) => b(a), val);
};
};
from blog.
函数式编程 感觉更适合 用在底层都一些框架 ,库之类的编写上, 对于业务来讲,代码不太好看懂
from blog.
pointfree这个概念,大佬从哪里看来的,厉害呀
from blog.
感觉从右到左有点反直觉啊。评论里很多 reduce 实现,需要注意处理 this
from blog.
pointfree这个概念,大佬从哪里看来的,厉害呀
https://www.ruanyifeng.com/blog/2017/03/pointfree.html
from blog.
Related Issues (20)
- 冴羽答读者问:如何在工作中打造影响力,带动同事?
- 无
- 冴羽答读者问:如何学习更有计划性、提升更稳更快? HOT 4
- 冴羽答读者问:过程比结果重要吗? HOT 1
- 冴羽答读者问:冴羽,你为什么写起了鸡汤? HOT 3
- 聊聊 npm 的语义化版本(Semver) HOT 4
- How to create Backlinks 😤
- 思考题第2题
- 可以理解为原型是prototype,原型链是通过__proto__ 链接起来的吗
- React 之 createElement 源码解读 HOT 8
- React 之元素与组件的区别 HOT 1
- React 之 Refs 的使用和 forwardRef 的源码解读
- React 之 Context 的变迁与背后实现 HOT 1
- 第一段不报错啊,刚试过了,会打印1
- how can i HOT 1
- Hosting of Blog Issue
- 全局对象
- 为啥每次都要创建一个 Child函数来new 子类?现在不都是 const p1 = new Person(); const p2 = new Person()吗
- 文档内容中文件结构的错位 HOT 1
- 原型链继承
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from blog.