Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: nom
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 96 lines (81 sloc) 2.593 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
class LoL { # declared in BOOTSTRAP
    # class LoL is List {
    # has Mu $!descriptor;

    method new(|) {
        my Mu $args := nqp::p6argvmarray();
        nqp::shift($args);
        nqp::p6list($args, self.WHAT, Mu);
    }
    
    method at_pos($pos is copy) {
        $pos = $pos.Int;
        self.exists_pos($pos)
          ?? nqp::findmethod(List, 'at_pos')(self, $pos)
          !! nqp::p6bindattrinvres(my $v, Scalar, '$!whence',
                 -> { nqp::findmethod(List, 'STORE_AT_POS')(self, $pos, $v) } )
    }

    multi method perl(LoL:D \SELF:) {
        self.WHAT.perl ~ '.new(' ~ self.map({.perl}).join(', ') ~ ')'
            ~ ('.item' if nqp::iscont(SELF));
    }

    method REIFY(Parcel \parcel, Mu \nextiter) {
        my Mu $rpa := nqp::getattr(parcel, Parcel, '$!storage');
        my Mu $iter := nqp::iterator($rpa);
        my int $i = 0;
        while $iter {
            nqp::bindpos($rpa, $i, my $v = nqp::shift($iter));
            $i = $i + 1;
        }
        nqp::findmethod(List, 'REIFY')(self, parcel, nextiter)
    }

    method STORE_AT_POS(\pos, Mu $v is copy) {
        nqp::findmethod(List, 'STORE_AT_POS')(self, pos, $v);
    }

}


sub infix:<X>(|lol) {
    my @l;
    my \elem = lol[0];
    @l[0] = nqp::iscont(elem) ?? (elem,).list.item !! (elem,).flat.item;
    my int $i = 0;
    my int $n = lol.elems - 1;
    my Mu $v := nqp::list();
    gather {
        while $i >= 0 {
            if @l[$i].gimme(1) {
                nqp::bindpos($v, $i, @l[$i].shift);
                if $i >= $n { take nqp::p6parcel(nqp::clone($v), nqp::null()) }
                else {
                    $i = $i + 1;
                    my \elem = lol[$i];
                    @l[$i] = nqp::iscont(elem) ?? (elem,).list.item !! (elem,).flat.item;
                }
            }
            else { $i = $i - 1 }
        }
    }
}

sub infix:<Z>(|lol) {
    my $arity = lol.elems;
    my @l = eager for ^$arity -> $i {
            my \elem = lol[$i]; # can't use mapping here, mustn't flatten

            if nqp::iscont(elem) { (elem,).list.item }
            else { (elem,).flat.item }
        }

    gather {
        loop {
            my \p = @l.map: { last unless .gimme(1); .shift }
            last if p.elems < $arity;
            take-rw p.Parcel;
        }
    }
}

my &zip := &infix:<Z>;

sub roundrobin(**@lol) {
    my @l = @lol.map({ (.flat,).list.item });
    gather {
        my $p;
        while $p := @l.grep(*.Bool).map(*.shift).eager.Parcel {
            take $p;
        }
    }
}

# vim: ft=perl6 expandtab sw=4
Something went wrong with that request. Please try again.