view 2-Io/day1-collections.io @ 54:91d02b3d74f8

Write up "unification" code
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Sep 2017 20:26:18 +0100
parents 4e1a659f8383
children
line wrap: on
line source

# Io has Lists and Maps as collections
toDos := list("Task 1", "Task 2")
toDos size println
# Calling a method with arguments is like most other languages
toDos append("Task 3")
toDos size println

# Lists are like arrays and are zero-indexed
list(1, 2, 3) at(1) println
# Lists can act like stacks and queues
list(1, 2, 3) prepend(0) println
list(1, 2, 3) append(4) println
list(1, 2, 3) push(4) println
list(1, 2, 3) pop println
list() isEmpty println
# Lists can also do some basic maths
list(1, 2, 3, 4) average println
list(1, 2, 3, 4) sum println

# Map is a prototype to be cloned
rincewind := Map clone
rincewind atPut("home", "Unseen University, Ankh Morpork")
rincewind at("home") println
rincewind atPut("job", "Wizzard")
rincewind at("job") println

# Maps can be turned in to objects with slots and lists (of (two-element) lists)
rincewind asObject println
rincewind asObject home println
rincewind asList println

# You can also pull keys from a map
rincewind keys println