« Code in Presentations, Part II | Main | So I've been doing some music... »

May 23, 2008

New lambda syntax in Ruby 1.9

I'm slowly getting used to the new -> way of specifying lambdas in Ruby 1.9. I still feel that, as a notation, it could be clearer. (I'd personally like just plain backslash, because that looks pretty close to a real lambda character, but that's not going to happen.) But having punctuation, rather than the word lambda, makes a surprising difference to the way my eyes read code.

For example, you could write a method that acts like a while loop.

def my_while(cond, &body)
  while cond.call
    body.call
  end
end   

In Ruby 1.8  and 1.9, you could call this as

a = 0
my_while lambda { a < 5 } do
   puts a
   a += 1
end

But my brain finds that seriously hard to scan. The Ruby 1.9 -> syntax makes it slightly (just slightly, mind you) better:

a = 0
my_while  -> { a < 5 }  do
   puts a
   a += 1
end

I suspect this is just a question of time. In a year or so, we'll parse the -> syntax in our heads without thinking twice. Once it does become natural, I suspect we'll find all sorts of new uses for procs.

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d83451c41c69e200e5528d68528834

Listed below are links to weblogs that reference New lambda syntax in Ruby 1.9:

Comments

I agree about the readability but don't think -> is much of an improvement. For one thing it looks like it's pointing the wrong way.

What about just having:

my_while { a < 5 } do
..
end

Just a guess, but couldn't the compiler attempt to treat the contents of the {} as a lambda if it throws "odd number list for Hash" ?

I realise this might cause confusion if you misformat a Hash by accident though.

Or how about

my_while ^{ a < 5 }

which is also similar to the lambda symbol?

What about using λ ?

I don't know. Our Waves web app framework has tried to embrace Ruby's inner lambda, but I don't see this notation helping at all. The arrow suggests some sort of mapping is happening. It is doubly confusing if you use lambdas as values in hashes.

:foo => -> { ... }

Ugh. I think lambda, while obviously a bit more verbose is worth the extra 4 characters. Also, as the above reader suggested, why not really embrace unicode and allow the actual λ character? Especially since the lambda keyword already provides a clear fallback? Ah, well.

The new syntax lets you do stuff you can't do with lambda:


pr = -> (a, b=1, &c) { ... }


Dave

Makes you wonder why this syntax couldn't be used instead:

pr = lambda {|a, b=1, &c| ... }

which would be the logical extension of what we already know.

But apparently ruby's parser can't handle that. They say. Therefor it had to become:

pr = lambda(a, b=1, &c) { ... }

but the parser couldn't handle that either, and therefor they needed a different syntax for lamdba:

pr = -> a, b=1, &c { ... }

I like the new syntax better, except I think -> should be replaced with something else.

@Dan: the new syntax for symbol keys in literal hashes makes things a little less "ugh":

hash = { foo: ->(a,b){ a + b } }


What about making "lambda" a keyword and using that instead of "->"?

Reasons not to make "lamda" a reserved keyword might include playing nice to code that might already use "lamda" for other purposes. And keeping the keyword count low is good and one important first impression ruby made at me.

Having read
http://pragdave.blogs.pragprog.com/pragdave/2008/04/fun-with-ruby-1.html

and
http://www.oreillynet.com/ruby/blog/2008/05/is_qwerty_harming_language_des.html

I say it's time to try. Mapping in a λ on the keyboard couldn't be too difficult? Non-us keyboards have a key with §½ where US-ones have `.
And let's make unicode (UTF-8, unix style - without the unneeded start bytes) the standard encoding for .rb, and everything else.

One way forward could be to get some japanese coders contribute to your source code. Their names in the file header comments are very likely to make text-editors autodetect UTF-8.

I dont know.... I dont really like the ->

I feel it visually distorts the whole code for no apparently needed reason.

λ on the other hand would be a fun character.

Anyway as long as I dont have to use that ugly -> i dont care.

FWIW, I find that it's easier to cope with the use of -> for lambda expressions if you imagine it being rotated 45 degrees CCW - yielding a (nearly) passable imitation of a λ. ;)

(OK it's not perfect)

FWIW, I find that it's easier to cope with the use of -> for lambda expressions if you imagine it being rotated 45 degrees CCW - yielding a (nearly) passable imitation of a λ. ;)

(OK it's not perfect)

Verify your Comment

Previewing your Comment

This is only a preview. Your comment has not yet been posted.

Working...
Your comment could not be posted. Error type:
Your comment has been posted. Post another comment

The letters and numbers you entered did not match the image. Please try again.

As a final step before posting your comment, enter the letters and numbers you see in the image below. This prevents automated programs from posting comments.

Having trouble reading this image? View an alternate.

Working...

Post a comment

Now in Beta

  • Programming Ruby, 3rd Edition
    Third Edition, Covering Ruby 1.9, now available
My Photo

Pragmatic Stuff

Photos

  • www.flickr.com
    This is a Flickr badge showing public photos from pragdave tagged with pragdave_badge. Make your own badge here.

Site Search

  • Google Search

    The web
    PragDave