News for hackers. Stuff that matters.
Re:Small functions to functionally augment ruby. by burke on Friday September 09, 2011 @09:53PM (#2980536)
I wrote a similar thing a few months ago.

https://github.com/stefanpenner/rebinder/blob/master/lib/reb...

My syntax was far more sarcastic and less legitimate, however. eg:

    add = Fixnum.☃.+
    add3 = add.∂(3)
    assert_equal 7, add3.☏(4)
The fun bit is that this implementation doesn't actually use lambdas, but rather (Unbound)Method instances.
Re:Small functions to functionally augment ruby. by teoryn on Friday September 09, 2011 @09:59AM (#2978093)
Naive fibonacci is a much more interesting example for memoize than factorial:

    fib = +lambda {|n| return 1 if n <= 1; fib[n-2] + fib[n-1]}
Re:Small functions to functionally augment ruby. by irahul on Friday September 09, 2011 @10:27AM (#2978209)
Yes. I added that to tests and README. Except that I have

    fib = +lambda {|n| return n if n <= 1; fib[n-1] + fib[n-1] }
My fibonacci is 0, 1, 1, 2, 3.. and yours is 1, 1, 2, 3... but doesn't make a difference as far as example goes.
Re:Small functions to functionally augment ruby. by phzbOx on Friday September 09, 2011 @09:29AM (#2978009)
What's with the [] notation? Isn't there a way to "overload" the () in ruby? I.e.

(sqr * inc)(5) ?

Re:Small functions to functionally augment ruby. by irahul on Friday September 09, 2011 @10:31AM (#2978224)
Welcome to Ruby - Ruby has procs, blocks, lambdas, methods which are similar yet different.

Blocks aren't objects - when they are converted to objects, they become procs. Procs are like blocks and lambdas are like methods, but you can't call either of them with () syntax.

And then there are multiple ways to call them:

  f.call()
  f[]
  f.()
Take your pick.
Re:Small functions to functionally augment ruby. by happy4crazy on Friday September 09, 2011 @09:49AM (#2978064)
No. The (sqr * inc) part returns a lambda, and in order to call a lambda you need to either .call() it, or you can use the [] syntax.
Re:Small functions to functionally augment ruby. by jablan on Friday September 09, 2011 @09:48AM (#2978061)
Nope. Whenever you use (), the parser assumes a method call. sqr, inc and (sqr * inc) are not methods (but Proc objects), and all you can do is call _their_ methods, such as [].