Last active
July 23, 2019 03:24
-
-
Save ajs/55db217498d3953cb2ea3ee24d96bd05 to your computer and use it in GitHub Desktop.
Numericish.pm6
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 MONKEY; | |
role Numericish [::Basis] does Numeric is export { | |
has Basis $.value; | |
method new(Basis $value) { self.bless :$value, |%_ } | |
multi method gist() { "{self.WHAT.perl}({self.value})" } | |
my @overrides = < | |
Bool Complex DUMP FatRat Int Num Rat Real Str abs acos | |
acosec acosech acosh acotan acotanh asec asech asin asinh atan | |
atan2 atanh base ceiling cis conj cos cosec cosech cosh cotan | |
cotanh exp floor isNaN log log10 polymod pred rand roots | |
round sec sech sign sin sinh sqrt succ tan tanh truncate unpolar | |
>; | |
for @overrides -> $name { | |
my $can = Basis.^can($name); | |
next unless $can; | |
if $can and $can[0].candidates > 1 { | |
say "Not yet sure how to define multi $name"; | |
} else { | |
::?CLASS.^add_method($name, my method (::?CLASS:D: |c) { | |
say "calling: $!value.$name"; | |
my \ret = $!value."$name"(|c); | |
return self.WHAT.new(ret) if ret ~~ Basis; | |
ret | |
}); | |
} | |
} | |
} | |
class Intish does Numericish[Int] {} | |
class Complexish does Numericish[Complex] {} | |
class Numish does Numericish[Num] {} | |
class Ratish does Numericish[Rat] {} | |
class FatRatish does Numericish[FatRat] {} | |
# Results: | |
#$ rm -rf lib/.precomp/ ; perl6 -I lib -e 'use Numericish; say Intish.new(1); say Intish.new(300).cos.^name' | |
#Not yet sure how to define multi Bool | |
#Not yet sure how to define multi DUMP | |
#Not yet sure how to define multi Real | |
#Not yet sure how to define multi Str | |
#Not yet sure how to define multi atan2 | |
#Not yet sure how to define multi base | |
#Not yet sure how to define multi exp | |
#Not yet sure how to define multi log | |
#Not yet sure how to define multi round | |
#Not yet sure how to define multi atanh | |
#(Intish(1)) | |
#Num |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment