This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Test; | |
class TimesPi does Real { | |
has Real $.n; | |
method new($n) { | |
self.bless(*, :$n); | |
} | |
method Bridge() { | |
$.n * pi; | |
} | |
method Str() { | |
$.n ~ "π"; | |
} | |
method perl() { | |
$.n ~ "π"; | |
# "TimesPi.new($.n)" | |
} | |
} | |
sub postfix:<π>($n) { | |
TimesPi.new($n); | |
} | |
multi sub infix:<+>(TimesPi $lhs, TimesPi $rhs) { | |
TimesPi.new($lhs.n + $rhs.n); | |
} | |
multi sub infix:<->(TimesPi $lhs, TimesPi $rhs) { | |
TimesPi.new($lhs.n - $rhs.n); | |
} | |
multi sub prefix:<->(TimesPi $n) { | |
TimesPi.new(- $n.n); | |
} | |
multi sub infix:<*>(TimesPi $lhs, Real $rhs) { | |
TimesPi.new($lhs.n * $rhs); | |
} | |
multi sub infix:<*>(Real $lhs, TimesPi $rhs) { | |
TimesPi.new($lhs * $rhs.n); | |
} | |
multi sub infix:<*>(TimesPi $lhs, TimesPi $rhs) { | |
$lhs.Bridge * $rhs.Bridge; | |
} | |
multi sub infix:</>(TimesPi $lhs, Real $rhs) { | |
TimesPi.new($lhs.n / $rhs); | |
} | |
multi sub infix:</>(TimesPi $lhs, TimesPi $rhs) { | |
$lhs.Bridge / $rhs.Bridge; | |
} | |
multi MAIN("test") { | |
plan *; | |
isa_ok 10π, TimesPi, "10π makes a TimesPi object"; | |
isa_ok 10π + 5π, TimesPi, "10π + 5π is still a TimesPi object"; | |
is 10π + 5π, 15π, "10π + 5π = 15π"; | |
isa_ok 10π - 5π, TimesPi, "10π - 5π is still a TimesPi object"; | |
is 10π - 5π, 5π, "10π - 5π = 5π"; | |
is -(5π), (-5)π, "-(5π) = (-5)π"; | |
isa_ok 10π * 5, TimesPi, "10π * 5 is still a TimesPi object"; | |
is 10π * 5, 50π, "10π * 5 = 50π"; | |
isa_ok 5 * 10π, TimesPi, "5 * 10π is still a TimesPi object"; | |
is 5 * 10π, 50π, "5 * 10π = 50π"; | |
is_approx 10π * 5π, 50 * (pi ** 2), "10π * 5π = 50π**2"; | |
is_approx 10π + 4, 10 * pi + 4, "10π + 4 is approximately 10 * pi + 4"; | |
done; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment