Skip to content

Instantly share code, notes, and snippets.

@draegtun
Created June 7, 2011 08:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save draegtun/1011885 to your computer and use it in GitHub Desktop.
Save draegtun/1011885 to your computer and use it in GitHub Desktop.
Lazy Hash
#!/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