Skip to content

Instantly share code, notes, and snippets.

@brikis98
Created February 4, 2012 09:15
Show Gist options
  • Select an option

  • Save brikis98/1736622 to your computer and use it in GitHub Desktop.

Select an option

Save brikis98/1736622 to your computer and use it in GitHub Desktop.
Seven Languages in Seven Weeks: Io, Day 2
Complex := Object clone do (
setValues := method(real, imaginary,
self real := real;
self imaginary := imaginary;
self
)
+ := method(other,
result := Complex clone setValues(self real + other real, self imaginary + other imaginary)
)
)
c1 := Complex clone setValues(1, 2)
c2 := Complex clone setValues(3, 4)
c3 := c1 + c2
c3 println
/*
Complex_0x22ee90:
imaginary = 6
real = 4
*/
fib_recursive := method(n,
if(n < 3, 1, fib_recursive(n - 1) + fib_recursive(n - 2))
)
fib_iterative := method(n,
prev_prev := 0
prev := 0
sum := 1
for(i, 2, n,
prev_prev = prev
prev = sum
sum = prev + prev_prev
)
sum
)
"-- Recursive Fibonacci --" println
for(i, 1, 10, fib_recursive(i) println)
"" println
"-- Iterative Fibonacci --" println
for(i, 1, 10, fib_iterative(i) println)
/*
-- Recursive Fibonacci --
1
1
2
3
5
8
13
21
34
55
-- Iterative Fibonacci --
1
1
2
3
5
8
13
21
34
55
*/
// Can't use Random due to http://stackoverflow.com/questions/3481651/how-do-i-import-an-addon-in-the-io-language
// toGuess := Random value(100) ceil
// A random number as per http://xkcd.com/221/
toGuess := 4
guessCounter := 0
previousDiff := nil
currentDiff := nil
while(guessCounter < 10,
guessCounter = guessCounter + 1
currentGuess := File standardInput readLine("Enter your guess: ") asNumber()
currentDiff := (toGuess - currentGuess) abs
if(currentDiff == 0) then (
guessCounter = 10
) else (
if (previousDiff == nil) then("Try again" println) elseif(currentDiff == previousDiff) then ("You just guessed that!" println) elseif(currentDiff < previousDiff) then ("Hotter" println) else("Colder" println)
previousDiff = currentDiff
)
)
if(currentDiff == 0, "Correct!", "Sorry, you ran out of guesses.") println
List myAverage := method(
sum := 0
self foreach(i, v, if(v type == "Number", sum = sum + v, Exception raise("#{v} is not a Number" interpolate)))
sum / (self size)
)
list(1) myAverage println // 1
list(3, 3, 3) myAverage println // 3
list(1, 2, 3, 4, 5, 6, 7, 8, 9) myAverage println // 5
list(1, "A", 3) myAverage println // Exception: A is not a Number
(1 / 0) println // inf
(1 / 2) println // 0.5
Number originalDivision := Number getSlot("/")
Number / := method(other,
if (other == 0, 0, self originalDivision(other))
)
(1 / 0) println // 0
(1 / 2) println // 0.5
sum2dArray := method(arr,
arr flatten sum
)
arr := list(list(1, 2), list(3, 4), 5, list(6, 7, 8), 9, 10)
sum2dArray(arr) println // 55
TwoDList := Object clone do (
init := method(
self lists := list()
)
dim := method(x, y,
self lists preallocateToSize(y)
for(i, 0, y - 1, self lists append(list() preallocateToSize(x)))
self
)
set := method(x, y, value,
self lists at(y) atInsert(x, value)
)
get := method(x, y,
self lists at(y) at(x)
)
transpose := method(
transposedList := TwoDList clone dim(self lists size, self lists at(0) size)
self lists foreach(y, subList,
subList foreach(x, value,
transposedList set(y, x, value)
)
)
transposedList
)
println := method(
self lists foreach(y, subList,
subList foreach(x, value,
if(x == 0 or x == subList size, "|" print)
" #{get(x, y)} |" interpolate print
)
"" println
)
)
toFile := method(name,
File with(name) open write(self serialized) close
)
fromFile := method(name,
doRelativeFile(name)
)
)
list = TwoDList clone dim(2, 3)
list set(0, 0, "A")
list set(1, 0, "B")
list set(0, 1, "C")
list set(1, 1, "D")
list set(0, 2, "E")
list set(1, 2, "F")
transposedList := list transpose
transposedList toFile("transposed.txt")
transposedListFromFile := TwoDList fromFile("transposed.txt")
"2x3 list:" println
list println
"Transposed 3x2 list:" println
transposedList println
"Transposed 3x2 list from file:" println
transposedListFromFile println
/*
2x3 list:
| A | B |
| C | D |
| E | F |
Transposed 3x2 list:
| A | C | E |
| B | D | F |
Transposed 3x2 list from file:
| A | C | E |
| B | D | F |
*/
@brikis98
Copy link
Author

brikis98 commented Feb 4, 2012

See Seven Languages in Seven Weeks: Io, Day 2 for more information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment