Skip to content

Instantly share code, notes, and snippets.

@grondilu
Last active September 20, 2017 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grondilu/996c4d4b8911d4b3ddb5d6fdc095e488 to your computer and use it in GitHub Desktop.
Save grondilu/996c4d4b8911d4b3ddb5d6fdc095e488 to your computer and use it in GitHub Desktop.
unit role Algebra;
proto method ADD($) {*}
multi method ADD(Real $) {...}
multi method ADD(::?CLASS $) {...}
proto method MULTIPLY($) {*}
multi method MULTIPLY(::?CLASS $) {...}
multi method MULTIPLY(Real $) {...}
multi prefix:<->(::?CLASS $a) { (-1)*$a }
multi infix:<+>(::?CLASS $a, ::?CLASS $b) { $a.ADD($b) }
multi infix:<->(::?CLASS $a, ::?CLASS $b) { $a.ADD($b.MULTIPLY(-1)) }
multi infix:<*>(::?CLASS $a, ::?CLASS $b) { $a.MULTIPLY($b) }
multi infix:<*>(Real $a, ::?CLASS $b) { $b.MULTIPLY($a) }
multi infix:<*>(::?CLASS $a, Real $b) { $a.MULTIPLY($b) }
multi infix:</>(::?CLASS $a, Real $b) { $a.MULTIPLY(1/$b) }
multi infix:<**>(::?CLASS $a, 0) { 1 }
multi infix:<**>(::?CLASS $a, 1) { $a }
multi infix:<**>(::?CLASS $a, UInt $n) { ($a*$a)**($n div 2) * $a**($n mod 2) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment