Skip to content

Instantly share code, notes, and snippets.

@mattyw
Created October 8, 2011 20:45
Show Gist options
  • Save mattyw/1272845 to your computer and use it in GitHub Desktop.
Save mattyw/1272845 to your computer and use it in GitHub Desktop.
10 Io one liners
// 1. Multiply Each Item in a List by 2
list(1,2,3,4,5) map(v, v*2) println
// 2. Sum a List of Numbers
list(1,2,3,4,5) reduce(+) println
// 3. Verify if a Word Exists in a String
list("hello", "foo") contains("hello") println
// 4. Read a File
File with("foo.txt") readLines println
// 5. Happy Birthday to You
for (i, 1, 4, if(i == 3, "Happy Birthday dear Io, " println, "Happy Birthday to You, " println))
// 6. Filter list of numbers
list(49, 58, 76, 82, 88, 90) select(v, v > 60) println
// 7. Fetch and Parse an XML web service
SGML
URL with("http://search.twitter.com/search.atom?&q=scala") fetch asXML println
// 8. Find minimum or maximum in a List
list(1,2,3,4,5) max println
list(1,2,3,4,5) min println
// 9. Parallel Processing
Thread createThread("Date now println")
// 10. Sieve of Eratosthenes
// I've failed to fit the Sieve into one line. But still, 4 lines isn't too bad for a first go.
Range
n := 120
primes := 2 to(n+1) asList
primes foreach(p, for(i, p, n+1, if(primes contains(p*i), primes remove(p*i))))
primes println
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment