Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions array/mutview.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
pub fn[X] MutArrayView::iterator(self : MutArrayView[X]) -> Iterator[X] {
let mut i = 0
Iterator::new(fn() {
guard i < self.length() else { None }
let elem = self.unsafe_get(i)
i += 1
Some(elem)
})
}

///|
pub fn[X] MutArrayView::rev_iterator(self : MutArrayView[X]) -> Iterator[X] {
let mut i = self.length()
Iterator::new(fn() {
guard i > 0 else { None }
i -= 1
Some(self.unsafe_get(i))
})
}

///|
pub fn[X] MutArrayView::iterator2(self : MutArrayView[X]) -> Iterator2[Int, X] {
let mut i = 0
Iterator2::new(fn() {
guard i < self.length() else { None }
let result = Some((i, self.unsafe_get(i)))
i += 1
result
})
}

///|
pub impl[X : Show] Show for MutArrayView[X] with output(self, logger) {
self[:].output(logger)
}

///|
pub impl[T : Eq] Eq for MutArrayView[T] with equal(self, other) -> Bool {
self[:] == other[:]
}

///|
pub impl[T : Compare] Compare for MutArrayView[T] with compare(self, other) -> Int {
self[:].compare(other[:])
}

///|
pub impl[A : Hash] Hash for MutArrayView[A] with hash_combine(self, hasher) {
self[:].hash_combine(hasher)
}
34 changes: 34 additions & 0 deletions array/mutview_test.mbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright 2025 International Digital Economy Academy
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

///|
test "iterator" {
let arr = [1, 2, 3, 4, 5]
let iter = arr.iterator().map(x => x * 2)
inspect(iter.to_array(), content="[2, 4, 6, 8, 10]")
}

///|
test "rev_iterator" {
let arr = [1, 2, 3, 4, 5]
let iter = arr.rev_iterator().map(x => x * 2)
inspect(iter.to_array(), content="[10, 8, 6, 4, 2]")
}

///|
test "iterator2" {
let arr = [1, 2, 3, 4, 5]
let iter = arr.iterator2()
inspect(iter.to_string(), content="[(0, 1), (1, 2), (2, 3), (3, 4), (4, 5)]")
}
8 changes: 8 additions & 0 deletions array/pkg.generated.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,14 @@ impl[A : Hash] Hash for ArrayView[A]
impl[X : Show] Show for ArrayView[X]
impl[A : @quickcheck.Arbitrary] @quickcheck.Arbitrary for ArrayView[A]

fn[X] MutArrayView::iterator(Self[X]) -> Iterator[X]
fn[X] MutArrayView::iterator2(Self[X]) -> Iterator2[Int, X]
fn[X] MutArrayView::rev_iterator(Self[X]) -> Iterator[X]
impl[T : Compare] Compare for MutArrayView[T]
impl[T : Eq] Eq for MutArrayView[T]
impl[A : Hash] Hash for MutArrayView[A]
impl[X : Show] Show for MutArrayView[X]

// Type aliases
pub using @builtin {type ArrayView as View}

Expand Down
Loading
Loading