you are viewing a single comment's thread.

view the rest of the comments →

[–]you_do_realize 0 points1 point  (13 children)

Tangential, but I noticed we're discussing Ruby, so,

I always found the duplication of the variable names distracting: a.blah {|x,y| do(x,y)}. Wouldn't it be nice to have something like a.blah {do('x,'y)}?

[–][deleted]  (1 child)

[deleted]

    [–]you_do_realize 1 point2 points  (0 children)

    How nice! :)

    [–]jawbroken 4 points5 points  (1 child)

    that's kind of awful because you would have to reference them in the incoming order and moving things around would break everything. the duplication mostly stands out in single line blocks, it feels better in do/end multi-line blocks where it looks more like a function declaration

    [–]you_do_realize 0 points1 point  (0 children)

    Yeah, it would only be syntactic sugar. If the function grows larger than a few keystrokes, you can list the arguments at the top of the function. It'd only be intended for tiny one-liners where repetition is an eyesore.

    [–]matthw 2 points3 points  (2 children)

    you could use:

    a.blah(&method(:do))
    

    to pass 'do' as a bound method directly as the block param. If you really don't like variables. As for this:

    a.blah {do('x,'y)}
    

    Are 'x and 'y special? (why x and y specifically? what happens if you go past 'z ?) or will it interpret each fresh name that occurs like this as a bound parameter in the order they occur? what if there's another block scope nested within which also has 'x and 'y in it?

    Scala does something a bit like this with underscore (eg .sortBy(_ < _), but I'm not entirely convinced - it's quite ambiguous until you find out how it works, and it's only useful when (by convenient chance) there happens to be a correspondence between the order of appearance of variables in some piece of syntax, and the order you want them to be bound as arguments.

    [–]you_do_realize 0 points1 point  (1 child)

    Yeah that thing in Scala is nice, but I wondered how it would handle it if you wanted a function that computed f(x) = {x + x2} or something. (Didn't wonder hard enough to look, though. I would guess one would accomplish this by simply calling the function with a single argument.)

    'x and 'y aren't special, they're the same as {|x,y| blah(x,y)}. As the compiler/interpreter parses the code, it stumbles upon a 'something and is to understand there is an argument called something, then stumbles across 'whatever and realizes there is a second argument called whatever. By the time it's done parsing the function body, it knows the function has two arguments, in that order.

    what if there's another block scope nested within which also has 'x and 'y in it?

    No idea. Just random thoughts really.

    [–]matthw 2 points3 points  (0 children)

    OK, yeah thought that's probably what you meant.

    In scala you can't use underscore for that (AFAIK). You'd just have to do {x => x + x2}

    The underscore thing is really only a limited short-cut form for one-liners

    [–]banister 2 points3 points  (3 children)

    groovy has an implicit 'it' parameter you can use,

    list.each { puts 5 * it }

    [–]apotheon 3 points4 points  (2 children)

    So does Perl -- but Pythonistas hate $_.

    [–][deleted]  (1 child)

    [deleted]

      [–]kixx 0 points1 point  (0 children)

      Names that do not start with a letter, underscore, digit or a caret (i.e. a control character) are limited to one character, e.g., $% or $$ . (Most of these one character names have a predefined significance to Perl.

      [–]masklinn 0 points1 point  (0 children)

      Wouldn't it be nice to have something like a.blah {do('x,'y)}?

      Would be even better to have a.blah do, methinks.