emp_sort: Sort a list of employees by salary in 4 languages
[experiments.git] / emp_sort / emp_sort.gist
1 Clojure
2
3 (def emps (map (fn [x] {:name (str "employee" x) :salary (* 100 x)}) (take 10 (range 1 10))))
4 (take (/ (count emps) 10) (reverse (sort-by :salary emps)))
5
6 Haskell
7
8 import Data.List (sortBy)
9
10 data Employee = Employee {name :: String, salary :: Double } deriving (Show)
11 let emps = map (\x -> Employee ("emp" ++ show x) (x * x)) [1..10] in take ((length emps) `div` 10) (sortBy (\x y -> compare (salary y) (salary x)) emps)
12
13 Scala
14
15 case class Employee(name: String, salary: Double)
16 val emps = (1 to 10).map(i => Employee("employee" + i, i * 100))
17 emps.sortBy(_.salary).reverse.take(math.ceil(emps.size / 10.0))
18
19 Python
20
21 emps = {"employee"+str(i): i*100 for i in range(1, 11)}
22 [(key, value) for key, value in sorted(emps.items(), key=lambda (k,v): (-v,k))][0:len(emps)/10]