您如何阅读ramda文档? [英] how do you read the ramda docs?

查看:51
本文介绍了您如何阅读ramda文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解Ramda的签名 docs .例如,如果您查看 map ,您会看到

I'm having trouble understanding the signature of Ramda docs. For example if you look at map you see this

Functor f =>(a→b)→f a→f b

我看不到这种模式如何适合示例:

I don't see how this pattern fits the example:

var double = x => x * 2;

R.map(double, [1, 2, 3]); //=> [2, 4, 6]

此示例中的函子是 [1,2,3] ,因此如何将其放置在 Functor f =&>中的 f 签名中;(a→b)→f a→f b ?另外,是什么意思?

The functor in this example is [1,2,3], so how does that get placed into the signature of f in Functor f => (a → b) → f a → f b? Also, what do the mean?

推荐答案

在这里我会给出一个简短的答案,但是更完整的答案会分布在两个答案中,分别是Ramda Wiki页面.(免责声明:我是该页面的作者,也是Ramda本身的负责人之一.)

I'll give a brief answer here, but a more complete one is spread across two answers to a similar question, which in turn was taken from the Ramda wiki page. (Disclaimer: I'm the author of that page and one of the principals in Ramda itself.)

这分为两部分:

Functor f => (a → b) → f a → f b

在粗箭头( => )之前,我们对其余部分有约束.此示例中的唯一约束是变量 f 必须为code> Functor .Functor是一种类型,其成员具有遵循特定法律的 map 方法.并且声明是通过另一种类型进行参数化的,因此我们不只是编写 f ,而是编写 f String f Number ,或更笼统地说, fa 用于某些未知类型的 a .

Before the fat arrow (=>) we have constraints on the remainder. The single constraint in this example is that the variable f must be a Functor. A Functor is a type whose members have a map method which obeys certain laws. And the declaration is parameterized over another type, so we don't write just f but f String, f Number, or more generically, f a for some unknown type a.

细箭头(-> )是Function类型的缩写.所以不用写

The skinny arrow (->) is an abbreviation for the type Function. So instead of writing

Function x y

我们可以改写

x -> y

或在需要时避免歧义.

(x -> y)

将它们放在一起,我们可以注意到,在 R.map(double,[1、2、3])中,我们有一个< double code> Number 到 Number ,这意味着我们的 a b 都是 Number .我们的函子是 Array .因此,将这些类型的定义专门化,我们让 map 接受一个从 Number Number 的函数,并返回一个包含 Number s并返回一个新的 Number s数组.(这是因为在此系统中,-> 绑定到右侧,所以(a-> b-> c)等同于(a->(b-> c)).在Ramda中,所有函数都以可调用的任何初始参数集调用的方式进行处理,直到提供所有术语之前,您都要继续返回函数.因此,使用Ramda函数, R.map(double)([1、2、3]) R.map(double,[1、2,3]).

Putting these together, we can note that in R.map(double, [1, 2, 3]), we have a function (double) from Number to Number, which means that our a and b are both Number. And our functor is Array. So specializing the definitions with these types, we have map accepting a function from Number to Number, and returning a function that takes an array of Numbers and returns a new array of Numbers. (That's because in this system, -> binds to the right, so (a -> b -> c) is equivalent to (a -> (b -> c)). In Ramda, all functions are curried in such a way that you can call them with any initial set of parameters, and until all the terms have been supplied, you continue to get back functions. Thus with Ramda functions there is no real difference between R.map(double)([1, 2, 3]) and R.map(double, [1, 2, 3]).

这篇关于您如何阅读ramda文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