iter

external iterator
utils
moon add YumeXi/iter@0.2.8
Download zip
Author
Version
0.2.8
License
BSD-2-Clause
Last updated
11 months ago
Downloads
26
README

#YumeXi/iter

An external iterator library for MoonBit.

#Examples

fn main {
let iter = [1, 2, 3, 4, 5] |> @iter.from_array
while iter.next() is Some(v) && v <= 3 {
println(v)
} // => [1, 2, 3]
}

#TODOs

  • Better conversion from/to internal iterator?

pub struct T[A] {
run : () -> A?
}

impl Show for T[A]

#
T::append

fn[A] T::append(self : T[A], other : A) -> T[A]

Appends an element to the end of the iterator.

Note: The name might be subject to change, related issue: https://github.com/moonbitlang/core/issues/1511

#
T::collect

fn[A] T::collect(self : T[A]) -> Array[A]

Collects the elements of the iterator into an array.

#
T::concat

fn[A] T::concat(self : T[A], other : T[A]) -> T[A]

Concatenates two iterators by appending the elements of the second iterator to the first.

#
T::count

fn[A] T::count(self : T[A]) -> Int

Consumes the iterator, counting and returning the number of iterations.

#
T::drop

fn[A] T::drop(self : T[A], n : Int) -> T[A]

Drops the first n elements from the iterator.

Note: The function is lazy, which only skips elements when it is requested.

#
T::drop_while

fn[A] T::drop_while(self : T[A], f : (A) -> Bool) -> T[A]

Skips elements from the iterator while the predicate returns true.

Note: The function is lazy, which only skips elements when it is requested.

#
T::each

fn[A] T::each(self : T[A], f : (A) -> Unit) -> Unit

Applies f to each element of the iterator.

#
T::eachi

fn[A] T::eachi(self : T[A], f : (Int, A) -> Unit) -> Unit

Applies f to each element of the iterator, passing the index as a parameter.

#
T::filter

fn[A] T::filter(self : T[A], f : (A) -> Bool) -> T[A]

#
T::filter_map

fn[A, B] T::filter_map(self : T[A], f : (A) -> B?) -> T[B]

Transforms the elements of the iterator using f ignoring None.

#
T::find_first

fn[A] T::find_first(self : T[A], pred : (A) -> Bool) -> A?

Finds the first element in the iterator that satisfies the predicate, or None if no such element is found.

#
T::flat_map

fn[A, B] T::flat_map(self : T[A], f : (A) -> T[B]) -> T[B]

Transforms each element of the iterator into an iterator and flattens the resulting iterators into a single iterator.

#
T::flatten

fn[A] T::flatten(self : T[T[A]]) -> T[A]

Flattens an iterator of iterators into a single iterator.

#
T::fold

fn[A, B] T::fold(self : T[A], init~ : B, f : (B, A) -> B) -> B

Folds the elements of the iterator using the given function, starting with the given initial value.

#
T::intersperse

fn[A] T::intersperse(self : T[A], sep : A) -> T[A]

Creates a new iterator which places separator between adjacent items of the original one.

#
T::iter

fn[A] T::iter(self : T[A]) -> Iter[A]

#
T::iter2

fn[A, B] T::iter2(self : T[(A, B)]) -> Iter2[A, B]

#
T::join

fn[A : Show] T::join(self : T[A], separator~ : String) -> String

Concatenates the elements of the iterator into a string, separated by separator.

#
T::last

fn[A] T::last(self : T[A]) -> A?

Consumes the iterator, returning the last value.

#
T::map

fn[A, B] T::map(self : T[A], f : (A) -> B) -> T[B]

Transforms the elements of the iterator using f.

#
T::mapi

fn[A, B] T::mapi(self : T[A], f : (Int, A) -> B) -> T[B]

#
T::maximum

fn[A : Compare + Eq] T::maximum(self : T[A]) -> A?

Returns the minimum element of the iterator, or None if the iterator is empty.

#
T::minimum

fn[A : Compare + Eq] T::minimum(self : T[A]) -> A?

Returns the maximum element of the iterator, or None if the iterator is empty.

#
T::next

fn[A] T::next(self : T[A]) -> A?

Consumes the next value.

#
T::nth

fn[A] T::nth(self : T[A], n : Int) -> A?

Returns the nth element of the iterator, or None if there isn't one.

#
T::op_add

fn[A] T::op_add(self : T[A], other : T[A]) -> T[A]

#
T::peek

fn[A] T::peek(self : T[A]) -> A?

#
T::prepend

fn[A] T::prepend(self : T[A], other : A) -> T[A]

Prepends an element to the beginning of the iterator.

Note: The name might be subject to change, related issue: https://github.com/moonbitlang/core/issues/1511

#
T::span

fn[A] T::span(self : T[A], pred : (A) -> Bool) -> (T[A], T[A])

Splits the iterator into two parts at the first element for which pred returns false.

Returns

Note that requesting the second part of the iterator will consume the first part.

Note: experimental function

#
T::take

fn[A] T::take(self : T[A], n : Int) -> T[A]

Takes the first n elements from the iterator.

#
T::take_while

fn[A] T::take_while(self : T[A], f : (A) -> Bool) -> T[A]

Takes elements from the iterator while the predicate returns true.

#
T::try_collect

fn[A, E : Error] T::try_collect(self : T[Result[A, E]]) -> Array[A] raise E

Note: experimental function, related pr: https://github.com/moonbitlang/core/pull/1260

#
T::zip

fn[A, B] T::zip(self : T[A], other : T[B]) -> T[(A, B)]

Zips two iterators into an iterator of pairs.

#
empty

fn[A] empty() -> T[A]

Creates an empty iterator.

#
from_array

fn[A] from_array(array : Array[A]) -> T[A]

Creates an iterator that generates values from an array.

#
from_iter

fn[A] from_iter(iter : Iter[A]) -> T[A]

Creates an iterator that generates values from an internal iterator.

#
new

fn[A] new(next : () -> A?) -> T[A]

#
range

fn range(start~ : Int, end? : Int, step? : Int, inclusive? : Bool) -> T[Int]

Creates an iterator that generates values from a range.

#
range_by

fn[A : Compare + Eq] range_by(start~ : A, end? : A, step~ : (A) -> A) -> T[A]

Note: experimental function

#
repeat

fn[A] repeat(a : A) -> T[A]

Creates an iterator that repeats a value indefinitely.

#
repeat_by

fn[A] repeat_by(f : () -> A) -> T[A]

Creates an iterator that repeats the result of executing a function indefinitely.

#
seq

fn[A] seq(init~ : A, step~ : (A) -> A, cond~ : (A) -> Bool) -> T[A]

Creates an iterator starts from init and extends by step, until cond returns false.

Note: experimental function

#
singleton

fn[A] singleton(a : A) -> T[A]

Creates an iterator that contains a single element.

#
unfold

fn[A, S] unfold(init~ : S, step~ : (S) -> (A, S)?) -> T[A]

Note: experimental function

#
unfold_eager

fn[A, S] unfold_eager(init~ : S, step~ : (S) -> (A, S)?) -> T[A]

Note: experimental function

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io