Hacker News new | past | comments | ask | show | jobs | submit login

Sure you know what `sum` means, and maybe even what `product` means.

`sum` is applying `add` repeatedly to a list. `product` is applying `multiply` repeatedly to a list.

The thing is most people use `+` instead of `add`.

Also why use two different things for `add` and `sum`?

How about we use `+` for both `add` and `sum`, since they are basically the same thing:

    1 + 2 + 3
    [+] 1, 2, 3
While we are at it, let's make it so you can also do the same for `multiply` and `product`:

    1 * 2 * 3
    [*] 1, 2, 3
Or perhaps even make it so that you can use any associative infix operator:

    1 ** 2 ** 3
    [**] 1, 2, 3

    1 < 2 < 3
    [<] 1, 2, 3

    (1,2) ⊆ (1,2,3) ⊆ (1,2,3,4,5)
    [⊆] (1,2), (1,2,3), (1,2,3,4,5)

    0 || 0e0 || Nil || 1
    [||] 0, 0e0, Nil, 1
I'm sure you know what exponentiation means, but do you know what word you would use to mean repeatedly applying it to a list? (If you know please tell me, because I sure don't.)

---

After you learn what `sum` means, you still have to learn what `product` means.

However once you learn that you can put any associative infix operator between `[]` and have it apply to a list, you've just learned one thing that is really both of the above, and also many more things.

This makes it quick to learn Perl6. You learn about one thing in one place, and that knowledge applies to everything everywhere. (This is true about almost every feature of Perl6.)

---

If you really don't like the syntax, you don't have to use it.

    # alias the `+` infix operator to a subroutine named `add`
    my &add = &infix:<+>;

    reduce( &add, 1, 2, 3 );
---

I would like to point out that this higher-order feature is basically the same one that allows `+=`.

    my $a = 1;
    $a += 2;
That is because `=` is a meta operator. Meaning it can take an infix operator as a sort-of argument.

    sub infix:<IOP> ( $l, $r ){
        $l + $r
    }

    my $a = 1;
    $a IOP= 2;


    $a *= 1;
    $a /= 2;
    $a max= 10;
I just taught you a second thing that you can now use for most infix operators.

What's more, is that you might have figured out that infix operators are just subroutines with a special name. I didn't even really mean to teach you that.

---

I mean sure a novice is more likely to know what `sum` means. But they have only to read this comment to learn a much larger chunk of Perl6.

Frankly I want newcomers to Perl6 to be confused about `[+]`, because then they are more likely to come to a place where I can quickly teach them everything above.

(Teach a man to fish and he eats for a lifetime. Teach a man to use a net and he can feed his family.)




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: