Comments (27)
@evanzlj 我现在维护三个平台就已经有点忙不过来啦,公众号就更麻烦了,而且我觉得还是 PC 端更适合学习一些,可以边看文章边动手试验代码。
from blog.
大佬,这个偏函数是不是有点问题,subFrom20(5),subFrom20(4)都输出15,那个args被污染拉,是不是该拷贝一份
from blog.
while(position < arguments.length) args.push(arguments[position++])
应该改为:
while(position < arguments.length) args[position]=(arguments[position++]);
@kashtian
from blog.
大佬有公众号吗
from blog.
@syzq 没有哎~
from blog.
第二版 while(position < arguments.length) args.push(argumetns[position++]); 中后一个arguments拼成argumetns了
from blog.
@SilenceZeng 非常感谢指出~ 已修改~ o( ̄▽ ̄)d
from blog.
总结得很棒!
from blog.
大佬开个公众号吧,这样可以有更大的动力~~ 赏你一杯咖啡,哈哈~~
from blog.
可不可以这样理解,偏函数的一种特殊情况就是curry
from blog.
那这样说来Function.prototype.bind
也是偏函数咯
from blog.
var _ = {};
args[i] === _
这样写是有什么奥妙么
from blog.
es6简化版
function partial(func, ...argsBound) {
return function(...args) {
return func.call(this, ...argsBound, ...args);
};
}
from blog.
大佬,这个偏函数是不是有点问题,subFrom20(5),subFrom20(4)都输出15,那个args被污染拉,是不是该拷贝一份
你写错了吧。没有被污染啊
from blog.
占位符用在里面有什么用
from blog.
while(position < arguments.length) args.push(arguments[position++]);
直接换成 下面这样会不会好点
if(position < arguments.length) args = args.concat([].slice.call(arguments, position));
from blog.
while(position < arguments.length) args.push(arguments[position++]);直接换成 下面这样会不会好点
if(position < arguments.length) args = args.concat([].slice.call(arguments, position));
不一样吧,原文的意思是:将arguments[position]这个值放入到args中;你改后的意思是:将arguments从position位置截取到数组最后一项的所有值放入到args中;原文放入了一个值,你放入了从position位置到最后一个位置的好几个值
from blog.
强烈建议大佬把博客文章整理一下搞个类似阮一峰老师的ES6微信小程序
from blog.
您好,我最近一直在学习您编写的文章,在这一章中我实在分辨不出柯里化和偏函数的区别,还有能说明下这两者的应用场景吗?
from blog.
// 第一版
// 似曾相识的代码
function partial(fn) {
var args = [].slice.call(arguments, 1);
return function() {
var newArgs = args.concat([].slice.call(arguments));
return fn.apply(this, newArgs);
};
};
这段就是bind的实现呀 所以可以理解成 Function.prototype.bind函数也是一个偏函数啦
from blog.
while(position < arguments.length) args.push(arguments[position++]);直接换成 下面这样会不会好点
if(position < arguments.length) args = args.concat([].slice.call(arguments, position));不一样吧,原文的意思是:将arguments[position]这个值放入到args中;你改后的意思是:将arguments从position位置截取到数组最后一项的所有值放入到args中;原文放入了一个值,你放入了从position位置到最后一个位置的好几个值
你可能没有注意到 第二个替换 while 改成了 if
from blog.
使用ts写偏函数,不过类型出于考虑,只用了any
function partical(fn: Function, ...args: any[]): Function {
return function (...moreArgs: any[]) {
return fn(...args, ...moreArgs)
}
}
from blog.
更加函数式的实现:
const _ = Symbol('_')
const partial = (fn, ...outerArgs) => {
return function (...innerArgs) {
return fn.call(this, ...outerArgs.map(arg => arg === _ ? innerArgs.shift() : arg), ...innerArgs)
}
}
partial._ = _
from blog.
函数式的实现:
const _ = Symbol('_') const partial = (fn, ...outerArgs) => { return function (...innerArgs) { return fn.call(this, ...outerArgs.map(arg => arg === _ ? innerArgs.shift() : arg), ...innerArgs) } } partial._ = _
我认为应该这样比较好,占位符可以由编写成员自定义
function partial(fn, holder = partial, ...args) {
return function (...params) {
return fn.call(this, ...args.map((arg) => (arg === holder ? (arg = params.shift()) : arg)), ...params)
}
}~~~
这样看起来就完美了
from blog.
“局部应用”翻译也有问题。逻辑上讲应该是 applied partial function
, 只是英语语法调整成了partially applied function
。正确翻译应该是 “已被传递(applied)部分(partial)(参数)(被省略)的函数”,简称不如为“部分定参函数”
from blog.
while(position < arguments.length) args.push(arguments[position++]);
大佬,这个有点看不懂可以解释一下嘛
from blog.
大佬 bind 和 局部应用 晕了 不太明白呢
from blog.
Related Issues (20)
- 大佬您好,看完大有收获,但是遇到了个问题,不太会解释它的执行上下文入栈出栈顺序,关于尾递归,顺序是怎样呢? HOT 1
- 冴羽答读者问:如何在工作中打造影响力,带动同事?
- 无
- 冴羽答读者问:如何学习更有计划性、提升更稳更快? 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.