Created
June 7, 2011 08:15
-
-
Save draegtun/1011885 to your computer and use it in GitHub Desktop.
Lazy Hash
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env io | |
# see - http://news.ycombinator.com/item?id=1626018 | |
Hash := Object clone do ( | |
with := method ( | |
hash := Map clone | |
call message arguments foreach (arg, | |
# arg = k(v) for eg. t(0) | |
k := arg name | |
v := arg argAt(0) asString | |
hash atPut (k, doString (v)) | |
) | |
hash | |
) | |
) | |
h := Hash with (t(0), f(1+1), with("hello"), writeln("not writeln!")) | |
h2 := Hash with (t(100), f("hello" asUppercase), at("my at"), atPut("my atPut")) | |
# Above lazily goes through the Hash with() building the key/value pairs without ever | |
# running methods/functions with, writeln, at, atPut and simply takes the method/func | |
# name and evaluates its argument into a key/value pair: | |
h at ("writeln") println # => "not writeln!" | |
h at ("f") println # => 2 | |
h2 at ("f") println # => "HELLO" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment