Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Haskell-like list comprehensions in Ruby
$stack, $draws = [], {}
def method_missing *args
return if args[0][/^to_/]
$stack << args.map { |a| a or $stack.pop }
$draws[$stack.pop(2)[0][0]] = args[1] if args[0] == :<
end
class Array
def +@
$stack.flatten!
keys = $draws.keys & $stack
draws = $draws.values_at *keys
comp = draws.shift.product(*draws).map do |draw|
$stack.map { |s| draw[keys.index s] rescue s }.reduce do |val, cur|
op = Symbol === cur ? [:send, :method][val.method(cur).arity] : :call
val.send op, cur
end
end
$stack, $draws = [], {}
Symbol === last ? comp.select(&pop) : comp
end
def -@
case map(&:class).index Range
when 0 then first.to_a
when 1 then [first] + last.step(last.min.ord - first.ord).to_a
else self
end
end
end
foo =+ [x * y | x <- [1..3], y <- [4..6]]
# [4, 5, 6, 8, 10, 12, 12, 15, 18]
bar =+ [a + b | a <- ['n','p'..'t'], b <- %w[a i u e o]]
# ["na", "ni", "nu", "ne", "no", "pa", "pi", "pu", "pe", "po", "ra", "ri", "ru", "re", "ro", "ta", "ti", "tu", "te", "to"]
baz =+ [i ** 2 / 3 | i <- [3,6..100], :even?]
# [12, 48, 108, 192, 300, 432, 588, 768, 972, 1200, 1452, 1728, 2028, 2352, 2700, 3072]
quux =+ [s.size.divmod(2) | s <- %w[Please do not actually use this.]]
# [[3, 0], [1, 0], [1, 1], [4, 0], [1, 1], [2, 1]]
@tomekw

This comment has been minimized.

Show comment
Hide comment

tomekw commented Aug 15, 2012

Nice!

@be

This comment has been minimized.

Show comment
Hide comment

be commented Aug 15, 2012

cool!

@rahult

This comment has been minimized.

Show comment
Hide comment

rahult commented Aug 16, 2012

sweet!

@pmarreck

This comment has been minimized.

Show comment
Hide comment
@pmarreck

pmarreck Aug 16, 2012

Seriously though, couldn't you make this non-global by only having it work within a block passed to a method named something like list_comprehend ?

Seriously though, couldn't you make this non-global by only having it work within a block passed to a method named something like list_comprehend ?

@elight

This comment has been minimized.

Show comment
Hide comment
@elight

elight Aug 16, 2012

Holy shit.....

elight commented Aug 16, 2012

Holy shit.....

@elight

This comment has been minimized.

Show comment
Hide comment
@elight

elight Aug 16, 2012

Gem it. Want. Want now.

elight commented Aug 16, 2012

Gem it. Want. Want now.

@FranklinChen

This comment has been minimized.

Show comment
Hide comment
@FranklinChen

FranklinChen Aug 16, 2012

This is so, so "wrong", yet thrilling.

This is so, so "wrong", yet thrilling.

@andkerosine

This comment has been minimized.

Show comment
Hide comment
@andkerosine

andkerosine Aug 16, 2012

@pmarreck It could definitely be cleaned up and made passably useful by wrapping everything in, say, a Raskell do block, but Kernel#method_missing is the only way to capture the bare variables that help to fully emulate Haskell's syntax, so it'd still be pretty unpleasant. This was mostly just an interesting proof of concept.

@elight I just can't bring myself to do it, at least not while there are so many holes: the magical undefined variables become nil after they're used once, so you'd have to ensure you're always using a different set of names in each comprehension; there's no way to filter a specific draw, only the entire result set; I couldn't figure out how to cleanly allow for previous variables to constrain subsequent ranges, and as long as Infinity forces a range into floats, the real power of Haskell's list comprehensions isn't available.

@FranklinChen I'm looking forward to laziness and refinements in 2.0, when something like this might actually be considered sane.

Owner

andkerosine commented Aug 16, 2012

@pmarreck It could definitely be cleaned up and made passably useful by wrapping everything in, say, a Raskell do block, but Kernel#method_missing is the only way to capture the bare variables that help to fully emulate Haskell's syntax, so it'd still be pretty unpleasant. This was mostly just an interesting proof of concept.

@elight I just can't bring myself to do it, at least not while there are so many holes: the magical undefined variables become nil after they're used once, so you'd have to ensure you're always using a different set of names in each comprehension; there's no way to filter a specific draw, only the entire result set; I couldn't figure out how to cleanly allow for previous variables to constrain subsequent ranges, and as long as Infinity forces a range into floats, the real power of Haskell's list comprehensions isn't available.

@FranklinChen I'm looking forward to laziness and refinements in 2.0, when something like this might actually be considered sane.

@moonglum

This comment has been minimized.

Show comment
Hide comment
@moonglum

moonglum Aug 17, 2012

This is amazing!

This is amazing!

@gcao

This comment has been minimized.

Show comment
Hide comment
@gcao

gcao Aug 17, 2012

This is crazy stuff! I'm still trying to understand how this works. If you could provide some inline comment, it'll be great.

gcao commented Aug 17, 2012

This is crazy stuff! I'm still trying to understand how this works. If you could provide some inline comment, it'll be great.

@styx

This comment has been minimized.

Show comment
Hide comment

styx commented Aug 18, 2012

👍

