diff --git a/builtin/iter2.mbt b/builtin/iter2.mbt index c7547229c..8b89b6004 100644 --- a/builtin/iter2.mbt +++ b/builtin/iter2.mbt @@ -88,3 +88,35 @@ pub fn Iter2::to_array[A, B](self : Iter2[A, B]) -> Array[(A, B)] { } arr } + +///| +/// Combines two iterators into one by appending the elements of the second iterator to the first. +/// +/// # Arguments +/// +/// * `self` - The first input iterator. +/// * `other` - The second input iterator to be appended to the first. +/// +/// # Returns +/// +/// Returns a new iterator that contains the elements of `self` followed by the elements of `other`. +/// +/// # Examples +/// +/// ```moonbit +/// test "Iter2::concat" { +/// inspect!( +/// "abcde".iter2().concat("fghij".iter2()).to_array(), +/// content="[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e'), (0, 'f'), (1, 'g'), (2, 'h'), (3, 'i'), (4, 'j')]", +/// ) +/// } +/// ``` +pub fn Iter2::concat[A, B]( + self : Iter2[A, B], + other : Iter2[A, B] +) -> Iter2[A, B] { + fn(yield_) { + guard self.run(yield_) == IterContinue else { IterEnd } + other.run(yield_) + } +}