From 555630c28e054c54d76c70d22c4ba1bda7119710 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 | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/builtin/iter2.mbt b/builtin/iter2.mbt index c7547229cf..8b89b60047 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_) + } +}