Skip to content

Instantly share code, notes, and snippets.

@krishicks
Created May 3, 2009 16:55
Show Gist options
  • Save krishicks/106050 to your computer and use it in GitHub Desktop.
Save krishicks/106050 to your computer and use it in GitHub Desktop.
# List comprehension
# Haskell
[x*2 | x <- [1..10]] #=> [2,4,6,8,10,12,14,16,18,20]
# Ruby
(1..10).map { |x| x*2 } #=> [2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
# List comprehension with condition
# Haskell
[x*2 | x <- [1..10], x*2 >= 12] #=> [12,14,16,18,20]
# Ruby
(1..10).map {|x| x*2}.select {|x| x>=12} #=> [12, 14, 16, 18, 20]
# List comprehension with filter
# Haskell
let boomBangs xs = [ if x < 10 then "BOOM!" else "BANG!" | x <- xs, odd x]
boomBangs [7..13] #=> ["BOOM!","BOOM!","BANG!","BANG!"]
# Ruby
boom_bangs = lambda {|xs| xs.select {|x| x.odd?}.map {|x| x<10 ? "BOOM!" : "BANG!"}}
boom_bangs.call(7..13) #=> ["BOOM!", "BOOM!", "BANG!", "BANG!"]
# List comprehension with multiple predicates
# Haskell
[ x | x <- [10..20], x /= 13, x /= 15, x /= 19] #=> [10,11,12,14,16,17,18,20]
# Ruby
(10..20).select {|x| x!=13 && x!=15 && x!=19} #=> [10, 11, 12, 14, 16, 17, 18, 20]
# List comprehension drawing from several lists
# Haskell
[ x*y | x <- [2,5,10], y <- [8,10,11]] #=> [16,20,22,40,50,55,80,100,110]
# Ruby
[2,5,10].product([8,10,11]).flat_map { |x,y| x * y } #=> [16, 20, 22, 40, 50, 55, 80, 100, 110]
# List comprehension drawing from several lists with filter
# Haskell
[ x*y | x <- [2,5,10], y <- [8,10,11], x*y > 50] #=> [55,80,100,110]
# Ruby (literal translation)
[2,5,10].map { |x| [8,10,11].map { |y| x*y } }.flatten.select { |z| z > 50 } #=> [55, 80, 100, 110]
# Ruby (slightly more idiomatic)
[2,5,10].product([8,10,11]).inject([]) { |acc, (x,y)| acc << x*y if x*y > 50; acc } #=> [55, 80, 100, 110]
# List comprehension that combines a list of adjectives and a list of nouns
# Haskell
let nouns = ["hobo","frog","pope"]
let adjectives = ["lazy","grouchy","scheming"]
[adjective ++ " " ++ noun | adjective <- adjectives, noun <- nouns]
#=> ["lazy hobo","lazy frog","lazy pope","grouchy hobo","grouchy frog","grouchy pope","scheming hobo","scheming frog","scheming pope"]
# Ruby
nouns = %w{hobo frog pope}
adjectives = %w{lazy grouchy scheming}
adjectives.map {|adjective| nouns.map{|noun| adjective + " " + noun}}.flatten
#=> ["lazy hobo", "lazy frog", "lazy pope", "grouchy hobo", "grouchy frog", "grouchy pope", "scheming hobo", "scheming frog", "scheming pope"]
# New length method
# Haskell
length' xs = sum [1 | _ <- xs]
length' [1,2,3] #=> 3
# Ruby
length = ->(xs) { xs.fill(1).inject(:+) }
length [[1,2,3]] #=> 3
# List comprehension that removes non-uppercase characters from a string
# Haskell
removeNonUppercase st = [ c | c <- st, c `elem` ['A'..'Z']]
removeNonUppercase "Hahaha! Ahahaha!" #=> "HA"
# Ruby (literal translation)
remove_non_uppercase = lambda { |st| st.each_char.select { |c| ("A".."Z").include? c }.to_s }
remove_non_uppercase.call "Hahaha! Ahahaha!" #=> "HA"
# Ruby (slightly more idiomatic)
remove_non_uppercase = ->(str) { str.gsub /[^A-Z]+/, '' }
remove_non_uppercase["Hahaha! Ahahaha!"] #=> "HA"
# List comprehension that removes all odd numbers without flattening the list
# Haskell
let xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]
[ [ x | x <- xs, even x ] | xs <- xxs] #=> [[2,2,4],[2,4,6,8],[2,4,2,6,2,6]]
# Ruby
xxs = [[1,3,5,2,3,1,2,4,5],[1,2,3,4,5,6,7,8,9],[1,2,4,2,1,6,3,1,3,2,3,6]]
xxs.map { |xs| xs.select(&:even?) } #=> [[2, 2, 4], [2, 4, 6, 8], [2, 4, 2, 6, 2, 6]]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment