wu.js -- A lazy, functional Javascript library
fitzgen.github.com
fitzgen.github.com
>>> var factorial = wu.match(
... [ 0 ], 1,
... [ Number ], function (n) { return n * factorial(n - 1); }
... );
>>> factorial(5);
120Each line (e.g. [ 0 ], 1,) represents something similar to (key,value) pairs. So, in the first example any input that matches the array ([0]) (or [1,293,592] for that matter) would return the "value", or in this case "1".
It gets better with the following line, if the input matches a ([Number]) then it returns "function (n) { return n * factorial(n-1); }", a common recursive algorithm for finding a factorial.
Really, a nice looking framework all around.
fac 0 = 1
fac n = n * fac(n-1)
* Prolog has unification, actually, which is more powerful than pattern matching. Instead of trying to match a value against a list of patterns, it tries to match two (or more) potentially partially-defined values against each other.
Pattern matching is one of my favorite language features. PM-based code is usually quite straightforward to read (especially compared to a jumble of nested 'if's!), and can be compiled to efficient decision trees. It also works well with both static typing (checking full coverage of ML-style variant types) and dynamic typing (such as with Erlang's receive). The closest thing most OO languages have is polymorphism, which tends to break up the pattern table into lots of little scattered ones (unless the language has multimethods), making it harder to see the big picture.
I have a quick hack of a Lua library that does pattern matching with linear search. Whenever I get around to making it actually do analysis and compile search functions, I'll put it on github. (The patterns can have variable captures in them, too.)
Although the implementation works great, I couldn't get past the (obvious) syntax issues and never actually used it in production code. It was an interesting experiment though, especially when combined with Sjoerd Visscher's algebraic data type library (http://w3future.com/weblog/stories/2008/06/16/adtinjs.xml).
The "Why?" section doesn't seem to have any information in the docs.
I will be trying to get a "real world" and practical example that is a little more in depth out soon, but my first goal was total API documentation coverage.
Thanks for the interest,
Nick
They're fun to write though, it'd be interesting to see if there are any useful applications.
Please remember that JS is no longer just client-side stuff. Node.js, and other implementations have become quite popular in some circles for doing server side things.
JavaScript runs on the server side? ;-)
Quote from wu.js site:
"Works great in the browser".
Hence my genuine interest in understanding the not so obvious real-world cases for lazy-seq and client-side programming.
If you have any questions, don't hesitate to ask anything (but I might disappear for a couple hours because I have a final coming up).
Thanks!
http://github.com/sharkbrainguy/sequences.js
Some basic examples:
It does suffer from the whole "what's the point" problem.
If anyone isn't aware, it is named after the infamous Wu-Tang Clan: http://www.youtube.com/watch?v=0OEcZqG9xnE
http://documentcloud.github.com/underscore/
Check out the benchmarks: