transducers:受 Clojure 启发的转换器在 JS 中的实现

  • T8_128337
    了解作者
  • 44.4KB
    文件大小
  • zip
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-06-11 22:11
    上传日期
换能器 ========== 库提供可组合的算法转换,这些转换独立于其输入和输出源的上下文,并且仅指定转换的本质。 换句话说,转换器不与它们正在操作的数据耦合,因此可以对内置 JS 类型(如数组、字符串、数字、迭代器)进行操作,也可以对自定义类型(如数据结构、 Observables)进行操作, 或您可能决定使用它们的任何其他内容。 以下资源很好地介绍了该库实现的 Transducers 思想。 应用程序接口 换能器 地图(f) 将f应用于输入数据结构的每一项。 const { map } = require ( "transducers" ) const inc = map ( x => x + 1 ) inc ( [ 2 , 3 , 4 ] ) // => [3, 4, 5] 过滤器(p) 仅保留输入中p(item)返回逻辑true 。 const { filter
transducers-master.zip
  • transducers-master
  • .travis.yml
    95B
  • package.json
    1.3KB
  • Readme.md
    7.6KB
  • src
  • test
  • index.js
    15.7KB
  • test.js
    260B
  • transducers.js
    15.5KB
  • lib
  • test
  • index.js
    74.2KB
  • test.js
    1.7KB
  • transducers.js
    74.7KB
