Skip to content

Commit

Permalink
feat(builtin): add Iter2::concat()
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed Feb 7, 2025
1 parent e3bf1b0 commit 2562dd2
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 0 deletions.
1 change: 1 addition & 0 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ impl[T : Show] Show for Iter[T]

type Iter2
impl Iter2 {
concat[A, B](Self[A, B], Self[A, B]) -> Self[A, B]
each[A, B](Self[A, B], (A, B) -> Unit) -> Unit
iter[A, B](Self[A, B]) -> Iter[(A, B)]
iter2[A, B](Self[A, B]) -> Self[A, B]
Expand Down
32 changes: 32 additions & 0 deletions builtin/iter2.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
/// "abc".iter2().concat("def".iter2()).to_array(),
/// content="[(0, 'a'), (1, 'b'), (2, 'c'), (0, 'd'), (1, 'e'), (2, 'f')]",
/// )
/// }
/// ```
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_)
}
}
24 changes: 24 additions & 0 deletions builtin/iter2_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

///|
test "iter2_to_iter" {
let numbers = [1, 2, 3]
let iter2_numbers = numbers.iter2() // creates Iter2[Int, Int] from Array
Expand All @@ -20,6 +21,7 @@ test "iter2_to_iter" {
inspect!(result, content="[(0, 1), (1, 2), (2, 3)]")
}

///|
test "iter2 identity function should return its input" {
let m : @builtin.Map[String, Int] = @builtin.Map::new()
m.set("x", 1)
Expand All @@ -29,3 +31,25 @@ test "iter2 identity function should return its input" {
let result = same_iter2.to_array()
inspect!(result, content="[(\"x\", 1), (\"y\", 2)]")
}

///|
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')]",
)
}

///|
test "Iter2::concat with early return" {
let first = Iter2::new(fn(y) {
for i, v in "abcde" {
ignore(y(i, v))
}
IterEnd
})
inspect!(
first.concat("fghij".iter2()).to_array(),
content="[(0, 'a'), (1, 'b'), (2, 'c'), (3, 'd'), (4, 'e')]",
)
}

0 comments on commit 2562dd2

Please sign in to comment.