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 with memoization | |
fakeClosure := Object clone | |
fakeClosure memo := Map clone | |
fakeClosure fibmemo := method(num, | |
#I need this next line when i assign this method to a slot in the | |
#outer object (see last line of this gist). | |
#Feels like a 'this/self' binding issue. Otherwise i would have expected | |
#the memo slot on fakeClosure to be visible here | |
memo := fakeClosure memo #not strictly necessary when calling 'fakeClosure fibmemo' | |
memo atPut(0 asString, 0) |
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
#traditional recursive fib | |
fib := method(num, | |
if(num == 0 or num == 1 | |
, num | |
, fib(num-1) + fib(num-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
#differential inheritance | |
#making types (objects with a type field) | |
Shape := Object clone | |
Shape area := method(width * height) | |
Shape width := 5 | |
Shape height := 9 | |
Rect := Shape clone | |
Square := Shape clone | |
Ellipse := Shape clone |
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
fakeClosure := Object clone do( | |
memo := Map clone | |
fibmemo := block(num, #note 'blocks' are lexically scoped | |
memo atPut(0 asString, 0) | |
memo atPut(1 asString, 1) | |
fibhelp := method(num, | |
one := memo at((num-1) asString) | |
two := memo at((num-2) asString) | |
if(one == nil | |
, one = fibhelp(num-1,memo) |
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
#get rid of the object and just use a method as a container | |
fibmemoclosure := method(num, | |
memo := Map clone | |
fibmemo := block(num, #note 'blocks' are lexically scoped | |
memo atPut(0 asString, 0) | |
memo atPut(1 asString, 1) | |
fibhelp := method(num, | |
one := memo at((num-1) asString) | |
two := memo at((num-2) asString) | |
if(one == nil |
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
#Minimal mixins in Io! | |
Object mix := method(obj, | |
#grab all the objects methods and add them to the target | |
#the target is the object mixing in the mixin | |
mixer := call target | |
obj slotNames foreach(slotName, | |
if(obj getSlot(slotName) type == "Block" | |
, mixer setSlot(slotName, obj getSlot(slotName)) | |
#ignore properties for the moment | |
) |
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
#Actors | |
#this code is from day 2 | |
Delay := Object clone | |
Delay send := method( | |
args := call message arguments | |
for(i,0, args size - 1, 2, | |
delay := doMessage(args at(i)) | |
msg := args at(i+1) | |
wait(delay) |
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
#The sleeping barber problem http://en.wikipedia.org/wiki/Sleeping_barber_problem | |
#Solved with Io actors | |
Barber := Object clone do( | |
sleeping := false | |
wakeup := method( | |
"Waking Up!" println | |
sleeping = false | |
) |
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
import scala.util.Random | |
class Game{ | |
val board = Array(Array("_", "_", "_"), | |
Array("_", "_", "_"), | |
Array("_", "_", "_")) | |
var currentPlayer = "X" | |
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
//Experimenting with traits. | |
trait Volume { | |
//define some methods we need to use to implement the functionality of | |
//this trait, the class using this trait will need to implement these methods | |
//concretely. | |
def area() : Int | |
def height : Int //this can also be a val | |
//the functionality provided by this trait | |
def volume() = { |
OlderNewer