内容介绍
# Transducers [![NPM version][npm-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Gitter][gitter-image]][gitter-url] ========= [![Browser support](https://ci.testling.com/Gozala/transducers.png)](http://ci.testling.com/Gozala/transducers) Library provides composable algorithmic transformations that are independent from the context of their input and output sources and specify only the essence of the transformation. In other words transducers are not coupled with a data they are operating & there for can operate on built-in JS types like arrays, strings, numbers, iterators as well as they could on custom types like [Immutable.js][] data structures, [RxJS][] Observables, [CSP Channels][] or whatever else you may decide to use them for. Following resources provide an excelent introduction to Transducers idea that this this library imlements. * ["Transducers are coming" announce blog post](http://blog.cognitect.com/blog/2014/8/6/transducers-are-coming) * [Rich Hickey's Transducers StrangeLoop presentation](https://www.youtube.com/watch?v=6mTbuzafcII) ## API ### transducers #### map(f) Applies `f` onto each item of the input data structure. ```js const {map} = require("transducers") const inc = map(x => x + 1) inc([2, 3, 4]) // => [3, 4, 5] ``` #### filter(p) Keeps only items from input on which `p(item)` returned logical `true`. ```js const {filter} = require("transducers") const isEven = x => !(x % 2) filter(isEven)([1, 2, 3, 4]) // => [2, 4] ``` #### remove(p) Removes items from input on with `p(item)` returned logical `true`. ```js const {remove} = require("transducers") const isEven = x => !(x % 2) remove(isEven)([1, 2, 3, 4]) // => [1, 3] ``` #### drop(n) Drops first `n` number of items from the input. ```js const {drop} = require("transducers") drop(2)([1, 2, 3, 4]) // => [3, 4] ``` #### dropWhile(p) Drops items from the input while `p(item)` is logical `true`. Note that once `p(item)` returns logical `false` `p` is no longer applied to items. ```js const {dropWhile} = require("transdures") dropWhile(x => x < 5)([1, 3, 7, 4, 9]) // => [7, 4, 9] ``` #### dropRepeats Drops duplicate items form the input (equality compared with ===). ```js const {dropRepeats} = require("transducers") dropRepeats([1, 2, 2, 2, 3, 3, 4]) // => [1, 2, 3, 4] ``` #### take(n) Takes first `n` number of items from the input. ```js const {take} = require("transducers") take(3)([1, 2, 3, 4, 5]) // => [1, 2, 3] ``` #### takeWhile(p) Takes items as long as `p(item)` returns logical `true`. Note than once `p(item)` returns logical `false` `p` is no longer applied to items. ```js const {takeWhile} = require("transducers") takeWhile(x => x < 5)([1, 4, 6, 5, 3]) // => [1, 4] ``` #### partition(n) Collects inputs into arrays of size `n`. In case there are not enough padding items, last partition will have less than `n` items. ```js const {partition} = require("transducers") partition(3)([0, 1, 2, 3, 4, 5]) // => [[0, 1, 2], [3, 4, 5]] partition(3)([0, 1, 2, 3]) // => [[0, 1, 2], [3]] ``` #### cat Concatenates items of the input. Assumes that items are collections. ```js const {cat} = require("transducers") cat([[1, 2, 3], [4], [5, 6]]) // => [1, 2, 3, 4, 5, 6] ``` ### composition If `map` is passed a transducer produces composed transducer: ```js (filter(isEven)) (map(inc)) ([1, 4, 9, 10]) // => [5, 11] ``` #### mapcat In fact library does not expose `mapcat` as it's pretty much made obsolete by a composition API. ```js map(x => x.split("/"))(cat)(["path/to", "dir/file"]) // => ["path", "to", "dir", "file"] ``` ## Different implementation There are other two known implementations of [Transducers][] in JS: - [transducers-js] - [transducers.js] In fact this library initially started as an attempt to enhance API of [transducers.js][] and of a general idea, which also lead it to it's own path. The core difference from both of the libraries is in the way transducers are composed & applied to input. In that regard it actually has more in common with [fab.js][] than transducers. #### transducer application In above mentioned libraries transducer application happens through other functions like [into][], [reduce][] & [transduce][] & expectation is that data type constructors can take transducer to create transduced version of it. In contrast this library does not provide [into][] and considers both [reduce][] and [transduce][] to be to low level APIs. For most common cases you would want to transform a data structure of a type to different data structure of the same type. In this library transducer functions can just take that data structure as an argument and return transformed version: ```js const inc = x => x + 1 map(inc)([1, 2, 3]) // => [2, 3, 4] map(char => char.toUpperCase())("hello") // => "HELLO" ```` ### transducers can be applied to primitives In this library transdures can also apply to primitives values like numbers just as well as they can to collections: ```js map(inc)(5) // => 6 ``` any transformation over nil types like `null` and `undefined` is no op and return input back: ```js map(inc)(null) // => null map(_ => 5)(null) // => null ``` #### transducer composition Transducers in all of the libraries (including this one) can be composed with as a plain function composition that you know or at least have seen in [underscore.js][http://underscorejs.org/#compose]. Idea is simple composing `f()` and `g()` functions produces `f(g())`. ```js reduce(_.compose(x => x + 1, x => x * 2), 0, 2) // => 5 ``` Although in case of transducers there is a surprising twist related to the implementation illustrated in the example below: ```js reduce(_.compose(map(x => x + 1), map(x => x * 2), [], [2]) // => [6] ``` Unlike right to left execution as in ordinary function composition execution order in transducers is from left to right instead. In order to avoid confusion & dependency on additional composition constructs this library takes inspiration from an API pioneered by [fab.js][] a while back: ```js (map(x => x + 1)) (map(x => x * 2)) ([2, 3]) // => [6, 8] ``` Execution is from top to bottom & no extra functions are need to compose them. **P.S.:** You still could use `_.compose` but in that case avoid using application like `_compose(f, g)([1, 2, 3])` as that won't do what you expect, given that input will be passed to `g` and then result to `f` instead of passing it to `f.g` composition. ## License [MIT License](http://en.wikipedia.org/wiki/MIT_License) [npm-url]: https://npmjs.org/package/transducers [npm-image]: https://img.shields.io/npm/v/transducers.svg?style=flat [travis-url]: https://travis-ci.org/Gozala/transducers [travis-image]: https://img.shields.io/travis/Gozala/transducers.svg?style=flat [gitter-url]: https://gitter.im/Gozala/transducers?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge [gitter-image]: https://badges.gitter.im/Join%20Chat.svg [clojure transducers]:http://blog.cognitect.com/blog/2014/8/6/transducers-are-coming [transducers.js]:http://jlongster.com/Transducers.js--A-JavaScript-Library-for-Transformation-of-Data [Immutable.js]:http://facebook.github.io/immutable-js/docs/#/Record [CSP Channels]:https://github.com/gozala/channel [RxJS]:https://github.com/Reactive-Extensions/RxJS [transducers-js]:https://github.com/cognitect-labs/transducers-js [transducers.js]:https://github.com/jlongster/transducers.js [fab.js]:https://www.youtube.com/watch?v=ViQ8kiLtDXc [into]:http://cognitect-labs.github.io/transducers-js/classes/transducers.html#methods_transducers.into [reduce]:http://cognitect-labs.github.io/transducers-js/classes/transducers.html#methods_transducers.reduce [transduce]:http://cognitect-labs.github.io/transducers-js/classes/transducers.html#methods_transducers.transduce
评论
    相关推荐
    • 数据结构算法
      讲述常用数据结构算法 支持pascal和c两种语言
    • 数据结构算法
      校园网下载的,上传给有需要的人看看,小弟初来报道,望末见笑
    • 数据结构 算法
      各种算法,方便大家学习数据结构,用视频的方式诠释算法的神奇,让大家领略数据结构的美妙
    • 数据结构算法
      数据结构算法 学习资料
    • 数据结构算法
      不错得数据结构算法chm
    • 数据结构算法
      很不错的一本书!挺全的,好不好由你定论。
    • 数据结构算法
      小东西数据结构演算 本课件是一个动态演示数据结构算法执行过程的辅助教学软件, 它可适应读者对算法的输入数据和过程执行的控制方式的不同需求, 在计算机的屏幕上显示算法执行过程中数据的逻辑结构或存储结构的变化...
    • 数据结构算法
      数据结构算法
    • 数据结构 算法
      数据结构 算法相关知识 精心整理 数据结构 算法相关知识 精心整理
    • 数据结构算法
      数据结构详细的算法。。。