Created
September 5, 2011 04:14
-
-
Save passingloop/1194077 to your computer and use it in GitHub Desktop.
7つの言語7つの世界 第3章 Io 言語のセルフスタディやってみた 1-2 日目
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
Mazda := Object clone | |
Mazda cry := method("Zoom-Zoom" println) | |
Mazda getSlot("cry") call |
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
fib := method(n, | |
if (n < 3, return 1) | |
pred := list(1, 1) | |
for(i, 3, n, | |
pred = list(pred at(1), pred at(0) + pred at(1))) | |
pred at(1) | |
) | |
fib(1) println | |
fib(4) println | |
fib(8) println |
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
fib := method(i, if(i < 3, 1, fib(i -2) + fib(i - 1))) | |
fib(1) println | |
fib(4) println | |
fib(8) println |
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
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 |
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
"Hello, world" println |
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
Matrix := Object clone do( | |
sizeX := 0 | |
sizeY := 0 | |
dim := method(x, y, sizeX = x; sizeY = y; self items := List clone; items setSize(x * y)) | |
set := method(x, y, value, check(x, y); items atPut(index(x, y), value)) | |
get := method(x, y, check(x, y); items at(index(x, y))) | |
check := method(x, y, if(x >= sizeX or y >= sizeY, Exception raise("index error"))) | |
index := method(x, y, x * sizeX + y) | |
) | |
m := Matrix clone | |
m dim(3, 3) | |
m set(1, 2, "foo") | |
m get(1, 2) println | |
m get(2, 2) println | |
m get(3, 3) println | |
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
List mySum := method( | |
s := 0; | |
self foreach(v, | |
if(v type != "Number", Exception raise("found a non-Number"), s = s + v)); | |
s | |
) | |
List myAverage := method( | |
self mySum / self size | |
) | |
list(1, 2, 3) myAverage println | |
list(1, 2, "three") myAverage println |
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
quiz := Object clone do( | |
start := method( | |
answer := Random value(99) ceil + 1 | |
error := false | |
10 repeat( | |
guess := File standardInput readLine("input (1-100) > ") asNumber | |
if(answer == guess, "correct" println; return) | |
e := (answer - guess) abs | |
error and if(e < error, "closer" println, "further" println) | |
error = e | |
) | |
) | |
) | |
quiz start |
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
Matrix := Object clone do( | |
sizeX := 0 | |
sizeY := 0 | |
dim := method(x, y, sizeX = x; sizeY = y; self items := List clone; items setSize(x * y)) | |
set := method(x, y, value, check(x, y); items atPut(index(x, y), value)) | |
get := method(x, y, check(x, y); items at(index(x, y))) | |
check := method(x, y, if(x >= sizeX or y >= sizeY, Exception raise("index error"))) | |
index := method(x, y, x * sizeX + y) | |
transpose := method( | |
newMatrix := Matrix clone | |
newMatrix dim(sizeY, sizeX) | |
for(i, 0, sizeY -1, | |
for(j, 0, sizeX -1, | |
newMatrix set(i, j, self get(j, i)))) | |
newMatrix | |
) | |
) | |
matrix := Matrix clone | |
matrix dim(3, 3) | |
matrix set(1, 2, "foo") | |
newMatrix := matrix transpose | |
matrix get(1, 2) println | |
newMatrix get(2, 1) println | |
(newMatrix get(2, 1) == matrix get(1, 2)) println |
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
Number divide := Number getSlot("/") | |
Number / = method(n, if(n == 0, 0, divide(n))) | |
(1 / 0) println | |
(5 / 3) println |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
くわしくは http://d.hatena.ne.jp/passingloop/20110909/seven_languages_in_seven_weeks_chapter_3_io_language_self_study