3 ms·
As someone who conjured this exact thing up in another language after reading SICP, I have to suggest that the first two commenters here missed the point: this
by Xurinos 15y ago
As someone who conjured this exact thing up in another language after reading SICP, I have to suggest that the first two commenters here missed the point: this is a very basic but powerful structure that implements a lazy stream and is nearly a monad (many standard options are written for you but could be generalized).
Want it to apply to data I/O? Stick your reader into the HEAD function, store the pointer to the next byte in TAIL, and implement functions for reading X number of bytes and converting them into their new types.
Yes, you can do math with it. You can also implement process/procedure into it, if you consider yourself popping lambdas off the stream, and which lambda is Next can be guided by choices in the current lambda.
Mostly, you can use this device to hold any collection of things and operate upon them in a streamy-syntax kind of way. For those who are familiar with jQuery, it is a bit of a reimplementation of a lot of jQuery's basic array-data handling with extra helpers like "range" tossed in and a stronger focus on the laziness. Where jQuery's prime target is work with the DOM, this stream package's target is the more theoretical version of some of jQuery's features, specifically those that iterate over the contained arrays. You could wrap jQuery in this and get an amazing hybrid.
Don't sell it short; it really is another useful and powerful way to approach data management. Check out how you construct your own stream with this package, and think beyond the basic numeric approaches.
- skilldrick 15y ago> As someone who conjured this exact thing up in another language after reading SICP Me too! http://jsfiddle.net/skilldrick/vT2mC/ http://jsfiddle.net/skilldrick/vT2mC/ I didn't get very far with it though - the combined issues of JavaScript's lack of tail-call optimisation and its verbose lambda syntax made building streams to painful. This looks like an interesting approach, though you'll never get around JS's heavy lambdas (unless you use CoffeeScript or something).
- raganwald 15y agoIt's hard to escape reimplementing streams after reading SICP, because Lazy evaluation is the red pill: http://weblog.raganwald.com/2007/02/haskell-ruby-and-infinity.html http://weblog.raganwald.com/2007/02/haskell-ruby-and-infinit...
- thesz 15y agoAs for "appending infinite lists" I'd like to suggest Control.Monad.Omega: http://hackage.haskell.org/package/control-monad-omega http://hackage.haskell.org/package/control-monad-omega "A monad for enumerating sets: like the list monad, but impervious to infinite descent. A depth-first search of a data structure can fail to give a full traversal if it has an infinitely deep path. Likewise, a breadth-first search of a data structure can fall short if it has an infinitely branching node. Omega addresses this problem by using a "diagonal" traversal that gracefully dissolves such data."
- derrida 15y agoI just want to quote something you said for the sheer awesome implications of it. "You can also implement process/procedure into it, if you consider yourself popping lambdas off the stream, and which lambda is Next can be guided by choices in the current lambda." This. Is. Powerful.