Skip to content

Instantly share code, notes, and snippets.

@sklppr
Created April 21, 2014 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sklppr/11140764 to your computer and use it in GitHub Desktop.
Save sklppr/11140764 to your computer and use it in GitHub Desktop.
Comparison of simple actions in different languages.

Select all elements in a that are greater than 5 and multiply them by 2.

Ruby

a.select { |x| x > 5 }.map { |x| x * 2 }

a.find_all { |x| x > 5 }.collect { |x| x * 2 }

a.inject([]) { |b, x| x > 5 ? (b << x * 2) : b }

Python

map(lambda x: x * 2, filter(lambda x: x > 5, a))

Clojure

(map #(* 2 %1) (filter #(> %1 5) a))

Haskell

map (*2) (filter (>5) a)

map (*2) $ filter (>5) a

[ x * 2 | x <- a, x > 5 ]

MATLAB

a(a > 5) * 2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment