Created
March 3, 2013 03:05
-
-
Save draftcode/5074305 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 == 1 or n == 2, 1, fib(n-1) + fib(n-2))) | |
| anotherFib := method(n, | |
| if (n == 1 or n == 2, 1, | |
| prev1 := 1 | |
| prev2 := 1 | |
| for (i, 1, n-1, | |
| nprev := prev1 + prev2 | |
| prev1 = prev2 | |
| prev2 = nprev) | |
| prev1)) | |
This file contains hidden or 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
| squareBrackets := method( | |
| ret := list() | |
| call message arguments foreach(arg, | |
| ret push(ret doMessage(arg)) | |
| ) | |
| ret | |
| ) | |
| [ 1, 2, 3, 4 ] println |
This file contains hidden or 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
| OperatorTable addAssignOperator(":", "atPutNumber") | |
| curlyBrackets := method( | |
| r := Map clone | |
| call message arguments foreach(arg, | |
| r doMessage(arg) | |
| ) | |
| r | |
| ) | |
| Map atPutNumber := method( | |
| self atPut( | |
| call evalArgAt(0) asMutable removePrefix("\"") removeSuffix("\""), | |
| call evalArgAt(1) | |
| ) | |
| ) | |
| doFile("xml.io") |
This file contains hidden or 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 := List clone | |
| Matrix set := method(x, y, value, | |
| self at(y) atPut(x, value) | |
| ) | |
| Matrix get := method(x, y, | |
| self at(y) at(x) | |
| ) | |
| Matrix dim := method(x, y, | |
| ret := Matrix clone | |
| y repeat(ret push(list() setSize(x))) | |
| ret) | |
| # l := dim(2, 3) | |
| # l set(1, 1, "asdf") | |
| # l get(1, 1) println | |
| # l println |
This file contains hidden or 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 serialize := method(filename, | |
| file := File clone openForUpdating(filename) | |
| self foreach(row, | |
| file write(row map(i, i asString) join(" ") with("\n")) | |
| ) | |
| file close | |
| ) | |
| Matrix deserialize := method(filename, | |
| file := File clone openForReading(filename) | |
| ret := Matrix clone | |
| file foreachLine(line, | |
| ret push(line strip split(" ") map(s, s asNumber)) | |
| ) | |
| file close | |
| ret | |
| ) | |
| # mtx := Matrix dim(2, 3) | |
| # mtx set(0, 0, 1) | |
| # mtx set(1, 0, 2) | |
| # mtx set(0, 1, 3) | |
| # mtx set(1, 1, 4) | |
| # mtx set(0, 2, 5) | |
| # mtx set(1, 2, 6) | |
| # | |
| # mtx serialize("serialized.txt") | |
| mtx := Matrix deserialize("serialized.txt") | |
| mtx println |
This file contains hidden or 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 myAverage := method(self sum / self size) | |
| # list(1, 2, 3, 4, 5, "asdf") myAverage println | |
| List myAverage := method( | |
| self reduce(acc, x, | |
| if (x proto != Number, Exception raise("not a Number"), acc+x)) / self size) | |
| # list(1, 2, 3, 4, 5, "asdf") myAverage println |
This file contains hidden or 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
| randomValue := ((Random value) * 100) floor | |
| 10 repeat( | |
| guess := File standardInput readLine asNumber | |
| if (guess == randomValue, | |
| "Right" println | |
| System exit(0), | |
| if (guess < randomValue, "More" println, "Less" println) | |
| ) | |
| ) | |
| "Failed" println |
This file contains hidden or 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
| ll := list(list(1, 2, 3, 4), | |
| list(5, 6), | |
| list(7, 8, 9, 10)) | |
| sumTwoDim := method(ll, | |
| ll map(l, l sum) sum) | |
| sumTwoDim(ll) println |
This file contains hidden or 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
| mtx := Matrix dim(2, 3) | |
| mtx set(0, 0, 1) | |
| mtx set(1, 0, 2) | |
| mtx set(0, 1, 3) | |
| mtx set(1, 1, 4) | |
| mtx set(0, 2, 5) | |
| mtx set(1, 2, 6) | |
| Matrix transpose := method( | |
| colSize := self at(0) size | |
| rowSize := self size | |
| ret := Matrix dim(rowSize, colSize) // Transposed | |
| for (c, 0, colSize - 1, | |
| for (r, 0, rowSize - 1, | |
| ret set(r, c, self get(c, r)))) | |
| ret) | |
| mtx transpose println |
This file contains hidden or 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
| Builder := Object clone | |
| Builder indent ::= 0 | |
| Map formatAttr := method( | |
| ret := list() | |
| self foreach(key, value, | |
| ret push(list(key, "=\"", value, "\"") join) | |
| ) | |
| ret join(" ") | |
| ) | |
| Builder forward := method( | |
| self setIndent(self indent + 2) | |
| ret := "" | |
| content := self doMessage(call message argAt(0)) | |
| if (content type == "Map", | |
| ret = ret with(list("<", call message name, " ", content formatAttr, ">\n") join), | |
| ret = ret with(list("<", call message name, ">\n") join) | |
| ret = ret with(list(" " repeated(self indent), content, "\n") join) | |
| ) | |
| call message arguments slice(1) foreach( | |
| arg, | |
| content := self doMessage(arg) | |
| ret = ret with(list(" " repeated(self indent), content, "\n") join) | |
| ) | |
| self setIndent(self indent - 2) | |
| ret = ret with(list(" " repeated(self indent), "</", call message name, ">") join) | |
| ret | |
| ) | |
| Builder div({"author": "Tate"}, | |
| ul( | |
| li("Io"), | |
| li("Lua"), | |
| li("Javascript") | |
| )) println |
This file contains hidden or 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
| (1 / 0) println | |
| origDiv := Number getSlot("/") | |
| Number / = method(b, if (b == 0, 0, origDiv(self, b))) | |
| (1 / 0) println |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment