Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: lazy-subst
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 233 lines (194 sloc) 7.522 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
my class IO { ... }

my class Cool {

    ## numeric methods

    method abs() { self.Numeric.abs }
    method conj() { self.Numeric.conj }
    method sqrt() { self.Numeric.sqrt }
    method sign() { self.Numeric.sign }
    method rand() { self.Num.rand }
    method sin() { self.Numeric.sin }
    method asin() { self.Numeric.asin }
    method cos() { self.Numeric.cos }
    method acos() { self.Numeric.acos }
    method tan() { self.Numeric.tan }
    method atan() { self.Numeric.atan }
    method atan2($y = 1e0) { self.Numeric.atan2($y.Numeric) }
    method sec() { self.Numeric.sec }
    method asec() { self.Numeric.asec }
    method cosec() { self.Numeric.cosec }
    method acosec() { self.Numeric.acosec }
    method cotan() { self.Numeric.cotan }
    method acotan() { self.Numeric.acotan }
    method sinh() { self.Numeric.sinh }
    method asinh() { self.Numeric.asinh }
    method cosh() { self.Numeric.cosh }
    method acosh() { self.Numeric.acosh }
    method tanh() { self.Numeric.tanh }
    method atanh() { self.Numeric.atanh }
    method sech() { self.Numeric.sech }
    method asech() { self.Numeric.asech }
    method cosech() { self.Numeric.cosech }
    method acosech() { self.Numeric.acosech }
    method cotanh() { self.Numeric.cotanh }
    method acotanh() { self.Numeric.acotanh }
    method cis() { self.Numeric.cis }
    
    proto method log(|$) {*}
    multi method log(Cool:D: ) { self.Numeric.log }
    multi method log(Cool:D: $base) { self.Numeric.log($base.Numeric) }

    proto method exp(|$) {*}
    multi method exp(Cool:D: ) { self.Numeric.exp }
    multi method exp(Cool:D: $base) { self.Numeric.exp($base.Numeric) }


    method roots(Cool $n) { self.Numeric.roots($n) }
    method log10() { self.Numeric.log10 }
    method unpolar($n) { self.Numeric.unpolar($n.Numeric) }

    method round($base = 1) { self.Numeric.round($base) }
    method floor() { self.Numeric.floor }
    method ceiling() { self.Numeric.ceiling }
    method truncate() { self.Numeric.truncate }

    ## string methods

    method bytes() {
        nqp::p6box_i(pir::bytelength__IS(nqp::unbox_s(self.Str)));
    }

    method chars() {
        nqp::p6box_i(nqp::chars(nqp::unbox_s(self.Str)));
    }

    method fmt($format = '%s') {
        nqp::p6box_s(
            nqp::sprintf(nqp::unbox_s($format.Stringy), nqp::list(self))
        )
    }

    method substr($start as Int, $length?) {
        self.Stringy.substr($start, $length);
    }

    method uc() {
        nqp::p6box_s(nqp::uc(nqp::unbox_s(self.Str)))
    }

    method lc() {
        nqp::p6box_s(nqp::lc(nqp::unbox_s(self.Str)))
    }

    method ucfirst() {
        my $self-str = self.Str;
        $self-str eq '' ?? '' !! $self-str.substr(0, 1).uc ~ $self-str.substr(1)
    }

    method lcfirst() {
        my $self-str = self.Str;
        $self-str eq '' ?? '' !! $self-str.substr(0, 1).lc ~ $self-str.substr(1)
    }

    method capitalize() { self.Stringy.capitalize }

    method chomp() {
        self.Str.chomp;
    }

    method chop() {
        self.Str.chop
    }

    method ord() {
        nqp::p6box_i(nqp::ord(nqp::unbox_s(self.Str)))
    }
    method chr() {
        self.Int.chr;
    }

    method flip() {
        nqp::p6box_s(pir::box__PS(nqp::unbox_s(self.Str)).reverse)
    }
    method trans(*@a) { self.Str.trans(@a) }

    proto method index(|$) {*}
    multi method index(Cool $needle, Cool $pos = 0) {
        my $result := nqp::p6box_i(nqp::index(
                nqp::unbox_s(self.Str),
                nqp::unbox_s($needle.Str),
                nqp::unbox_i($pos.Int)
        ));
        # TODO: fail() instead of returning Str
        $result < 0 ?? Str !! $result;
    }

    proto method rindex(|$) {*}
    multi method rindex(Cool $needle, Cool $pos?) {
        if $needle eq '' {
            return $pos.defined && $pos < self.chars
                    ?? $pos
                    !! self.chars;
        }
        my $result = $pos.defined
            ?? nqp::p6box_i(
                pir::rindex__ISSI(
                    nqp::unbox_s(self.Str),
                    nqp::unbox_s($needle.Str),
                    nqp::unbox_i($pos.Int)
                ))
            !! nqp::p6box_i(
                pir::rindex__ISS(
                    nqp::unbox_s(self.Str),
                    nqp::unbox_s($needle.Str),
                ));
        fail "substring not found" if $result < 0;
        $result;
    }

    method ords(Cool:D:) { self.Str.ords }
    proto method split(|$) {*}
    multi method split(Regex $pat, $limit = $Inf, :$all) {
        self.Stringy.split($pat, $limit, :$all);
    }
    multi method split(Cool $pat, $limit = $Inf, :$all) {
        self.Stringy.split($pat.Stringy, $limit, :$all);
    }
    proto method match(|$) {*}
    multi method match(Cool:D: $target, *%adverbs) {
        self.Stringy.match($target, |%adverbs)
    }

    proto method comb(|$) {*}
    multi method comb() { self.Str.comb() }
    multi method comb(Regex $matcher, $limit = $Inf) { self.Str.comb($matcher, $limit) }

    proto method subst(|$) {*}
    multi method subst($matcher, $replacement, *%adverbs) {
        self.Stringy.subst($matcher, $replacement, |%adverbs);
    }

    method sprintf(*@args) { sprintf(self, @args) };
    method printf (*@args) { printf(self, @args) };
    method samecase(Cool:D: Cool $pattern) { self.Stringy.samecase($pattern) }

    method IO() { IO.new(:path(self.Stringy)) }
    method trim () { self.Stringy.trim };
    method trim-leading () { self.Stringy.trim-leading };
    method trim-trailing() { self.Stringy.trim-trailing };

    method eval(*%opts) {
        eval(self.Stringy, |%opts);
    }

    multi method Real() { self.Numeric.Real }
    method Int() { self.Numeric.Int }
    method Num() { self.Numeric.Num }
    method Rat() { self.Numeric.Rat }
}
Metamodel::ClassHOW.exclude_parent(Cool);

