Skip to content

Instantly share code, notes, and snippets.

@passingloop
Created September 8, 2011 06:31
Show Gist options
  • Save passingloop/1202777 to your computer and use it in GitHub Desktop.
Save passingloop/1202777 to your computer and use it in GitHub Desktop.
7つの言語7つの世界 第3章 Io 言語のセルフスタディやってみた 2-3日目
book({"Author" : "Tate"},
ul(
li("Io"),
li("Lua"),
li("JavaScript")
)
)
OperatorTable addAssignOperator(":", "setPair")
Map setPair := method(k, v,
self atPut(k asMutable removePrefix("\"") removeSuffix("\""), v)
)
Builder := Object clone do(
output := Sequence clone
forward := method(
output appendSeq("<", call message name, ">\n")
call message arguments foreach(arg,
content := self doMessage(arg);
if(content type == "Map",
content foreach(k, v,
output removeSeq(">\n") appendSeq(" \"", k, "\"=\"", v, "\">\n")
)
)
)
if(content type == "Sequence", output appendSeq(content, "\n"))
output appendSeq("</", call message name, ">\n")
self
)
curlyBrackets := method(
self r := Map clone
call message arguments foreach(arg,
r doMessage(arg)
)
r
)
)
Builder doFile("book.txt") output println
Builder := Object clone do(
forward := method(
indent
writeln("<", call message name, ">")
call message arguments foreach(arg,
depth = depth + 1
content := self clone doMessage(arg);
if(content type == "Sequence", indent; writeln(content))
depth = depth - 1
)
indent
writeln("</", call message name, ">")
)
indent := method(depth repeat(write(" ")))
depth := 0
)
Builder ul(
li("Io"),
li("Lua"),
li("JavaScript"))
squareBrackets := method(
call message arguments
)
l := ["foo", "bar", "baz"]
l type println
l asString println
Matrix := Object clone do(
dim := method(x, y,
self cols := List clone setSize(x)
for(i, 0, x - 1, cols atPut(i, List clone setSize(y)))
)
set := method(x, y, v,
cols at(x) atPut(y, v)
)
get := method(x, y,
cols at(x) at(y)
)
load := method(path,
Lobby doFile(path)
)
save := method(path,
f := File with(path)
f openForUpdating
f write(self serialized)
f close
)
)
m := Matrix clone
m dim(3, 2)
m set(0, 1, "foo")
m set(2, 0, "bar")
m save("m.io")
n := Matrix load("m.io")
n get(0, 1) println
n get(2, 0) println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment