From 7054447e391e6a04e0f1265d5fe6904c4ac790dc Mon Sep 17 00:00:00 2001 From: rami3l Date: Fri, 7 Feb 2025 13:11:58 +0800 Subject: [PATCH] feat(builtin): add `Iter2::concat()` --- builtin/iter2.mbt | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/builtin/iter2.mbt b/builtin/iter2.mbt index c7547229c..dec955c17 100644 --- a/builtin/iter2.mbt +++ b/builtin/iter2.mbt @@ -88,3 +88,37 @@ 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" { +/// let ns = [10, 11, 12, 13, 14] +/// let ms = [15, 16, 17, 18, 19] +/// inspect!( +/// ns.iter2().concat(ms.iter2()).to_array(), +/// content="[(0, 10), (1, 11), (2, 12), (3, 13), (4, 14), (0, 15), (1, 16), (2, 17), (3, 18), (4, 19)]", +/// ) +/// } +/// ``` +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_) + } +}