Skip to content

Instantly share code, notes, and snippets.

@sriki77 sriki77/2d.io
Created Nov 28, 2011

Embed
What would you like to do?
Io Day 2
list( list(2,3) list(3,4,5) list(6)) flatten sum
(2/3) println
(2/0) println
(0/0) println
div := Number getSlot("/")
Number / :=method(d, if(d==0,d,self div(d)))
(2/3) println
(2/0) println
(0/0) println
#!/usr/bin/env io
fib_recur := method(n, if(n==1,return 1) if(n==2,return 1) return fib_recur(n-1)+fib_recur(n-2))
fib_iter := method(n, if(n==1,return 1) if(n==2,return 1)
(p1 :=1)(p2 :=1)(v :=0)
for(i,2,n-1,v=p1+p2
(p1=p2)(p2=v))
return v)
(fib_recur(30)==fib_iter(30)) println
List myAverage :=method(
v := self detect(x, x type != "Number")
(v != nil) ifTrue (Exception raise("Non number: "..(v).." found."))
self average
)
list(1,2,3,4) myAverage println
list(1,2,"a",4) myAverage println
#!/usr/bin/env io
Matrix := List clone
Matrix dim := method(x,y,
m := Matrix clone; for(i,1,y,m append(list()))
m foreach(l, l appendSeq("0" repeated(x) asList)); m
)
Matrix get := method(x,y,at(y) at(x))
Matrix set := method(x,y,value, at(y) atPut(x,value))
Matrix pp := method(f, foreach(row, row foreach(col, f write(""..(col).." ")); f write("\n"))
)
Matrix read := method(f,
m := Matrix clone; f foreachLine(l, m append(l split(" "))))
f := File openForReading("inpmat.txt")
3x4 := Matrix read(f)
f close
f := File with("matrix.txt")
f remove
f openForUpdating
3x4 pp(f)
#!/usr/bin/env io
Matrix := List clone
Matrix dim := method(x,y,
m := Matrix clone
for(i,1,y,m append(list()))
m foreach(l, l appendSeq("0" repeated(x) asList))
m
)
Matrix get := method(x,y,at(y) at(x))
Matrix set := method(x,y,value,at(y) atPut(x,value))
3x4 :=Matrix dim (3,4)
3x4 println
3x4 get(2,3) println
3x4 set(2,3,23)
3x4 get(2,3) println
#!/usr/bin/env io
num := Random clone value(1,100) round
in := File standardInput
loop(
"Enter Guess: " print
g :=in readLine asNumber
if(g ==num,"Guessed it right" println; break)
if(g>num, "Colder" println,"Hotter" println)
)
Matrix := List clone
Matrix dim := method(x,y,
m := Matrix clone; for(i,1,y,m append(list()))
m foreach(l, l appendSeq("0" repeated(x) asList)); m
)
Matrix cols := method(size)
Matrix rows := method(at(0) size)
Matrix get := method(x,y, at(y) at(x) )
Matrix set := method(x,y,value, at(y) atPut(x,value))
Matrix trans := method(
t :=Matrix dim(cols,rows)
for(i,0,rows-1,for(j,0,cols-1, t set(j,i,self get(i,j))))
t
)
3x4 :=Matrix dim (3,4)
for(i,0,2,for(j,0,3, 3x4 set(i,j,i+j)))
3x4 println
3x4 trans println
4x4 :=Matrix dim (4,4)
for(i,0,3,for(j,0,3, 4x4 set(i,j,i)))
4x4 println
4x4 trans println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.