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

May 23, 2008

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

Joc

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?

Bucciarati

What about using λ ?

Dan Yoder

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.

Dave Thomas

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


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


Dave

Lawrence Pit

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.

ste

@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 "->"?

Simon B.

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.

markus

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.

Rob Grainger

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)

Rob Grainger

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)

Rob Ortiz

If you want to pass your closure as a option to a hash you get something that looks like this:

method_call(:foo => -> { puts 'ruby is not Erlang!!!'})

this is a much easier read in 1.8

method_call(:foo => lambda { puts 'ruby is not Erlang!!!'})


so why not just include both and end all the fuss ....

The comments to this entry are closed.

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.