Skip to content

Instantly share code, notes, and snippets.

View tafsiri's full-sized avatar

Yannick Assogba tafsiri

View GitHub Profile
#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)
#traditional recursive fib
fib := method(num,
if(num == 0 or num == 1
, num
, fib(num-1) + fib(num-2)
)
)
@tafsiri
tafsiri / gist:762296
Created January 2, 2011 04:58
making objects and types in io
#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
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)
#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
@tafsiri
tafsiri / mixins.io
Created January 23, 2011 22:37
mixins in Io
#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
)
#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)
#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
)
import scala.util.Random
class Game{
val board = Array(Array("_", "_", "_"),
Array("_", "_", "_"),
Array("_", "_", "_"))
var currentPlayer = "X"
//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() = {