Skip to content

Instantly share code, notes, and snippets.

@LuckyKoala
Created June 27, 2017 14:15
Show Gist options
  • Save LuckyKoala/c4e4603e4c9ea52efac66e0cdfcf3b37 to your computer and use it in GitHub Desktop.
Save LuckyKoala/c4e4603e4c9ea52efac66e0cdfcf3b37 to your computer and use it in GitHub Desktop.
Abou Seven Languages In Seven Weeks Io,Day 2. Use js as extension name so the code can be highlighted.
//For 3.3
//Create a nested list
li := list(1,2,3,list(3,4),"str")
//Flatten and only operate on Number and then sum them
List deepsum := method(
self flatten select(isKindOf(Number)) sum
)
li deepsum
//For 3.2
if(Number getSlot("originalDivision"), //First check if the method have already been changed
"Already changed" println, //Print message
Number setSlot("originalDivision", Number getSlot("/")); //Save the original method
//And then changed method to add code about "* / 0"
Number setSlot("/",
method(denominator,
if(denominator==0, 0, return(self originalDivision(denominator))))))
//Test case
3/0 println
5/2 println
//For 3.1
Test := Object clone
Test fib := method(val,
if(val > 2, fib(val-1)+fib(val-2),
if(val <= 0, 0, 1)))
list(1,2,3,4,5,6,7,8) foreach(val, Test fib(val) print; ", " print);
"Finished" println
Test fib_loop := method(val,
if(val==1, return(1))
p := 0
n := 1
c := 0
for(i, 1, val-1, c=p+n;p=n;n=c); c)
list(1,2,3,4,5,6,7,8) foreach(val, Test fib_loop(val) print; ", " print);
"Finished" println
//For 3.8
randomNumber := (Random value( 99 ) + 1) floor();
standardIO := File standardInput;
triedTimes := 0
10 repeat(
"Guess number (1..100): " println;
guess := standardIO readLine asNumber;
triedTimes = triedTimes+1;
if(
(guess == randomNumber),
break;
);
if(
guess > randomNumber,
"Too big" println,
"Too small" println
);
);
if(
(guess == randomNumber),
(
"Awesome! Excellent guess! You have tried #{triedTimes} times in total." interpolate println;
),
(
"Sorry, better luck next time. The answer is #{randomNumber}." interpolate println;
)
);
//For 3.5
Matrix := List clone
Matrix dim := method(x, y,
self setSize(x) map(list() setSize(y))
)
Matrix set := method(x, y, v,
self at(x) atPut(y, v); self
)
Matrix get := method(x, y,
self at(x) at(y)
)
//For 3.7
matrix := list(
list( "A", 0, 0 ),
list( "0", "B", "0" ),
list( "0", "0", "C" )
);
filePath := "./data.txt"
dataFile := File open(filePath)
dataFile write(matrix serialized)
dataFile close
doMatrix := doFile(filePath)
("Matrix (size: " .. matrix size() .. ")") println();
matrix println();
("doFile Matrix (size: " .. doMatrix size() .. ")") println();
doMatrix println();
//For 3.4
//if any item is not kind of Number then raise an Exception
List myAverage := method(if(self size==0, return,
self flatten select(isKindOf(Number)
ifFalse(Exception raise("NaN")))
average))
//For 3.6
Matrix transpose := method(
new_x := self at(0) size;
new_y := self size;
new := Matrix dim(new_x, new_y);
self foreach(x, xarray,
xarray foreach(y, yv, new set(y, x, self get(x, y))));
return(new)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment