-
Star
(258)
You must be signed in to star a gist -
Fork
(35)
You must be signed in to fork a gist
-
-
Save andkerosine/3356675 to your computer and use it in GitHub Desktop.
$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]] |
Bonus points for the name!
Amazing! Some comments would be useful, though.
so cool!
So, what about using it in place of function argument?
Like:
[x * y | x <- [1..3], y <- [4..6]].each { |a| print a }
Ruby 2.1!
Looks really nice!
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
+1 for "Raskell". Pun in "Rascal"?
Awesome job! ❤️ Gem it please?
"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)
Nice but for Ruby I prefer using Ruby syntax:
(1..3).zip(4..6).map { |a, b| a * b }
I'd humbly like to up-vote the previous comment.
Impressive.
Yuck