Created
January 28, 2013 02:48
-
-
Save yangsu/4652640 to your computer and use it in GitHub Desktop.
Io Tutorial
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
# Io Tutorial | |
# Math | |
1+1 # ==> 2 | |
2 sin # ==> 0.909297 | |
2 sqrt # ==> 1.414214 | |
# Variables | |
a := 1 # ==> 1 | |
a # ==> 1 | |
b := 2 * 3 # ==> 6 | |
a + b # ==> 7 | |
# Conditions | |
a := 2 | |
if(a == 1) then(writeln("a is one")) else(writeln("a is not one")) | |
a is not one | |
if(a == 1, writeln("a is one"), writeln("a is not one")) | |
a is not one | |
# Lists | |
d := List clone append(30, 10, 5, 20) # ==> list(30, 10, 5, 20) | |
d size # ==> 4 | |
d print # ==> list(30, 10, 5, 20) | |
d := d sort # ==> list(5, 10, 20, 30) | |
d first # ==> 5 | |
d last # ==> 30 | |
d at(2) # ==> 20 | |
d remove(30) # ==> list(5, 10, 20) | |
d atPut(1, 123) # ==> list(5, 123, 20) | |
list(30, 10, 5, 20) select(>10) # ==> list(30, 20) | |
list(30, 10, 5, 20) detect(>10) # ==> 30 | |
list(30, 10, 5, 20) map(*2) # ==> list(60, 20, 10, 40) | |
list(30, 10, 5, 20) map(v, v*2) # ==> list(60, 20, 10, 40) | |
# Loops | |
for(i, 1, 10, write(i, " ")) | |
1 2 3 4 5 6 7 8 9 10 | |
d foreach(i, v, writeln(i, ": ", v)) | |
0: 5 | |
1: 123 | |
3: 20 | |
list("abc", "def", "ghi") foreach(println) | |
abc | |
def | |
ghi | |
# Strings | |
a := "foo" # ==> "foo" | |
b := "bar" # ==> "bar" | |
c := a .. b # ==> "foobar" | |
c at(0) # ==> 102 | |
c at(0) asCharacter # ==> "f" | |
s := "this is a test" # ==> "this is a test" | |
words := s split(" ", "\t") print | |
"this", "is", "a", "test" | |
s findSeq("is") # ==> 2 | |
s findSeq("test") # ==> 10 | |
s slice(10) # ==> "test" | |
s slice(2, 10) # ==> "is is a " |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment