Initial Commit
This commit is contained in:
789 files changed
+149185
No files matched your search
@@ -0,0 +1,547 @@
|
||||
#! @file adapters.sakura
|
||||
#! @author John Christman (sorakatadzuma@gmail.com)
|
||||
#! @copyright Malunal Studios, LLC.
|
||||
#! @license MIT
|
||||
#!
|
||||
#! Contains the adapters, each the iterator one of `iterator`'s defaults makes of
|
||||
#! another, which it owns: what it gives, and, where what it adapts knows how
|
||||
#! many elements it has left or walks backward, how many it has left or its
|
||||
#! elements from the far end. Each is named for what it does, and only written
|
||||
#! out where a program keeps one in a field.
|
||||
|
||||
module core;
|
||||
|
||||
|
||||
#! What `map` makes: what a function makes of each element.
|
||||
#! @tparam T What the iterator it adapts gives.
|
||||
#! @tparam U What the function gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The function.
|
||||
pub struct mapped<T, U, I, F> : iter<U> where I: iter<T> where F: invokable<def(T): U> {
|
||||
var _from: I;
|
||||
var _f: F;
|
||||
}
|
||||
|
||||
#! Gives what the function makes of the next element.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe what it makes; otherwise, `nothing`.
|
||||
pub def mapped<T, U, I, F>::next(&mut this): maybe<U> {
|
||||
if this._from.next() is some(x) {
|
||||
return this._f(move(&mut x));
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def mapped<T, U, I, F>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! Gives what the function makes of the element at the far end.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe what it makes; otherwise, `nothing`.
|
||||
pub def mapped<T, U, I, F>::next_back(&mut this): maybe<U> where I: backward_iterator<T> {
|
||||
if this._from.next_back() is some(x) {
|
||||
return this._f(move(&mut x));
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `filter` makes: each element a test passes.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The test.
|
||||
pub struct filtered<T, I, F> : iter<T> where I: iter<T> where F: invokable<def(&T): bool> {
|
||||
var _from: I;
|
||||
var _test: F;
|
||||
}
|
||||
|
||||
#! Gives the next element the test passes.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def filtered<T, I, F>::next(&mut this): maybe<T> {
|
||||
while this._from.next() is some(x) {
|
||||
return move(&mut x) if this._test(&x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Gives the element nearest the far end the test passes.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def filtered<T, I, F>::next_back(&mut this): maybe<T> where I: backward_iterator<T> {
|
||||
while this._from.next_back() is some(x) {
|
||||
return move(&mut x) if this._test(&x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `filter_map` makes: what a function makes of each element, where it
|
||||
#! makes something.
|
||||
#! @tparam T What the iterator it adapts gives.
|
||||
#! @tparam U What the function may give.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The function.
|
||||
pub struct filter_mapped<T, U, I, F> : iter<U> where I: iter<T> where F: invokable<def(T): maybe<U>> {
|
||||
var _from: I;
|
||||
var _f: F;
|
||||
}
|
||||
|
||||
#! Gives the next thing the function makes.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe what it makes; otherwise, `nothing`.
|
||||
pub def filter_mapped<T, U, I, F>::next(&mut this): maybe<U> {
|
||||
while this._from.next() is some(x) {
|
||||
var made := this._f(move(&mut x));
|
||||
if made is some(&mut kept) {
|
||||
return move(kept);
|
||||
}
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! Gives the thing the function makes nearest the far end.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe what it makes; otherwise, `nothing`.
|
||||
pub def filter_mapped<T, U, I, F>::next_back(&mut this): maybe<U> where I: backward_iterator<T> {
|
||||
while this._from.next_back() is some(x) {
|
||||
var made := this._f(move(&mut x));
|
||||
if made is some(&mut kept) {
|
||||
return move(kept);
|
||||
}
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `enumerate` makes: each element with its index, counting from 0.
|
||||
#! @tparam T What the iterator it adapts gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct enumerated<T, I> : iter<pair<u64, T>> where I: iter<T> {
|
||||
var _from: I;
|
||||
var _at: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element with its index.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the pair; otherwise, `nothing`.
|
||||
pub def enumerated<T, I>::next(&mut this): maybe<pair<u64, T>> {
|
||||
if this._from.next() is some(x) {
|
||||
val at := this._at;
|
||||
this._at++;
|
||||
return pair::<u64, T> { first = at, second = move(&mut x) };
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def enumerated<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! Gives the element at the far end with its index, which how many are left
|
||||
#! says.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the pair; otherwise, `nothing`.
|
||||
pub def enumerated<T, I>::next_back(&mut this): maybe<pair<u64, T>> where I: backward_iterator<T>, sized_iterator<T> {
|
||||
val left := this._from.remaining();
|
||||
if this._from.next_back() is some(x) {
|
||||
return pair::<u64, T> { first = this._at + left - 1, second = move(&mut x) };
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `zip` makes: the elements of two iterators side by side, until either
|
||||
#! ends.
|
||||
#! @tparam T What the first gives.
|
||||
#! @tparam U What the second gives.
|
||||
#! @tparam I The first.
|
||||
#! @tparam J The second.
|
||||
pub struct zipped<T, U, I, J> : iter<pair<T, U>> where I: iter<T> where J: iter<U> {
|
||||
var _from: I;
|
||||
var _other: J;
|
||||
}
|
||||
|
||||
#! Gives the next element of each, side by side.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the pair; otherwise, `nothing` once either ends.
|
||||
pub def zipped<T, U, I, J>::next(&mut this): maybe<pair<T, U>> {
|
||||
var first := this._from.next();
|
||||
if first is not some(&mut a) {
|
||||
return nothing {};
|
||||
}
|
||||
var second := this._other.next();
|
||||
if second is not some(&mut b) {
|
||||
return nothing {};
|
||||
}
|
||||
return pair::<T, U> { first = move(a), second = move(b) };
|
||||
}
|
||||
|
||||
#! How many pairs it has left: as many as the shorter has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def zipped<T, U, I, J>::remaining(&this): u64 where I: sized_iterator<T> where J: sized_iterator<U> {
|
||||
val a := this._from.remaining();
|
||||
val b := this._other.remaining();
|
||||
return a < b ? a : b;
|
||||
}
|
||||
|
||||
#! What `chain` makes: the elements of one iterator, then another's.
|
||||
#! @tparam T What both give.
|
||||
#! @tparam I The first.
|
||||
#! @tparam J The second.
|
||||
pub struct chained<T, I, J> : iter<T> where I: iter<T> where J: iter<T> {
|
||||
var _first: I;
|
||||
var _second: J;
|
||||
var _on_second: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element of the first, or once it has ended, of the second.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def chained<T, I, J>::next(&mut this): maybe<T> {
|
||||
if !this._on_second {
|
||||
if this._first.next() is some(x) {
|
||||
return move(&mut x);
|
||||
}
|
||||
this._on_second = true;
|
||||
}
|
||||
return this._second.next();
|
||||
}
|
||||
|
||||
#! How many elements it has left: what both have.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def chained<T, I, J>::remaining(&this): u64 where I: sized_iterator<T> where J: sized_iterator<T> {
|
||||
return (this._on_second ? 0 : this._first.remaining()) + this._second.remaining();
|
||||
}
|
||||
|
||||
#! Gives the element at the far end of the second, or once it has met its
|
||||
#! front, of the first.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def chained<T, I, J>::next_back(&mut this): maybe<T> where I: backward_iterator<T> where J: backward_iterator<T> {
|
||||
if this._second.next_back() is some(x) {
|
||||
return move(&mut x);
|
||||
}
|
||||
return nothing {} if this._on_second;
|
||||
return this._first.next_back();
|
||||
}
|
||||
|
||||
#! What `take` makes: the first elements, up to a count.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct taken<T, I> : iter<T> where I: iter<T> {
|
||||
var _from: I;
|
||||
var _left: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element, while the count lasts.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def taken<T, I>::next(&mut this): maybe<T> {
|
||||
return nothing {} if this._left == 0;
|
||||
this._left--;
|
||||
return this._from.next();
|
||||
}
|
||||
|
||||
#! How many elements it has left: the fewer of the count and what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def taken<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
val held := this._from.remaining();
|
||||
return held < this._left ? held : this._left;
|
||||
}
|
||||
|
||||
#! What `skip` makes: the elements after the first, up to a count.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct skipped<T, I> : iter<T> where I: iter<T> {
|
||||
var _from: I;
|
||||
var _skip: u64;
|
||||
}
|
||||
|
||||
#! Gives the next element, passing over those to skip the first time.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def skipped<T, I>::next(&mut this): maybe<T> {
|
||||
if this._skip != 0 {
|
||||
val n := this._skip;
|
||||
this._skip = 0;
|
||||
return this._from.nth(n);
|
||||
}
|
||||
return this._from.next();
|
||||
}
|
||||
|
||||
#! How many elements it has left: what it adapts has, past those to skip.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def skipped<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
val held := this._from.remaining();
|
||||
return held > this._skip ? held - this._skip : 0;
|
||||
}
|
||||
|
||||
#! Gives the element at the far end, while it is past those to skip.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def skipped<T, I>::next_back(&mut this): maybe<T> where I: backward_iterator<T>, sized_iterator<T> {
|
||||
return nothing {} if this._from.remaining() <= this._skip;
|
||||
return this._from.next_back();
|
||||
}
|
||||
|
||||
#! What `take_while` makes: the elements while a test passes them.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The test.
|
||||
pub struct taken_while<T, I, F> : iter<T> where I: iter<T> where F: invokable<def(&T): bool> {
|
||||
var _from: I;
|
||||
var _test: F;
|
||||
var _done: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element where the test passes it, and ends at the first it
|
||||
#! does not.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def taken_while<T, I, F>::next(&mut this): maybe<T> {
|
||||
return nothing {} if this._done;
|
||||
if this._from.next() is some(x) {
|
||||
return move(&mut x) if this._test(&x);
|
||||
}
|
||||
this._done = true;
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `skip_while` makes: the elements from the first a test does not pass.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The test.
|
||||
pub struct skipped_while<T, I, F> : iter<T> where I: iter<T> where F: invokable<def(&T): bool> {
|
||||
var _from: I;
|
||||
var _test: F;
|
||||
var _past: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element, passing over those the test passes the first time.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def skipped_while<T, I, F>::next(&mut this): maybe<T> {
|
||||
return this._from.next() if this._past;
|
||||
this._past = true;
|
||||
while this._from.next() is some(x) {
|
||||
return move(&mut x) if !this._test(&x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `step` makes: every `n`th element, the first included.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct stepped<T, I> : iter<T> where I: iter<T> {
|
||||
var _from: I;
|
||||
var _by: u64;
|
||||
var _first: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element, passing over those between.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def stepped<T, I>::next(&mut this): maybe<T> {
|
||||
if this._first {
|
||||
this._first = false;
|
||||
return this._from.next();
|
||||
}
|
||||
return this._from.nth(this._by - 1);
|
||||
}
|
||||
|
||||
#! How many elements it has left: every `n`th of what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def stepped<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
val held := this._from.remaining();
|
||||
return this._first ? (held + this._by - 1) / this._by : held / this._by;
|
||||
}
|
||||
|
||||
#! What `rev` makes: the elements from the far end.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts, which walks backward.
|
||||
pub struct reversed<T, I> : iter<T> where I: backward_iterator<T> {
|
||||
var _from: I;
|
||||
}
|
||||
|
||||
#! Gives the element at the far end of what it adapts.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def reversed<T, I>::next(&mut this): maybe<T> {
|
||||
return this._from.next_back();
|
||||
}
|
||||
|
||||
#! Gives the element at the front of what it adapts, its own far end.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def reversed<T, I>::next_back(&mut this): maybe<T> {
|
||||
return this._from.next();
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def reversed<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! What `peekable` makes: the elements, with the next looked at by `peek`
|
||||
#! before it is given.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
pub struct peeking<T, I> : iter<T> where I: iter<T> {
|
||||
var _from: I;
|
||||
var _held: maybe<T>;
|
||||
var _peeked: bool;
|
||||
}
|
||||
|
||||
#! Gives the next element, the one `peek` looked at where it did.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def peeking<T, I>::next(&mut this): maybe<T> {
|
||||
if this._peeked {
|
||||
this._peeked = false;
|
||||
return move(&mut this._held);
|
||||
}
|
||||
return this._from.next();
|
||||
}
|
||||
|
||||
#! Looks at the next element without giving it, so `next` gives it after.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe a reference to the element, which it holds; otherwise,
|
||||
#! `nothing`.
|
||||
pub def peeking<T, I>::peek(&mut this): maybe<&T> {
|
||||
if !this._peeked {
|
||||
this._held = this._from.next();
|
||||
this._peeked = true;
|
||||
}
|
||||
if this._held is some(&x) {
|
||||
return x;
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left: what it adapts has, and the one it holds.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def peeking<T, I>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
var made := this._from.remaining();
|
||||
if this._peeked && this._held is some(&x) {
|
||||
made++;
|
||||
}
|
||||
return made;
|
||||
}
|
||||
|
||||
#! What `flat_map` makes: the elements of each iterator a function makes of
|
||||
#! each element, in turn.
|
||||
#! @tparam T What the iterator it adapts gives.
|
||||
#! @tparam U What the iterators the function makes give.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam J The iterators the function makes.
|
||||
#! @tparam F The function.
|
||||
pub struct flat_mapped<T, U, I, J, F> : iter<U>
|
||||
where I: iter<T> where J: iter<U> where F: invokable<def(T): J> {
|
||||
var _from: I;
|
||||
var _f: F;
|
||||
var _inner: maybe<J>;
|
||||
}
|
||||
|
||||
#! Gives the next element of the iterator it is walking, or of the next it
|
||||
#! makes, once that one ends.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def flat_mapped<T, U, I, J, F>::next(&mut this): maybe<U> {
|
||||
while true {
|
||||
if this._inner is some(&mut inner) {
|
||||
if inner.next() is some(x) {
|
||||
return move(&mut x);
|
||||
}
|
||||
}
|
||||
if this._from.next() is not some(y) {
|
||||
return nothing {};
|
||||
}
|
||||
this._inner = this._f(move(&mut y));
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `copied` makes: a copy of what each reference refers to.
|
||||
#! @tparam X What the references refer to.
|
||||
#! @tparam I The iterator of references it adapts.
|
||||
pub struct copying<X, I> : iter<X> where I: iter<&X> {
|
||||
var _from: I;
|
||||
}
|
||||
|
||||
#! Gives a copy of what the next reference refers to.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the copy; otherwise, `nothing`.
|
||||
pub def copying<X, I>::next(&mut this): maybe<X> {
|
||||
if this._from.next() is some(r) {
|
||||
return copy(r);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def copying<X, I>::remaining(&this): u64 where I: sized_iterator<&X> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! Gives a copy of what the reference at the far end refers to.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the copy; otherwise, `nothing`.
|
||||
pub def copying<X, I>::next_back(&mut this): maybe<X> where I: backward_iterator<&X> {
|
||||
if this._from.next_back() is some(r) {
|
||||
return copy(r);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! What `inspect` makes: the elements as they are, after a function is called on
|
||||
#! each.
|
||||
#! @tparam T What it gives.
|
||||
#! @tparam I The iterator it adapts.
|
||||
#! @tparam F The function.
|
||||
pub struct inspected<T, I, F> : iter<T> where I: iter<T> where F: invokable<def(&T): void> {
|
||||
var _from: I;
|
||||
var _f: F;
|
||||
}
|
||||
|
||||
#! Gives the next element, once the function has seen it.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def inspected<T, I, F>::next(&mut this): maybe<T> {
|
||||
if this._from.next() is some(x) {
|
||||
this._f(&x);
|
||||
return move(&mut x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
|
||||
#! How many elements it has left, which are what it adapts has.
|
||||
#! @param this The iterator.
|
||||
#! @returns How many.
|
||||
pub def inspected<T, I, F>::remaining(&this): u64 where I: sized_iterator<T> {
|
||||
return this._from.remaining();
|
||||
}
|
||||
|
||||
#! Gives the element at the far end, once the function has seen it.
|
||||
#! @param this The iterator.
|
||||
#! @returns Maybe the element; otherwise, `nothing`.
|
||||
pub def inspected<T, I, F>::next_back(&mut this): maybe<T> where I: backward_iterator<T> {
|
||||
if this._from.next_back() is some(x) {
|
||||
this._f(&x);
|
||||
return move(&mut x);
|
||||
}
|
||||
return nothing {};
|
||||
}
|
||||
Reference in new issue
Block a user