Skip to content

Instantly share code, notes, and snippets.

@jdewind
Created December 1, 2012 19:13
Show Gist options
  • Save jdewind/4184216 to your computer and use it in GitHub Desktop.
Save jdewind/4184216 to your computer and use it in GitHub Desktop.
Fun inline list comprehension in Haskell
[x | x <- (zip [1..] ['A'..'Z']), even (fst x)]
@jdewind
Copy link
Author

jdewind commented Dec 1, 2012

In ruby:

((1..('A'..'Z').to_a.size).zip ('A'..'Z').to_a).select { |(x,y)| x.even? }

@jacius
Copy link

jacius commented Dec 1, 2012

Tightened up ruby version:

xs = ('A'..'Z'); (1..xs.count).zip(xs).select { |i,x| i.even? }

Alternative approach:

('A'..'Z').each_with_index.map { |x,i| [i+1,x] if (i+1).even? }.compact

Neither is quite as elegant as the Haskell, of course.

@jdewind
Copy link
Author

jdewind commented Dec 1, 2012

Thanks John!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment