/* Seven languages in seven days. Chapter 3 on IO, Day 1 examples */ "hello" print Vehicle := Object clone Vehicle description := "Something to take you places" Vehicle description = "Something to take you far away" Vehicle nonexistingSlot = "This won't work." Vehicle description Vehicle slotNames Vehicle type Object type Car := Vehicle clone Car slotNames Car type Car description ferrari := Car clone ferrari slotNames ferrari type Ferrari := Car clone Ferrari type Ferrari slotNames ferrari slotNames method("So, you've come for an argument." println) method() type Car drive := method("Vroom" println) ferrari drive ferrari getSlot("drive") ferrari getSlot("type") ferrari proto Car proto Lobby toDos := list("find my car", "find Continuum Transfunctioner") toDos size toDos append("Find a present") list(1, 2, 3, 4) list(1, 2, 3, 4) average list(1, 2, 3, 4) sum list(1, 2, 3, 4) at(1) list(1, 2, 3, 4) append(5) list(1, 2, 3, 4) pop list(1, 2, 3, 4) prepend(0) list() isEmpty elvis := Map clone elvis atPut("home", "Graceland") elvis at("home") elvis atPut("style", "rock and roll") elvis asObject elvis asList elvis keys elvis size 4 < 5 true and false true and 0 true proto true clone false clone nil clone Highlander := Object clone Highlander clone := Highlander h1 := Highlander clone h2 := Highlander clone h2 == h1 o1 := Object clone o2 := Object clone o1 == o2 Object clone := "hosed" /* from samples/misc/Account.io, added "init" */ Account := Object clone Account balance := 0.0 Account deposit := method(v, balance = balance + v) Account show := method(write("Account balance: $", balance, "\n")) Account init := method( self balance := 0 ) a1 := Account clone a1 deposit(10) a1 show /* book again */ unless := method( (call sender doMessage(call message argAt(0))) ifFalse( call sender doMessage(call message argAt(1))) ifTrue( call sender doMessage(call message argAt(2))) ) unless(1 == 2, write("One is not two\n"), write("one is two\n")) /* from http://web.archive.org/web/20070811220804/http://codefluency.com/2006/10/3/getting-to-know-io */ default := 10 safe := method( slotName := call argAt(0) name ; if( val := call sender getSlot(slotName), val, call sender setSlot( slotName, default))) sum := safe(bar) + safe(foo) foo /* http://iota.flowsnake.org/call-objects.html */ times := method(n, result := list() n repeat( value := call evalArgAt(1) result append(value) ) return result ) z := 42 x := times(5, z) times( 3, x, 4,5,6) /* http://stackoverflow.com/questions/4419779/io-language-apply-arguments */ z := 2 m1 := method( call message arguments) m1( 1+1) m1( 1, 1+z, z) m2 := method( call evalArgs ) m2( 1+1) m2( 1, 1+z, z) /* http://io-fans.jottit.com/ */ /* http://code.google.com/p/iopeg/wiki/IoNuggets */ Sequence += := method(str, if(isMutable, appendSeq(str), asMutable +=(str)))