As someone who is still learning Ruby and would like to learn Haskell I have no idea what is going on here. Would someone be kind enough to explain? Thanks.
It's just adding a syntactic construct to ruby which looks similar to haskells list comprehensions. Explanation for haskell: http://learnyouahaskell.com/starting-out#im-a-list-comprehen...
Haskell style list comprehensions in Ruby
21–29 of 29 posts
Re: Haskell style list comprehensions in Ruby
#22Side question, rant. I love the syntactic side of list or dict comprehension (in python) and often use them but as soon as I have to debug or expand the functionality, I have to slice them into for loop. And then I hate myself for being lazy/clever and more and more, when I start typing a = [, I hear an internal voice: wait, aren't you being wrongfully clever one again? I am the only one?
Dict comprehensions are just a little muddier, even for simple things, but a single line of debug output after the comprehension should confirm whether or not you're getting the structure you expect, provided you are still doing only trivial work inside your comprehension.
Re: Haskell style list comprehensions in Ruby
#23It's neat that Ruby has programmable syntax like this, but Haskell's comprehensions are one of its worst syntactic features IMHO. They're inherently non-composable.
And with -XMonadComprehensions you can have the compiler do that for a whole bunch more Monads.
Re: Haskell style list comprehensions in Ruby
#24As someone who is still learning Ruby and would like to learn Haskell I have no idea what is going on here. Would someone be kind enough to explain? Thanks.
The unary operators `def -@` gets called when you do something like: `-[1,2,3]`.
So in the example code at the bottom, it's doing `bar =+ [x | x Hacking unary operators has a long history in silliness in ruby. See also: https://github.com/jicksta/superators
Re: Haskell style list comprehensions in Ruby
#25Re: Haskell style list comprehensions in Ruby
#26 require 'multiarray'
include Hornetseye
# Object
lazy(4) { |i| i + 2 }
# Sequence(INT):
# [ 2, 3, 4, 5 ]
lazy(3, 2) { |x, y| x }
# MultiArray(INT,2):
# [ [ 0, 1, 2 ],
# [ 0, 1, 2 ] ]
lazy(3, 2) { |x, y| x + 1 }
# MultiArray(INT,2):
# [ [ 1, 2, 3 ],
# [ 1, 2, 3 ] ]
lazy(3, 3) { |x, y| y + 4 }
# MultiArray(INT,2):
# [ [ 4, 4, 4 ],
# [ 5, 5, 5 ],
# [ 6, 6, 6 ] ]
lazy(3, 3) { |x, y| (x + 1) * (y + 4) }
# MultiArray(INT,2):
# [ [ 4, 8, 12 ],
# [ 5, 10, 15 ],
# [ 6, 12, 18 ] ]
lazy { |x,y| Sequence['n', 'p', 'r', 't'][x] + Sequence['a', 'i', 'u', 'e', 'o'][y] }
# MultiArray(OBJECT,2):
# [ [ "na", "pa", "ra", "ta" ],
# [ "ni", "pi", "ri", "ti" ],
# [ "nu", "pu", "ru", "tu" ],
# [ "ne", "pe", "re", "te" ],
# [ "no", "po", "ro", "to" ] ]
s = lazy(33) { |i| 3 * (i+1) }
# Sequence(INT):
# [ 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48, 51, .... ]
s.mask((s % 2).eq(0)).collect { |i| i ** 2 / 3 }
# Sequence(INT):
# [ 12, 48, 108, 192, 300, 432, 588, 768, 972, 1200, 1452, 1728, .... ]
[1] https://github.com/wedesoft/multiarray
[2] http://www.wedesoft.de/hornetseye-api/Re: Haskell style list comprehensions in Ruby
#27Side question, rant. I love the syntactic side of list or dict comprehension (in python) and often use them but as soon as I have to debug or expand the functionality, I have to slice them into for loop. And then I hate myself for being lazy/clever and more and more, when I start typing a = [, I hear an internal voice: wait, aren't you being wrongfully clever one again? I am the only one?
[0] - http://google-styleguide.googlecode.com/svn/trunk/pyguide.ht...
Re: Haskell style list comprehensions in Ruby
#28Wow, that's some nontrivial Ruby, some explanation of this code would be nice, I program in Ruby for some 6 years now and I still had to do a lot of head-scratching to more or less figure this out. I had no idea Ruby allows overloading of prefix operators, for example.
Re: Haskell style list comprehensions in Ruby
#29It's neat that Ruby has programmable syntax like this, but Haskell's comprehensions are one of its worst syntactic features IMHO. They're inherently non-composable.
They're equivalent to do-notation in the List monad so as long as you don't really need the inline syntax you can just mechanically translate it to do nation and back. And with -XMonadComprehensions you can have the compiler do that for a whole bunch more Monads.