4 ms·
> but I tend to write very functional code A bit offtopic, but since you said that and you seem to be both fond of functional programming and in touch with lat
by nnq 14y ago
> but I tend to write very functional code
A bit offtopic, but since you said that and you seem to be both fond of functional programming and in touch with latest ruby developments, you're probably a good person to ask: How the heck do you do "normal/proper" currying in Ruby? And how do you do it with keyword arguments? (I'm trying to compile a list of functional-idiom examples in popular dynamic language, and when I reached Ruby and currying, all the snippets I found made me go "WTF?!"...)
- tomstuart 14y agoRuby only has built-in support for currying procs: >> add = -> x, y { x + y } => #<Proc (lambda)> >> add.call(3, 5) => 8 >> add.call(3) ArgumentError: wrong number of arguments (1 for 2) >> curried_add = add.curry => #<Proc (lambda)> >> add_three = curried_add.call(3) => #<Proc (lambda)> >> add_three.call(5) => 8 If you want a no-brainer way of currying methods, you're SOL.
- donw 14y agoThat's pretty much it; the only other way is to build a function combinator from a Proc or Lambda: irb(main):004:0> multiply = -> a, b { a * b } => #<Proc:0x007fe2822029e0@(irb):4 (lambda)> irb(main):006:0> square = -> a { multiply.call(a, a) } => #<Proc:0x007fe2840746a8@(irb):6 (lambda)> irb(main):007:0> square.call(3) => 9
- jroesch 14y agoYou can do: "Hello".method(:split).to_proc.curry[""] => ['H', 'e', 'l', 'l', 'o'] If you want to even make the receiver an argument it would take a little more hoop jumping, but with a tiny monkey patch you can enable something like this: "Hello".curry.split[""]