问题
After reading SICP, I recently discovered streamjs. The developer referenced linqjs as an alternate implementation with different syntax, but I can't make the connection. How do the methods in streamjs map to those in linqjs?
回答1:
I haven't used either library, but, here's my initial analysis (I've read quite a bit of SICP, but not the whole thing admittedly).
stream.js
is an implementation of a functional style data structure for a list. Many data structures in a functional language tend to be recursive, much like the Stream
structure. It is composed of a head element, and a Stream
for the tail (subsequent elements). Here, lazy evaluation can be achieved by allowing the tail to be a function (i.e. infinite sequences).
Now, to answer your question, all of the functions provided by linq.js
should be able to be defined with other common higher order functions like map, reduce, walk, fold, etc.
Sure, stream.js
does not implement the Any()
method from linq.js
, but you can just do that with reduce()
.
回答2:
I guess they are similar because they pass functions around instead of "scalar values" ,so they can do lazy evaluation ( evaluate the result at the end of the operations / on demand and not at each operation like with classic javascript data structures). I used that principle with my pimple.js library , which has nothing to do with stream or link but uses lazy evalutation.
回答3:
linq.js and stream.js have the following similarities:
- Use functions to implement streams as a data structure
- Use streams to implement lazy evaluation of operations
linq.js and stream.js have the following differences:
- linq.js has syntactic sugar for querying JSON
- stream.js has the ability to chain streams
- linq.js has syntactic sugar for set operations
References
- LINQ in JavaScript for Real
- LINQ in JavaScript, ES6 Style
来源:https://stackoverflow.com/questions/13630368/whats-the-relationship-between-streamjs-and-linqjs