Well, I'm a little late to the party, especially since there are already examples in Python In response to http://solog.co/47/10-scala-one-liners-to-impress-your-friends/ and http://ricardo.cc/2011/06/02/10-CoffeeScript-One-Liners-to-Impress-Your-Friends.html
1. Multiple Each Item in a List by 2
Some Python love
[x*2 for x in range(1,10)]
And clojure
(map #(* % 2) (range 1 11))
2. Sum a List of Numbers
sum(range(1, 1000))
reduce(lambda t,s : t + s, range(1,1000))
3. Verify if Exists in a String
wordList = ["coffeescript", "eko", "play framework", "and stuff", "falsy"]
tweet = "This is an example tweet talking about javascript and stuff."
any(word in tweet for word in wordList)
[word for word in wordList if word in tweet]
4. Read in a File
open("data.txt").read()
5. Happy Birthday to You!
["Happy Birthday" + ("dear Robert" if i == 3 else "to you") for i in range(1, 4)]
6. Filter list of numbers
passed = []
failed = []
[(passed if score > 60 else failed).append(score) for score in [49, 58, 76, 82, 88, 90]]
7. Fetch and Parse a XML web service
from urllib import urlopen
from xml.dom.minidom import parseString
import json
x = parseString(urlopen("http://search.twitter.com/search.atom?&q=python").read())
j = json.loads(urlopen("http://search.twitter.com/search.json?&q=python").read())
8. Find minimum (or maximum) in a List
min(14, 35, -7, 46, 98)
max(14, 35, -7, 46, 98)
9. Parallel Processing
Doesn't work in the REPL
from multiprocessing import Pool
def f(x):
return x ** x
Pool(2).map(f, range(1,3000))
10. Sieve of Eratosthenes
Code from : http://www.twistypuzzles.com/forum/viewtopic.php?p=85711&sid=ce67e3e1d9887e559c59ee5a7604aff0#p85711
[x for x in range(2,n) if x not in [j for i in range(2,int(n**.5)) for j in range(i*2,n,i)]]