sub chop(Cool $s) { $s.chop }
sub chomp(Cool $s) { $s.chomp }
sub flip(Cool $s) { $s.flip }
sub index(Cool $s,$needle,$pos=0) { $s.index($needle,$pos) }
sub lc(Cool $s) { $s.lc }
sub lcfirst(Cool $s) { $s.lcfirst }
sub ord(Cool $s) { $s.ord }
sub substr(Cool $s,$pos,$chars?) { $s.substr($pos,$chars) }
sub uc(Cool $s) { $s.uc }
sub ucfirst(Cool $s) { $s.ucfirst }
proto sub rindex(|$) { * };
multi sub rindex(Cool $s, Cool $needle, Cool $pos) { $s.rindex($needle, $pos) };
multi sub rindex(Cool $s, Cool $needle) { $s.rindex($needle) };

proto sub ords(|$) { * }
multi sub ords(Cool $s) { ords($s.Stringy) }

proto sub comb(|$) { * }
multi sub comb(Regex $matcher, Cool $input, $limit = *) { $input.comb($matcher, $limit) }

proto sub capitalize(|$) { * }
multi sub capitalize(Str:D $x) {$x.capitalize }
multi sub capitalize(Cool $x) {$x.Stringy.capitalize }

sub sprintf(Cool $format, *@args) {
    @args.gimme(*);
    nqp::p6box_s(
        nqp::sprintf(nqp::unbox_s($format.Stringy),
            nqp::clone(nqp::getattr(@args, List, '$!items'))
        )
    );
}

sub printf(Cool $format, *@args) { print sprintf $format, @args };
sub samecase(Cool $string, Cool $pattern) { $string.samecase($pattern) }
sub split($pat, Cool $target, $limit = $Inf, :$all) {
    $target.split($pat, $limit, :$all);
}

sub chars(Cool $x) { $x.Str.chars }
Something went wrong with that request. Please try again.