[英]Understanding Ramda.js
Question 1: 问题1:
var _curry1 = function _curry1(fn) {
return function f1(a) {
if (arguments.length === 0) {
return f1;
} else if (a != null && a['@@functional/placeholder'] === true) {
return f1;
} else {
return fn.apply(this, arguments);
}
};
};
What is the purpose of checking a['@@functional/placeholder'] === true
? 检查
a['@@functional/placeholder'] === true
的目的是什么?
Question 2: 问题2:
http://ramdajs.com/0.18.0/docs/#reduce http://ramdajs.com/0.18.0/docs/#reduce
How do I read the notation? 如何阅读符号?
(a,b -> a) -> a -> [b] -> a
This is my first time seeing such notation, where does it come from? 这是我第一次看到这种表示法,它是从哪里来的?
Question 1: 问题1:
There is no "notation". 没有“符号”。 __.js should clear it up:
__。js应该清除它:
module.exports = {'@@functional/placeholder': true};
so @@functional/placeholder
is no different than foo
in 所以
@@functional/placeholder
与foo
中的没有什么不同
a = { foo: true }
a.foo
a["foo"]
(Obviously, you can't write a.@@functional/placeholder
because of all the odd symbols there.) (显然,由于那里所有的奇数符号,您不能编写
a.@@functional/placeholder
。)
The intent is also seen in that file: 该文件中也可以看到该意图:
/**
* A special placeholder value used to specify "gaps" within curried functions,
* allowing partial application of any combination of arguments,
* regardless of their positions.
*
* If `g` is a curried ternary function and `_` is `R.__`, the following are equivalent:
*
* - `g(1, 2, 3)`
* - `g(_, 2, 3)(1)`
* - `g(_, _, 3)(1)(2)`
* - `g(_, _, 3)(1, 2)`
* - `g(_, 2, _)(1, 3)`
* - `g(_, 2)(1)(3)`
* - `g(_, 2)(1, 3)`
* - `g(_, 2)(_, 3)(1)`
...
So the intent is to be able to "skip" some places when currying. 因此,其目的是能够在进行curry时“跳过”某些地方。 The test decides whether an argument is a real argument or the
__.js
placeholder, and behaves accordingly. 测试确定参数是真实参数还是
__.js
占位符,并相应地执行操作。 Why it is @@functional/placeholder
- presumably precisely because it is hoped that it is too weird, and will thus not collide with anyone's legitimate data. 为什么使用
@@functional/placeholder
大概正是因为希望它太怪异,从而不会与任何人的合法数据发生冲突。
Question 2: 问题2:
The notation is standard in type theory, and popularised by Haskell. 该符号是类型理论中的标准,并已由Haskell推广。
a
and b
are any types. a
和b
是任何类型。 (...)
is a tuple of types, [a]
is an list whose elements are a
. (...)
是类型的元组, [a]
是其元素为a
的列表。 a -> b
is a function that takes an argument of type a
and yields a return of type b
, and is right-associative. a -> b
是采用类型的参数的函数a
,并产生类型的返回b
,并且是右结合。 The example in question reads: 有问题的示例显示:
It is a function that takes an argument a function that takes two arguments (of types a
and b
respectively) and returns a value of type a
; 它是一个函数,它的参数有两个参数(的类型的函数
a
和b
分别地),并返回类型的值a
; and yields a function that takes an argument of type a
and returns a function that takes an argument that is a list of elements of type b
, returning a value of type a
. 并产生一个函数,它类型的参数
a
,并返回一个函数,它的参数是类型的元素列表b
,返回类型的值a
。
This reads very confusingly, but an uncurried description will be a bit easier: it is a function that takes three arguments: the first one being a function (as described above), the second one being a value of a
, the third one being a list of b
elements, and returns a value of a
. 此读取很容易混淆的,而是一个uncurried描述将容易一点:它是一个函数,它有三个参数:第一个是一个函数(如上所述),第二个是值
a
,第三个是一个的列表b
的元件,并且返回的值a
。
Specifically, R.reduce
is such a function: in 具体来说,
R.reduce
是这样的功能:
R.reduce(add, 10, numbers);
add
is a function that takes two integers (both a
and b
being the same, integer), and returns an integer ( (a, b) -> a
); add
是一个函数,它接受两个整数( a
和b
都是相同的整数),并返回一个整数( (a, b) -> a
); 10
is of type integer ( a
); 10
是整数( a
)类型; numbers is a list of integers ( [b]
); 数字是整数列表(
[b]
); and the return value is an integer ( a
). 返回值为整数(
a
)。
Note that it mixes curried and uncurried syntax; 注意,它混合了咖喱和未咖喱的语法。 if it was fully curried,
add
would be a -> b -> a
, not (a, b) -> a
. 如果完全咖喱,则
add
将是a- a -> b -> a
,而不是(a, b) -> a
。
Question 2: 问题2:
That is a Hindley-Milner type signature. 那是Hindley-Milner类型的签名。 For the example given, 'a' and 'b' are arbitrary types and '->' is a function.
对于给定的示例,“ a”和“ b”是任意类型,“->”是一个函数。
So lets break it down. 因此,让我们分解一下。
First, we take a type 'a' and a function 'b -> a' 首先,我们采用类型'a'和函数'b-> a'
(a, b -> a) (a,b-> a)
This returns a function, which returns a function,.. (cause you know, currying). 这将返回一个函数,该函数将返回一个函数,..(因为您知道,currying)。 The point is we can add '()' to make it a bit more readable.
关键是我们可以添加'()'使其更具可读性。
(a, b -> a) -> (a -> ([b] -> a)) (a,b-> a)->(a->([b]-> a))
So if you pass this function 'a' and a function 'a' -> 'b', you get back 因此,如果您传递此函数“ a”和函数“ a”->“ b”,则会返回
a -> ([b] -> a) a->([b]-> a)
And if you pass this a type 'a' 如果您通过此操作,则输入“ a”
[b] -> a [b]-> a
Where [b] is an array of type 'b's. 其中[b]是类型'b'的数组。 Passing this function [b] gives you a type a
传递此函数[b]会为您提供类型a
a 一种
If you want to read more about functional programming in JavaScript, I can recommend The Mostly Adequate Guide 如果您想了解有关JavaScript中的函数式编程的更多信息,我可以推荐《 最适当的指南》。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.