Skip to content

Instantly share code, notes, and snippets.

@BeniaminK
Created February 3, 2015 16: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 BeniaminK/d7bff2005cc63fd68525 to your computer and use it in GitHub Desktop.
Save BeniaminK/d7bff2005cc63fd68525 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/io
Matrix := Object clone
Matrix mainList := list()
Matrix dim := method(x, y,
for(i, 1, x, mainList push(list()))
for(i, 1, x, mainList foreach(arr, arr setSize(y)))
self
)
Matrix transpose := method(
yy := mainList size
xx := mainList at(0) size
mainList2 := list()
for(i, 1, xx, mainList2 push(list()))
for(i, 1, xx, mainList2 foreach(arr, arr setSize(yy)))
for(x, 0, yy - 1,
for(y, 0, xx - 1,
mainList2 at(y) atPut(x, mainList at(x) at(y))
)
)
mainList = mainList2
self
)
Matrix get := method(x,y,
mainList at(x) at(y)
)
Matrix set := method(x,y,value,
mainList at(x) atPut(y, value)
)
Matrix asString := method(
sizeX := mainList size
sizeY := mainList at(0) size
str := ""
for (x, 0, sizeX - 1,
for (y, 0, sizeY - 1,
str = "#{str} #{mainList at(x) at(y)}" interpolate
)
)
str = str asMutable strip
"#{sizeX} #{sizeY} #{str}" interpolate
)
Matrix fromString := method(string,
splitted := string split
"From string: #{string} #{splitted}" interpolate println
x := splitted at(0) asNumber
y := splitted at(1) asNumber
splitted removeAt(0)
splitted removeAt(0)
splitted reverseInPlace
"x:#{x} y:#{y} " interpolate println
temp := dim(x,y)
for (xx, 0, x - 1,
for (yy, 0, y - 1,
temp mainList at(xx) atPut(yy, splitted pop)
)
)
temp
)
matrix := Matrix dim(2,3)
matrix println
matrix set(0,0,1)
matrix set(0,1,2)
matrix set(0,2,3)
matrix set(1,0,4)
matrix set(1,1,5)
matrix set(1,2,6)
"GET:" println
matrix get(0,0) println
matrix get(1,1) println
"Normal matrix:" println
matrix println
matrix := matrix transpose
"Transposed: " println
matrix println
matrix asString println
"Matrix asString: #{matrix}" interpolate println
b := Matrix fromString(matrix asString)
"Matrix fromString: #{b}" interpolate println
f := File with("/tmp/matrix.txt")
f openForWriting
f write(matrix)
f close
f := File with("/tmp/matrix.txt")
f openForReading
f readLine(line)
f close
b:= Matrix fromString(line)
"Matrix fromString: #{b}" interpolate println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment