Io Tutorial


Math

Io> 1+1
==> 2

Io> 2 sin
==> 0.909297

Io> 2 sqrt
==> 1.414214

Variables

Io> a := 1
==> 1

Io> a
==> 1

Io> b := 2 * 3
==> 6

Io> a + b
==> 7

Conditions

Io> a := 2

Io> if(a == 1) then(writeln("a is one")) else(writeln("a is not one"))
a is not one

Io> if(a == 1, writeln("a is one"), writeln("a is not one"))
a is not one

Lists

Io> d := List clone append(30, 10, 5, 20)    
==> list(30, 10, 5, 20)   

Io> d size
==> 4

Io> d print
==> list(30, 10, 5, 20)

Io> d := d sort
==> list(5, 10, 20, 30)

Io> d first
==> 5

Io> d last
==> 30

Io> d at(2)
==> 20

Io> d remove(30)
==> list(5, 10, 20)

Io> d atPut(1, 123)
==> list(5, 123, 20)

Io> list(30, 10, 5, 20) select(>10)
==> list(30, 20)

Io> list(30, 10, 5, 20) detect(>10)
==> 30

Io> list(30, 10, 5, 20) map(*2)
==> list(60, 20, 10, 40)

Io> list(30, 10, 5, 20) map(v, v*2)
==> list(60, 20, 10, 40)

Loops

Io> for(i, 1, 10, write(i, " "))
1 2 3 4 5 6 7 8 9 10 

Io> d foreach(i, v, writeln(i, ": ", v))
0: 5
1: 123
3: 20

Io> list("abc", "def", "ghi") foreach(println)
abc
def
ghi

Strings

Io> a := "foo"
==> "foo"

Io> b := "bar"
==> "bar"

Io> c := a .. b
==> "foobar"

Io> c at(0)
==> 102

Io> c at(0) asCharacter
==> "f"

Io> s := "this is a test"
==> "this is a test"

Io> words := s split(" ", "\t") print
"this", "is", "a", "test"

Io> s findSeq("is")
==> 2

Io> s findSeq("test")
==> 10

Io> s slice(10)
==> "test"

Io> s slice(2, 10)
==> "is is a "

Objects

Io> Contact := Object clone
==>  Contact_0x7fad4365a640:
    type = "Contact"

Io> Contact type
==> Contact

Io> Contact proto type
==> Object

Io> Contact name := nil
==> nil

Io> Contact address := nil
==> nil

Io> holmes := Contact clone setName("Holmes") setAddress("221B Baker St") setCity("London")
==> Contact_0x7fad4365a640
    type = "Contact"
    name = "Holmes"
    address = "221B Baker St"
    city = "London"

Io> holmes slotNames
==> list("type", "name", "address", "city")

Io> Contact fullAddress := method(name .. "\n" .. address .. "\n" .. city)
==> method(
    name .. "\n" .. address .. "\n" .. city
)

Io> holmes fullAddress
==> "Holmes
221B Baker St
London"

Io> holmes getSlot("fullAddress")
==> method(
    name .. "\n" .. address .. "\n" .. city
)

Namespace

Io> Lobby slotNames
==> list(set_, Protos, Lobby, _, exit, forward)
  
Io> Lobby Protos slotNames
==> list(Core, Addons)

Io> Lobby protos Core slotNames
==> list(Duration, Number, Eol, Coroutine, Sequence, DynLib, Normal, Notifier, ImmutableSequence, Collector, Debugger, Directory, CFunction, Block, vector, WeakLink, nil, false, CLI, Compiler, RunnerMixIn, Continue, File, TestSuite, Future, DirectoryCollector, Scheduler, UnitTest, FileCollector, FutureProxy, Date, true, Map, Break, List, AddonLoader, Call, String, Sandbox, Importer, Exception, DummyLine, Locals, Error, TestRunner, Profiler, Object, System, Path, Addon, SerializationStream, Return, OperatorTable, Vector, Message)

Io> Lobby protos Core Date slotNames
==> list(zone, -=, month, fromNumber, asSerialization, secondsToRun, +, print, isValidTime, setGmtOffset, fromSerialization, fromString, setDay, now, asJson, clock, timeStampString, setToUTC, asNumberString, setSecond, second, minute, format, year, convertToLocal, today, -, setYear, setMinute, hour, +=, asString, asAtomDate, secondsSinceNow, isDaylightSavingsTime, justSerialized, secondsSince, isDST, cpuSecondsToRun, gmtOffsetSeconds, asNumber, copy, isPast, gmtOffset, setHour, isToday, convertToUTC, convertToZone, setMonth, day)

More

  Why's Io Has A Very Clean Mirror
Why's Lazy Bricks, Lazy Mortar
Quag's Getting Started Tutorial