diff --git a/array/array.mbt b/array/array.mbt index 958d255a6..a5e4674c1 100644 --- a/array/array.mbt +++ b/array/array.mbt @@ -54,41 +54,6 @@ pub fn push_iter[T](self : Array[T], iter : Iter[T]) -> Unit { } } -///| -/// Creates a new array of the specified length, where each element is -/// initialized using an index-based initialization function. -/// -/// Parameters: -/// -/// * `length` : The length of the new array. If `length` is less than or equal -/// to 0, returns an empty array. -/// * `initializer` : A function that takes an index (starting from 0) and -/// returns a value of type `T`. This function is called for each index to -/// initialize the corresponding element. -/// -/// Returns a new array of type `Array[T]` with the specified length, where each -/// element is initialized using the provided function. -/// -/// Example: -/// -/// ```moonbit -/// test "Array::makei" { -/// let arr = Array::makei(3, fn(i) { i * 2 }) -/// inspect!(arr, content="[0, 2, 4]") -/// } -/// ``` -pub fn Array::makei[T](length : Int, value : (Int) -> T) -> Array[T] { - if length <= 0 { - [] - } else { - let array = Array::make(length, value(0)) - for i in 1.. Self[T] join(Self[String], String) -> String last[A](Self[A]) -> A? - makei[T](Int, (Int) -> T) -> Self[T] map_option[A, B](Self[A], (A) -> B?) -> Self[B] //deprecated push_iter[T](Self[T], Iter[T]) -> Unit shuffle[T](Self[T], rand~ : (Int) -> Int) -> Self[T] diff --git a/array/array_test.mbt b/array/array_test.mbt index bfca078e5..f4cb55544 100644 --- a/array/array_test.mbt +++ b/array/array_test.mbt @@ -567,7 +567,7 @@ test "Array::makei with zero length" { } ///| -test "Array::makei with negative length" { +test "panic/Array::makei with negative length" { let arr = Array::makei(-1, fn(i) { i * 2 }) assert_eq!(arr, []) } diff --git a/builtin/array.mbt b/builtin/array.mbt index 26a4543c5..1ece94f61 100644 --- a/builtin/array.mbt +++ b/builtin/array.mbt @@ -74,6 +74,37 @@ pub fn Array::make[T](len : Int, elem : T) -> Array[T] { arr } +///| +/// Creates a new array of the specified length, where each element is +/// initialized using an index-based initialization function. +/// +/// Parameters: +/// +/// * `length` : The length of the new array. If `length` is less than or equal +/// to 0, returns an empty array. +/// * `initializer` : A function that takes an index (starting from 0) and +/// returns a value of type `T`. This function is called for each index to +/// initialize the corresponding element. +/// +/// Returns a new array of type `Array[T]` with the specified length, where each +/// element is initialized using the provided function. +/// +/// Example: +/// +/// ```moonbit +/// test "Array::makei" { +/// let arr = Array::makei(3, fn(i) { i * 2 }) +/// inspect!(arr, content="[0, 2, 4]") +/// } +/// ``` +pub fn Array::makei[T](len : Int, value : (Int) -> T) -> Array[T] { + let arr = Array::make_uninit(len) + for i in 0.. Iter2[Int, A] length[T](Self[T]) -> Int make[T](Int, T) -> Self[T] + makei[T](Int, (Int) -> T) -> Self[T] map[T, U](Self[T], (T) -> U) -> Self[U] map_inplace[T](Self[T], (T) -> T) -> Unit mapi[T, U](Self[T], (Int, T) -> U) -> Self[U]