Skip to content

Commit

Permalink
add FixedArray::is_empty
Browse files Browse the repository at this point in the history
  • Loading branch information
illusory0x0 authored and bobzhang committed Jan 15, 2025
1 parent 8cf2d06 commit eacae40
Show file tree
Hide file tree
Showing 3 changed files with 29 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 @@ -695,6 +695,7 @@ impl FixedArray {
default[X]() -> Self[X]
fill[T](Self[T], T) -> Unit
get[T](Self[T], Int) -> T
is_empty[T](Self[T]) -> Bool
iter[T](Self[T]) -> Iter[T]
iter2[T](Self[T]) -> Iter2[Int, T]
length[T](Self[T]) -> Int
Expand Down
23 changes: 23 additions & 0 deletions builtin/fixedarray.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,26 @@ pub fn compare[T : Compare](self : FixedArray[T], other : FixedArray[T]) -> Int
}
}
}

///|
/// Tests whether the FixedArray contains no elements.
///
/// Parameters:
///
/// * `FixedArray` : The FixedArray to check.
///
/// Returns `true` if the FixedArray has no elements, `false` otherwise.
///
/// Example:
///
/// ```moonbit
/// test "FixedArray::is_empty" {
/// let empty : FixedArray[Int] = []
/// inspect!(empty.is_empty(), content="true")
/// let non_empty = [1, 2, 3]
/// inspect!(non_empty.is_empty(), content="false")
/// }
/// ```
pub fn FixedArray::is_empty[T](self : FixedArray[T]) -> Bool {
self.length() == 0
}
5 changes: 5 additions & 0 deletions builtin/fixedarray_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@ test "compare" {
inspect!(arr3.compare(arr1), content="-1")
inspect!(arr1.compare(arr1), content="0")
}

test "is_empty" {
let arr : FixedArray[Int] = []
assert_true!(arr.is_empty())
}

0 comments on commit eacae40

Please sign in to comment.