UNPKG

9.95 kBMarkdownView Raw
1Ramda
2=============
3
4A practical functional library for JavaScript programmers.
5
6[![Build Status](https://github.com/ramda/ramda/workflows/Build/badge.svg)](https://github.com/ramda/ramda/actions?query=workflow%3ABuild)
7[![npm module](https://badge.fury.io/js/ramda.svg)](https://www.npmjs.org/package/ramda)
8[![deno land](http://img.shields.io/badge/available%20on-deno.land/x-lightgrey.svg?logo=deno&labelColor=black)](https://deno.land/x/ramda@v0.27.2)
9[![nest badge](https://nest.land/badge.svg)](https://nest.land/package/ramda)
10[![Gitter](https://badges.gitter.im/Join_Chat.svg)](https://gitter.im/ramda/ramda?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
11
12
13
14Why Ramda?
15----------
16
17<img src="https://ramdajs.com/ramdaFilled_200x235.png"
18 width="170" height="190" align="right" hspace="12" />
19
20There are already several excellent libraries with a functional flavor. Typically, they are meant to be general-purpose toolkits, suitable for working in multiple paradigms. Ramda has a more focused goal. We wanted a library designed specifically for a functional programming style, one that makes it easy to create functional pipelines, one that never mutates user data.
21
22
23
24What's Different?
25-----------------
26
27The primary distinguishing features of Ramda are:
28
29* Ramda emphasizes a purer functional style. Immutability and side-effect free functions
30 are at the heart of its design philosophy. This can help you get the job done with simple,
31 elegant code.
32
33* Ramda functions are automatically curried. This allows you to easily build up new functions
34 from old ones simply by not supplying the final parameters.
35
36* The parameters to Ramda functions are arranged to make it convenient for currying. The data
37 to be operated on is generally supplied last.
38
39The last two points together make it very easy to build functions as sequences of simpler functions, each of which transforms the data and passes it along to the next. Ramda is designed to support this style of coding.
40
41
42
43Introductions
44-------------
45
46* [Introducing Ramda](http://buzzdecafe.github.io/code/2014/05/16/introducing-ramda) by Buzz de Cafe
47* [Why Ramda?](http://fr.umio.us/why-ramda/) by Scott Sauyet
48* [Favoring Curry](http://fr.umio.us/favoring-curry/) by Scott Sauyet
49* [Why Curry Helps](https://hughfdjackson.com/javascript/why-curry-helps/) by Hugh Jackson
50* [Hey Underscore, You're Doing It Wrong!](https://www.youtube.com/watch?v=m3svKOdZijA&app=desktop) by Brian Lonsdorf
51* [Thinking in Ramda](https://randycoulman.com/blog/categories/thinking-in-ramda) by Randy Coulman
52
53
54
55Philosophy
56----------
57Using Ramda should feel much like just using JavaScript.
58It is practical, functional JavaScript. We're not introducing
59lambda expressions in strings, we're not borrowing consed
60lists, we're not porting over all of the Clojure functions.
61
62Our basic data structures are plain JavaScript objects, and our
63usual collections are JavaScript arrays. We also keep other
64native features of JavaScript, such as functions as objects
65with properties.
66
67Functional programming is in good part about immutable objects and
68side-effect free functions. While Ramda does not *enforce* this, it
69enables such style to be as frictionless as possible.
70
71We aim for an implementation both clean and elegant, but the API is king.
72We sacrifice a great deal of implementation elegance for even a slightly
73cleaner API.
74
75Last but not least, Ramda strives for performance. A reliable and quick
76implementation wins over any notions of functional purity.
77
78
79
80Installation
81------------
82
83To use with node:
84
85```bash
86$ npm install ramda
87```
88
89Then in the console:
90
91```javascript
92const R = require('ramda');
93```
94
95To use directly in [Deno](https://deno.land):
96```javascript
97import * as R from "https://deno.land/x/ramda@v0.27.2/mod.ts";
98```
99
100or using Nest.land:
101```javascript
102import * as R from "https://x.nest.land/ramda@0.27.2/mod.ts";
103```
104
105To use directly in the browser:
106
107```html
108<script src="path/to/yourCopyOf/ramda.js"></script>
109```
110
111or the minified version:
112
113```html
114<script src="path/to/yourCopyOf/ramda.min.js"></script>
115```
116
117or from a CDN, either cdnjs:
118
119```html
120<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.29.1/ramda.min.js"></script>
121```
122
123or one of the below links from [jsDelivr](http://jsdelivr.com):
124
125```html
126<script src="//cdn.jsdelivr.net/npm/ramda@0.29.1/dist/ramda.min.js"></script>
127<script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>
128```
129
130(note that using `latest` is taking a significant risk that ramda API changes could break your code.)
131
132These script tags add the variable `R` on the browser's global scope.
133
134Or you can inject ramda into virtually any unsuspecting website using [the bookmarklet](https://github.com/ramda/ramda/blob/master/BOOKMARKLET.md).
135
136**Note for versions > 0.25**
137Ramda versions > 0.25 don't have a default export.
138So instead of `import R from 'ramda';`, one has to use `import * as R from 'ramda';`
139Or better yet, import only the required functions via `import { functionName } from 'ramda';`
140
141**Note for ES6 module and browsers**
142In order to access to the ES6 module in browsers, one has to provide the content of the __es__ directory (see below for the build instructions) and use `import * as R from './node_modules/ramda/es/index.js';`
143
144
145### Build
146
147`npm run build` creates `es`, `src` directories and updates both __dist/ramda.js__ and __dist/ramda.min.js__
148
149#### Partial Builds
150
151It is possible to build Ramda with a subset of the functionality to reduce its file size. Ramda's build system supports this with command line flags. For example if you're using `R.compose`, `R.reduce`, and `R.filter` you can create a partial build with:
152
153 npm run --silent partial-build compose reduce filter > dist/ramda.custom.js
154
155This requires having Node/io.js installed and ramda's dependencies installed (just use `npm install` before running partial build).
156
157Documentation
158-------------
159
160Please review the [API documentation](https://ramdajs.com/docs/).
161
162Also available is our [Cookbook](https://github.com/ramda/ramda/wiki/Cookbook) of functions built from Ramda that you may find useful.
163
164
165The Name
166--------
167
168Ok, so we like sheep. That's all. It's a short name, not already
169taken. It could as easily have been `eweda`, but then we would be
170forced to say _eweda lamb!_, and no one wants that. For non-English
171speakers, lambs are baby sheep, ewes are female sheep, and rams are male
172sheep. So perhaps ramda is a grown-up lambda... but probably not.
173
174
175
176
177Running The Test Suite
178----------------------
179
180**Console:**
181
182To run the test suite from the console, you need to have `mocha` installed:
183
184 npm install -g mocha
185
186Then from the root of the project, you can just call
187
188 mocha
189
190Alternately, if you've installed the dependencies, via:
191
192 npm install
193
194then you can run the tests (and get detailed output) by running:
195
196 npm test
197
198**Browser:**
199
200You can use [testem](https://github.com/airportyh/testem) to
201test across different browsers (or even headlessly), with livereloading of
202tests. Install testem (`npm install -g testem`) and run `testem`. Open the
203link provided in your browser and you will see the results in your terminal.
204
205If you have _PhantomJS_ installed, you can run `testem -l phantomjs` to run the
206tests completely headlessly.
207
208
209Usage
210-----------------
211
212For `v0.25` and up, import the whole library or pick ES modules directly from the library:
213
214```js
215import * as R from 'ramda'
216
217const {identity} = R
218R.map(identity, [1, 2, 3])
219```
220
221Destructuring imports from ramda *does not necessarily prevent importing the entire library*. You can manually cherry-pick methods like the following, which would only grab the parts necessary for `identity` to work:
222
223```js
224import identity from 'ramda/src/identity'
225
226identity()
227```
228
229Manually cherry picking methods is cumbersome, however. Most bundlers like Webpack and Rollup offer tree-shaking as a way to drop unused Ramda code and reduce bundle size, but their performance varies, discussed [here](https://github.com/scabbiaza/ramda-webpack-tree-shaking-examples). Here is a summary of the optimal setup based on what technology you are using:
230
2311. Webpack + Babel - use [`babel-plugin-ramda`](https://github.com/megawac/babel-plugin-ramda) to automatically cherry pick methods. Discussion [here](https://www.andrewsouthpaw.com/ramda-webpack-and-tree-shaking/), example [here](https://github.com/AndrewSouthpaw/ramda-webpack-tree-shaking-examples/blob/master/07-webpack-babel-plugin-ramda/package.json)
2321. Webpack only - use `UglifyJS` plugin for treeshaking along with the `ModuleConcatenationPlugin`. Discussion [here](https://github.com/ramda/ramda/issues/2355), with an example setup [here](https://github.com/scabbiaza/ramda-webpack-tree-shaking-examples/blob/master/06-webpack-scope-hoisted/webpack.config.js)
2331. Rollup - does a fine job properly treeshaking, no special work needed; example [here](https://github.com/scabbiaza/ramda-webpack-tree-shaking-examples/blob/master/07-rollup-ramda-tree-shaking/rollup.config.js)
234
235
236Typings
237-----------------
238
239- [TypeScript](https://www.npmjs.com/package/@types/ramda)
240- [Flow](https://github.com/flowtype/flow-typed/tree/master/definitions/npm/ramda_v0.x.x)
241
242
243
244
245Translations
246-----------------
247
248- [Chinese(中文)](http://ramda.cn/)
249- [Ukrainian(Українська)](https://github.com/ivanzusko/ramda)
250- [Portuguese(BR)](https://github.com/renansj/ramda)
251- [Russian(Русский)](https://github.com/Guck111/ramda)
252- [Spanish(ES)](https://github.com/wirecobweb/ramda)
253
254
255
256
257Funding
258-----------------
259
260If you wish to donate to Ramda please see our [Open Collective](https://opencollective.com/ramda) page. Thank you!
261
262
263Acknowledgements
264-----------------
265
266Thanks to [J. C. Phillipps](http://www.jcphillipps.com) for the Ramda logo.
267Ramda logo artwork &copy; 2014 J. C. Phillipps. Licensed Creative Commons
268[CC BY-NC-SA 3.0](http://creativecommons.org/licenses/by-nc-sa/3.0/).