@towynlin

This comment has been minimized.

Show comment
Hide comment
@towynlin

towynlin Aug 18, 2012

Wow, totally ridonkulous. Nicely done!

Wow, totally ridonkulous. Nicely done!

@rdp

This comment has been minimized.

Show comment
Hide comment
@rdp

rdp Aug 20, 2012

now if it could avoid using method_missing somehow...

rdp commented Aug 20, 2012

now if it could avoid using method_missing somehow...

@gsinclair

This comment has been minimized.

Show comment
Hide comment
@gsinclair

gsinclair Aug 21, 2012

Wow, I'm amazed. I really wish Ruby supported this style.

Wow, I'm amazed. I really wish Ruby supported this style.

@thejoecarroll

This comment has been minimized.

Show comment
Hide comment
@thejoecarroll

thejoecarroll May 12, 2013

Nice work. Let's see if this can be made fly with 2.0...

Nice work. Let's see if this can be made fly with 2.0...

@xatier

This comment has been minimized.

Show comment
Hide comment
@xatier

xatier May 12, 2013

Really cool!

xatier commented May 12, 2013

Really cool!

@pkondzior

This comment has been minimized.

Show comment
Hide comment

Yuck

@dansimco

This comment has been minimized.

Show comment
Hide comment
@dansimco

dansimco May 12, 2013

Bonus points for the name!

Bonus points for the name!

@appplemac

This comment has been minimized.

Show comment
Hide comment
@appplemac

appplemac May 12, 2013

Amazing! Some comments would be useful, though.

Amazing! Some comments would be useful, though.

@RichardFreeman

This comment has been minimized.

Show comment
Hide comment

so cool!

@vseloved

This comment has been minimized.

Show comment
Hide comment
@vseloved

vseloved May 12, 2013

So, what about using it in place of function argument?

Like:
[x * y | x <- [1..3], y <- [4..6]].each { |a| print a }

So, what about using it in place of function argument?

Like:
[x * y | x <- [1..3], y <- [4..6]].each { |a| print a }

@zhangandyx

This comment has been minimized.

Show comment
Hide comment

Ruby 2.1!

@linduxed

This comment has been minimized.

Show comment
Hide comment
@linduxed

linduxed May 12, 2013

Looks really nice!

Looks really nice!

@freakhill

This comment has been minimized.

Show comment
Hide comment
@freakhill

freakhill May 12, 2013

I played a bit with the idea to get that. Trying out ruby2 refinements. Too lazy for the "|" support but can do conditions.
https://gist.github.com/freakhill/5564328

I played a bit with the idea to get that. Trying out ruby2 refinements. Too lazy for the "|" support but can do conditions.
https://gist.github.com/freakhill/5564328

@igbanam

This comment has been minimized.

Show comment
Hide comment
@igbanam

igbanam May 12, 2013

+1 for "Raskell". Pun in "Rascal"?

igbanam commented May 12, 2013

+1 for "Raskell". Pun in "Rascal"?

@darth10

This comment has been minimized.

Show comment
Hide comment
@darth10

darth10 May 13, 2013

Awesome job! ❤️ Gem it please?

darth10 commented May 13, 2013

Awesome job! ❤️ Gem it please?

@wteuber

This comment has been minimized.

Show comment
Hide comment
@wteuber

wteuber May 15, 2013

"Haskell-like" without lambdas?
Even if it doesn't look as much like Haskell, this snippet is much closer to the Haskell principles, without abusing Ruby.

foo = ->(x=(1..3), y=(4..6)){x.flat_map{|a| y.map{|b| a*b}}}.call

(Of course you usually wouldn't need to call any lambdas before actually executing the application)

wteuber commented May 15, 2013

"Haskell-like" without lambdas?
Even if it doesn't look as much like Haskell, this snippet is much closer to the Haskell principles, without abusing Ruby.

foo = ->(x=(1..3), y=(4..6)){x.flat_map{|a| y.map{|b| a*b}}}.call

(Of course you usually wouldn't need to call any lambdas before actually executing the application)

@hauleth

This comment has been minimized.

Show comment
Hide comment
@hauleth

hauleth May 18, 2013

Nice but for Ruby I prefer using Ruby syntax:

(1..3).zip(4..6).map { |a, b| a * b }

hauleth commented May 18, 2013

Nice but for Ruby I prefer using Ruby syntax:

(1..3).zip(4..6).map { |a, b| a * b }
@chrislerum

This comment has been minimized.

Show comment
Hide comment
@chrislerum

chrislerum May 19, 2013

I'd humbly like to up-vote the previous comment.

I'd humbly like to up-vote the previous comment.

@whistler

This comment has been minimized.

Show comment
Hide comment
@whistler

whistler May 19, 2013

Thats quite brilliant, the author does say "Please do not actually use this."
@hauleth zip is a bit different, list comprehensions are more like cross products:

(1..3).to_a.product((4..6).to_a).map{|a,b| a*b}

or you could do a double map_flat like @wteuber.

Thats quite brilliant, the author does say "Please do not actually use this."
@hauleth zip is a bit different, list comprehensions are more like cross products:

(1..3).to_a.product((4..6).to_a).map{|a,b| a*b}

or you could do a double map_flat like @wteuber.

@josiah14

This comment has been minimized.

Show comment
Hide comment
@josiah14

josiah14 Apr 16, 2015

Impressive.

Impressive.

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