Saturday, October 8, 2011

10 Io one liners to impress your friends

It's been ages since I've done anything with the Io language. But after seeing this spate of 10 [language] one liners to impress your friends in Davide Varvello's post I thought I would spend the evening trying it with Io. Here's the gist:
// 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
view raw one_liners.io hosted with ❤ by GitHub
According to Davide's post the others in this category so far are:
Ruby
Scala
CoffeeScript
Haskell
Clojure
Python
Groovy
I'd love to see more. It's a great little activity to get started with a language.

1 comment:

  1. This was my attempt at 8 of those 10 items: http://news.ycombinator.com/item?id=2612686

    /I3az/

    ReplyDelete