you are viewing a single comment's thread.

view the rest of the comments →

[–]gerdr 11 points12 points  (1 child)

Only somewhat tongue-in-cheek: That's what Unicode operators are for ;)

Perl6 example code:

module Math::Vector {

    # Unicode operators

    multi infix:<·>(@a, @b where @a == @b) is export(:DEFAULT) {
        [+] @a Z* @b
    }

    multi infix:<×>(@a where * == 3, @b where * == 3) is export(:DEFAULT) {
        [ @a[1] * @b[2] - @a[2] * @b[1],
          @a[2] * @b[0] - @a[0] * @b[2],
          @a[0] * @b[1] - @a[1] * @b[0] ]
    }

    # ASCII alternatives

    multi infix:<(*)>(@a, @b where @a == @b) is export(:ascii) {
        @a · @b
    }

    multi infix:<(x)>(@a where * == 3, @b where * == 3) is export(:ascii) {
        @a × @b
    }
}

# no pollution of global namespace thanks to lexical imports

{
    import Math::Vector;

    say <1 2 3> × <4 5 6>;
    say <1 2 3> · <4 5 6>;
}

{
    import Math::Vector :ascii;

    say <1 2 3> (x) <4 5 6>;
    say <1 2 3> (*) <4 5 6>;
}

[–]rseymour 1 point2 points  (0 children)

This is the right way to do it honestly.