You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
9.0 KiB

пре 4 година
  1. Ramda
  2. =============
  3. A practical functional library for JavaScript programmers.
  4. [![Build Status](https://travis-ci.org/ramda/ramda.svg?branch=master)](https://travis-ci.org/ramda/ramda)
  5. [![npm module](https://badge.fury.io/js/ramda.svg)](https://www.npmjs.org/package/ramda)
  6. [![dependencies](https://david-dm.org/ramda/ramda.svg)](https://david-dm.org/ramda/ramda)
  7. [![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)
  8. Why Ramda?
  9. ----------
  10. <img src="https://ramdajs.com/ramdaFilled_200x235.png"
  11. width="170" height="190" align="right" hspace="12" />
  12. There 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.
  13. What's Different?
  14. -----------------
  15. The primary distinguishing features of Ramda are:
  16. * Ramda emphasizes a purer functional style. Immutability and side-effect free functions
  17. are at the heart of its design philosophy. This can help you get the job done with simple,
  18. elegant code.
  19. * Ramda functions are automatically curried. This allows you to easily build up new functions
  20. from old ones simply by not supplying the final parameters.
  21. * The parameters to Ramda functions are arranged to make it convenient for currying. The data
  22. to be operated on is generally supplied last.
  23. The 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.
  24. Introductions
  25. -------------
  26. * [Introducing Ramda](http://buzzdecafe.github.io/code/2014/05/16/introducing-ramda) by Buzz de Cafe
  27. * [Why Ramda?](http://fr.umio.us/why-ramda/) by Scott Sauyet
  28. * [Favoring Curry](http://fr.umio.us/favoring-curry/) by Scott Sauyet
  29. * [Why Curry Helps](https://hughfdjackson.com/javascript/why-curry-helps/) by Hugh Jackson
  30. * [Hey Underscore, You're Doing It Wrong!](https://www.youtube.com/watch?v=m3svKOdZijA&app=desktop) by Brian Lonsdorf
  31. * [Thinking in Ramda](http://randycoulman.com/blog/categories/thinking-in-ramda) by Randy Coulman
  32. Philosophy
  33. ----------
  34. Using Ramda should feel much like just using JavaScript.
  35. It is practical, functional JavaScript. We're not introducing
  36. lambda expressions in strings, we're not borrowing consed
  37. lists, we're not porting over all of the Clojure functions.
  38. Our basic data structures are plain JavaScript objects, and our
  39. usual collections are JavaScript arrays. We also keep other
  40. native features of JavaScript, such as functions as objects
  41. with properties.
  42. Functional programming is in good part about immutable objects and
  43. side-effect free functions. While Ramda does not *enforce* this, it
  44. enables such style to be as frictionless as possible.
  45. We aim for an implementation both clean and elegant, but the API is king.
  46. We sacrifice a great deal of implementation elegance for even a slightly
  47. cleaner API.
  48. Last but not least, Ramda strives for performance. A reliable and quick
  49. implementation wins over any notions of functional purity.
  50. Installation
  51. ------------
  52. To use with node:
  53. ```bash
  54. $ npm install ramda
  55. ```
  56. Then in the console:
  57. ```javascript
  58. const R = require('ramda');
  59. ```
  60. To use directly in the browser:
  61. ```html
  62. <script src="path/to/yourCopyOf/ramda.js"></script>
  63. ```
  64. or the minified version:
  65. ```html
  66. <script src="path/to/yourCopyOf/ramda.min.js"></script>
  67. ```
  68. or from a CDN, either cdnjs:
  69. ```html
  70. <script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
  71. ```
  72. or one of the below links from [jsDelivr](http://jsdelivr.com):
  73. ```html
  74. <script src="//cdn.jsdelivr.net/npm/ramda@0.25.0/dist/ramda.min.js"></script>
  75. <script src="//cdn.jsdelivr.net/npm/ramda@0.25/dist/ramda.min.js"></script>
  76. <script src="//cdn.jsdelivr.net/npm/ramda@latest/dist/ramda.min.js"></script>
  77. ```
  78. (note that using `latest` is taking a significant risk that ramda API changes could break your code.)
  79. These script tags add the variable `R` on the browser's global scope.
  80. Or you can inject ramda into virtually any unsuspecting website using [the bookmarklet](https://github.com/ramda/ramda/blob/master/BOOKMARKLET.md).
  81. **Note for versions > 0.25**
  82. Ramda versions > 0.25 don't have a default export.
  83. So instead of `import R from 'ramda';`, one has to use `import * as R from 'ramda';`
  84. Or better yet, import only the required functions via `import { functionName } from 'ramda';`
  85. ### Build
  86. `npm run build` creates `es`, `src` directories and updates both __dist/ramda.js__ and __dist/ramda.min.js__
  87. #### Partial Builds
  88. It 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:
  89. npm run --silent partial-build compose reduce filter > dist/ramda.custom.js
  90. This requires having Node/io.js installed and ramda's dependencies installed (just use `npm install` before running partial build).
  91. Documentation
  92. -------------
  93. Please review the [API documentation](https://ramdajs.com/docs/).
  94. Also available is our [Cookbook](https://github.com/ramda/ramda/wiki/Cookbook) of functions built from Ramda that you may find useful.
  95. The Name
  96. --------
  97. Ok, so we like sheep. That's all. It's a short name, not already
  98. taken. It could as easily have been `eweda`, but then we would be
  99. forced to say _eweda lamb!_, and no one wants that. For non-English
  100. speakers, lambs are baby sheep, ewes are female sheep, and rams are male
  101. sheep. So perhaps ramda is a grown-up lambda... but probably not.
  102. Running The Test Suite
  103. ----------------------
  104. **Console:**
  105. To run the test suite from the console, you need to have `mocha` installed:
  106. npm install -g mocha
  107. Then from the root of the project, you can just call
  108. mocha
  109. Alternately, if you've installed the dependencies, via:
  110. npm install
  111. then you can run the tests (and get detailed output) by running:
  112. npm test
  113. **Browser:**
  114. You can use [testem](https://github.com/airportyh/testem) to
  115. test across different browsers (or even headlessly), with livereloading of
  116. tests. Install testem (`npm install -g testem`) and run `testem`. Open the
  117. link provided in your browser and you will see the results in your terminal.
  118. If you have _PhantomJS_ installed, you can run `testem -l phantomjs` to run the
  119. tests completely headlessly.
  120. Usage
  121. -----------------
  122. For `v0.25` and up, import the whole library or pick ES modules directly from the library:
  123. ```js
  124. import * as R from 'ramda'
  125. const {identity} = R
  126. R.map(identity, [1, 2, 3])
  127. ```
  128. Destructuring 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:
  129. ```js
  130. import identity from 'ramda/src/identity'
  131. identity()
  132. ```
  133. Manually 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:
  134. 1. 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)
  135. 1. 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)
  136. 1. 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)
  137. Typings
  138. -----------------
  139. - [TypeScript](https://github.com/types/npm-ramda/)
  140. - [Flow](https://github.com/flowtype/flow-typed/tree/master/definitions/npm/ramda_v0.x.x)
  141. Translations
  142. -----------------
  143. - [Chinese(中文)](http://ramda.cn/)
  144. - [Ukrainian(Українська)](https://github.com/ivanzusko/ramda)
  145. - [Portuguese(BR)](https://github.com/renansj/ramda)
  146. - [Russian(Русский)](https://github.com/Guck111/ramda)
  147. Acknowledgements
  148. -----------------
  149. Thanks to [J. C. Phillipps](http://www.jcphillipps.com) for the Ramda logo.
  150. Ramda logo artwork &copy; 2014 J. C. Phillipps. Licensed Creative Commons
  151. [CC BY-NC-SA 3.0](http://creativecommons.org/licenses/by-nc-sa/3.0/).