This file contains hidden or 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
// 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 |
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.
This was my attempt at 8 of those 10 items: http://news.ycombinator.com/item?id=2612686
ReplyDelete/I3az/