http://qs321.pair.com?node_id=194139


in reply to Re: Re: Isn't that nice?
in thread Isn't that nice?

Forgive the duff syntax, been busy and I can't find an example, I'd have thought the lecture notes would be available online somewhere.., and I can't recall all the details precisely.

sub divide( $x, $y ) { $x / $y } sub half assuming divide( $y=2 )
Uh does that look familiar to anyone?

--

Brother Frankus.

Replies are listed 'Best First'.
Re: Re: Re: Re: Isn't that nice?
by TimToady (Parson) on Aug 30, 2002 at 22:38 UTC
    Probably more like:
    my &half := &divide.assuming( $y => 2 )
    But that's the general idea.

    Larry

      Apart from being substantially prettier, is this functionally or efficiently different from

      my $half = sub { $_[1]=2; goto ÷ }; print $half->( 120 ),$/; __END__ 60
      ?
      Well It's better than the Abottoire, but Yorkshire!
Re: Re: Re: Re: Isn't that nice?
by pdcawley (Hermit) on Aug 30, 2002 at 20:46 UTC
    It's just currying. The syntax is unique to perl, but the semantics are reasonably common in functional programming languages. The 'new' thing about perl 6's 'assuming' operator is that, according to Damian, it doesn't actually create a new function, but that's not really going to be user visible.
Re: Re: Re: Re: Isn't that nice?
by hding (Chaplain) on Aug 30, 2002 at 18:15 UTC

    Yes, it looks a little bit like OCaml (i.e. I'm not sure this has never been seen in any other language). :-)

    # let divide ~x ~y = x / y;; val divide : x:int -> y:int -> int = <fun> # let half = divide ~y:2;; val half : x:int -> int = <fun> # half 120;; - : int = 60

Re: Re: Re: Re: Isn't that nice?
by blssu (Pilgrim) on Aug 30, 2002 at 16:57 UTC
    The mutant offspring of COBOL and ML? You want me to be *familiar* with something like that?!
Re: Re: Re: Re: Isn't that nice?
by ichimunki (Priest) on Aug 30, 2002 at 21:03 UTC
    Is that really going to be in Perl 6 like that?

    How is that an improvement over sub half { divide($_[0]/2) };? Especially when you consider that to write sub half assuming... as shown you have (apparently) to know that divide has a $y to which to assign 2.

      You can create functions with defaults. I don't know about you, but that's an instant winner for me. And you can reduce temporary variables by setting up a function in stages, perhaps by asking the user for the parameters one by one, and then it fires when you have them all filled in. Saves temporary variables and looks neater. Of course you don't need to use this - you can keep writing temporary variables if you really want.

      As I understand it, this is another effect of the much repeated 'closures are first class objects'.

      ____________________
      Jeremy
      I didn't believe in evil until I dated it.