Posted 10 September 2014 - 03:00 AM
Some amazing initial work by Shimomura Ikkei that I found, but I had the idea to add a few more things to it. Currently working on porting over various methods from Ruby, this is a very early version of what can be done.
http://pastebin.com/NDwsNrZj
I'll probably start namespacing these under something like Enumerable to keep it out of the global namespace. Any opinions so far on it?
Future plans:
That'd be something for language readability for sure. As to how to pull that off, I may well make a global method called enumeration that takes in a table and returns one with the methods tagged on it. Makes for a slight pain iterating, but I'll have to look for a better solution…
Thoughts?
EDIT So this is a thing: https://github.com/mirven/underscore.lua
No real need to continue given that, unless I can magically find how to monkeypatch to the above code.
EDIT(again) ok, so you can do that (kinda), go figure. Props to mirven for sure, handy, this. Minified version should be a tinge more practical, but still a load for sure.
underscore.lua (10,326b) - http://pastebin.com/4UK502Jb
underscore.min.lua (6,035b) - http://pastebin.com/XBQZcVeK
http://pastebin.com/NDwsNrZj
I'll probably start namespacing these under something like Enumerable to keep it out of the global namespace. Any opinions so far on it?
Future plans:
- See about pattern matching
- Monadic composition starting with Maybe
- List Comprehensions
- More powerful piping and streaming ala Clojure and Elixir
{1,2,3,4}.each(function (x) print(x + 1) end) -- returns the table unmodified, but executes code for each item
{1,2,3,4}.map(function(x) return x * 2 end) -- returns {2,4,6,8}
{1,2,3,4}.reduce(operator.add) -- returns 10
{1,2,3,4}.partition(function(x) return i % 2 == 0 end) -- returns {{2,4},{1,3}}
That'd be something for language readability for sure. As to how to pull that off, I may well make a global method called enumeration that takes in a table and returns one with the methods tagged on it. Makes for a slight pain iterating, but I'll have to look for a better solution…
Thoughts?
EDIT So this is a thing: https://github.com/mirven/underscore.lua
No real need to continue given that, unless I can magically find how to monkeypatch to the above code.
EDIT(again) ok, so you can do that (kinda), go figure. Props to mirven for sure, handy, this. Minified version should be a tinge more practical, but still a load for sure.
underscore.lua (10,326b) - http://pastebin.com/4UK502Jb
underscore.min.lua (6,035b) - http://pastebin.com/XBQZcVeK
Edited on 10 September 2014 - 03:23 AM