#! @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 : iterator where I: iterator where F: invokable { 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::next(&mut this): maybe { 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::remaining(&this): u64 where I: sized_iterator { 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::next_back(&mut this): maybe where I: backward_iterator { 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 : iterator where I: iterator where F: invokable { 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::next(&mut this): maybe { 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::next_back(&mut this): maybe where I: backward_iterator { 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 : iterator where I: iterator where F: invokable> { 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::next(&mut this): maybe { 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::next_back(&mut this): maybe where I: backward_iterator { 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 : iterator> where I: iterator { 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::next(&mut this): maybe> { if this._from.next() is some(x) { val at := this._at; this._at++; return pair:: { 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::remaining(&this): u64 where I: sized_iterator { 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::next_back(&mut this): maybe> where I: backward_iterator, sized_iterator { val left := this._from.remaining(); if this._from.next_back() is some(x) { return pair:: { 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 : iterator> where I: iterator where J: iterator { 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::next(&mut this): maybe> { 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:: { 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::remaining(&this): u64 where I: sized_iterator where J: sized_iterator { 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 : iterator where I: iterator where J: iterator { 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::next(&mut this): maybe { 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::remaining(&this): u64 where I: sized_iterator where J: sized_iterator { 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::next_back(&mut this): maybe where I: backward_iterator where J: backward_iterator { 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 : iterator where I: iterator { 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::next(&mut this): maybe { 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::remaining(&this): u64 where I: sized_iterator { 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 : iterator where I: iterator { 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::next(&mut this): maybe { 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::remaining(&this): u64 where I: sized_iterator { 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::next_back(&mut this): maybe where I: backward_iterator, sized_iterator { 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 : iterator where I: iterator where F: invokable { 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::next(&mut this): maybe { 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 : iterator where I: iterator where F: invokable { 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::next(&mut this): maybe { 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 : iterator where I: iterator { 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::next(&mut this): maybe { 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::remaining(&this): u64 where I: sized_iterator { 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 : iterator where I: backward_iterator { 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::next(&mut this): maybe { 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::next_back(&mut this): maybe { 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::remaining(&this): u64 where I: sized_iterator { 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 : iterator where I: iterator { var _from: I; var _held: maybe; 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::next(&mut this): maybe { 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::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::remaining(&this): u64 where I: sized_iterator { 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 : iterator where I: iterator where J: iterator where F: invokable { var _from: I; var _f: F; var _inner: maybe; } #! 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::next(&mut this): maybe { 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 : iterator where I: iterator<&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::next(&mut this): maybe { 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::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::next_back(&mut this): maybe 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 : iterator where I: iterator where F: invokable { 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::next(&mut this): maybe { 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::remaining(&this): u64 where I: sized_iterator { 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::next_back(&mut this): maybe where I: backward_iterator { if this._from.next_back() is some(x) { this._f(&x); return move(&mut x); } return nothing {}; }