Skip to content

Commit

Permalink
Merge branch 'moonbitlang:main' into immutable_vector
Browse files Browse the repository at this point in the history
  • Loading branch information
CAIMEOX authored Apr 26, 2024
2 parents d97eccf + ae44a64 commit 2866ceb
Show file tree
Hide file tree
Showing 68 changed files with 3,669 additions and 369 deletions.
10 changes: 10 additions & 0 deletions _typos.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
[files]
extend-exclude = ["**/*_gen.mbt"]

[default.extend-identifiers]
IDentifier = "IDentifier"

[default]
extend-ignore-re = [
# ignore string literals
"\"(.*?)\"",
".*#|.*",
]
88 changes: 64 additions & 24 deletions array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ test "iter" {
@assertion.assert_eq(i, 5)?
}

/// Iterates over the array with index.
/// Iterates over the array with index.
///
/// # Arguments
///
Expand Down Expand Up @@ -150,7 +150,7 @@ test "iter_rev" {
@assertion.assert_eq(i, 1)?
}

/// Iterates over the array with index in reversed turn.
/// Iterates over the array with index in reversed turn.
///
/// # Arguments
///
Expand All @@ -162,7 +162,7 @@ test "iter_rev" {
/// ```
/// [1, 2, 3, 4, 5].iter_revi(fn(index, elem){
/// print("(\(index),\(elem)) ")
/// }) //output: (4,5) (3,4) (2,3) (1,2) (0,1)
/// }) //output: (4,5) (3,4) (2,3) (1,2) (0,1)
/// ```
pub fn iter_revi[T](self : Array[T], f : (Int, T) -> Unit) -> Unit {
let len = self.length()
Expand Down Expand Up @@ -238,7 +238,7 @@ test "map" {
}

/// Maps a function over the elements of the arr with index.
///
///
/// # Example
/// ```
/// let arr = [3, 4, 5]
Expand Down Expand Up @@ -329,7 +329,7 @@ test "from_array" {
}

/// Fold out values from an array according to certain rules.
///
///
/// # Example
/// ```
/// let sum = [1, 2, 3, 4, 5].fold_left(init=0, fn { sum, elem => sum + elem })
Expand All @@ -353,7 +353,7 @@ test "fold_left" {
}

/// Fold out values from an array according to certain rules in reversed turn.
///
///
/// # Example
/// ```
/// let sum = [1, 2, 3, 4, 5].fold_right(init=0, fn { sum, elem => sum + elem })
Expand All @@ -377,7 +377,7 @@ test "fold_right" {
}

/// Fold out values from an array according to certain rules with index.
///
///
/// # Example
/// ```
/// let sum = [1, 2, 3, 4, 5].fold_lefti(init=0, fn { index, sum, elem => sum + index })
Expand Down Expand Up @@ -406,7 +406,7 @@ test "fold_lefti" {
}

/// Fold out values from an array according to certain rules in reversed turn with index.
///
///
/// # Example
/// ```
/// let sum = [1, 2, 3, 4, 5].fold_righti(init=0, fn { index, sum, elem => sum + index })
Expand Down Expand Up @@ -436,7 +436,7 @@ test "fold_righti" {
}

/// Reverses the order of elements in the slice, in place.
///
///
/// # Example
/// ```
/// let arr = [1, 2, 3, 4, 5]
Expand Down Expand Up @@ -477,9 +477,9 @@ test "reverse" {
}

/// Swap two elements in the array.
///
/// # Example
///
///
/// # Example
///
/// ```
/// let arr = [1, 2, 3, 4, 5]
/// arr.swap(0, 1)
Expand Down Expand Up @@ -512,9 +512,9 @@ test "swap" {
}

/// Check if all the elements in the array match the condition.
///
/// # Example
///
///
/// # Example
///
/// ```
/// let arr = [1, 2, 3, 4, 5]
/// arr.all(fn(ele) { ele < 6 }) // true
Expand Down Expand Up @@ -546,9 +546,9 @@ test "all" {
}

/// Check if any of the elements in the array match the condition.
///
/// # Example
///
///
/// # Example
///
/// ```
/// let arr = [1, 2, 3, 4, 5]
/// arr.any(fn(ele) { ele < 6 }) // true
Expand Down Expand Up @@ -580,7 +580,7 @@ test "any" {
}

/// Fill the array with a given value.
///
///
/// # Example
/// ```
/// [0, 0, 0, 0, 0].fill(3) // [3, 3, 3, 3, 3]
Expand Down Expand Up @@ -608,7 +608,7 @@ test "fill" {
}

/// Search the array index for a given element.
///
///
/// # Example
/// ```
/// let arr = [3, 4, 5]
Expand Down Expand Up @@ -642,7 +642,7 @@ test "search" {
}

/// Checks if the array contains an element.
///
///
/// # Example
/// ```
/// let arr = [3, 4, 5]
Expand Down Expand Up @@ -676,7 +676,7 @@ test "contains" {
}

/// Check if the array starts with a given prefix.
///
///
/// # Example
/// ```
/// let arr = [3, 4, 5]
Expand Down Expand Up @@ -719,7 +719,7 @@ test "starts_with" {
}

/// Check if the array ends with a given suffix.
///
///
/// # Example
/// ```
/// let v = [3, 4, 5]
Expand Down Expand Up @@ -768,7 +768,7 @@ test "ends_with" {
}

/// Convert array to list.
///
///
/// # Example
///
/// ```
Expand Down Expand Up @@ -894,3 +894,43 @@ test "op_add" {
content="[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]",
)?
}

/// @intrinsic %iter.from_array
pub fn as_iter[T](self : Array[T]) -> @iter.Iter[T] {
@iter.Iter::_unstable_internal_make(
fn(yield) {
for i = 0, len = self.length(); i < len; i = i + 1 {
if yield(self[i]).not() {
break false
}
} else {
true
}
},
)
}

test "as_iter" {
let arr = [1, 2, 3, 4, 5]
let iter = arr.as_iter()
let exb = Buffer::make(0)
let mut i = 0
iter.iter(
fn(x) {
exb.write_string(x.to_string())
exb.write_char('\n')
i = i + 1
},
)
@assertion.assert_eq(i, arr.length())?
exb.expect(
content=
#|1
#|2
#|3
#|4
#|5
#|
,
)?
}
21 changes: 12 additions & 9 deletions array/array.mbti
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package moonbitlang/core/array

alias @moonbitlang/core/iter as @iter

// Values
fn is_sorted[T : @moonbitlang/core/builtin.Compare + @moonbitlang/core/builtin.Eq](Array[T]) -> Bool
fn is_sorted[T : Compare + Eq](Array[T]) -> Bool

fn new[T](Int, () -> T) -> Array[T]

Expand All @@ -11,8 +13,9 @@ fn new_with_index[T](Int, (Int) -> T) -> Array[T]
type TimSortRun
fn Array::all[T](Array[T], (T) -> Bool) -> Bool
fn Array::any[T](Array[T], (T) -> Bool) -> Bool
fn Array::contains[T : @moonbitlang/core/builtin.Eq](Array[T], T) -> Bool
fn Array::ends_with[T : @moonbitlang/core/builtin.Eq](Array[T], Array[T]) -> Bool
fn Array::as_iter[T](Array[T]) -> @iter.Iter[T]
fn Array::contains[T : Eq](Array[T], T) -> Bool
fn Array::ends_with[T : Eq](Array[T], Array[T]) -> Bool
fn Array::fill[T](Array[T], T) -> Unit
fn Array::fold_left[T, U](Array[T], (U, T) -> U, U) -> U
fn Array::fold_lefti[T, U](Array[T], (Int, U, T) -> U, U) -> U
Expand All @@ -26,14 +29,14 @@ fn Array::iteri[T](Array[T], (Int, T) -> Unit) -> Unit
fn Array::map[T, U](Array[T], (T) -> U) -> Array[U]
fn Array::mapi[T, U](Array[T], (Int, T) -> U) -> Array[U]
fn Array::op_add[T](Array[T], Array[T]) -> Array[T]
fn Array::op_equal[T : @moonbitlang/core/builtin.Eq](Array[T], Array[T]) -> Bool
fn Array::op_equal[T : Eq](Array[T], Array[T]) -> Bool
fn Array::reverse[T](Array[T]) -> Unit
fn Array::search[T : @moonbitlang/core/builtin.Eq](Array[T], T) -> Option[Int]
fn Array::sort[T : @moonbitlang/core/builtin.Compare + @moonbitlang/core/builtin.Eq](Array[T]) -> Unit
fn Array::search[T : Eq](Array[T], T) -> Option[Int]
fn Array::sort[T : Compare + Eq](Array[T]) -> Unit
fn Array::sort_by[T](Array[T], (T, T) -> Int) -> Unit
fn Array::sort_by_key[T, K : @moonbitlang/core/builtin.Compare + @moonbitlang/core/builtin.Eq](Array[T], (T) -> K) -> Unit
fn Array::stable_sort[T : @moonbitlang/core/builtin.Compare + @moonbitlang/core/builtin.Eq](Array[T]) -> Unit
fn Array::starts_with[T : @moonbitlang/core/builtin.Eq](Array[T], Array[T]) -> Bool
fn Array::sort_by_key[T, K : Compare + Eq](Array[T], (T) -> K) -> Unit
fn Array::stable_sort[T : Compare + Eq](Array[T]) -> Unit
fn Array::starts_with[T : Eq](Array[T], Array[T]) -> Bool
fn Array::swap[T](Array[T], Int, Int) -> Unit
fn Array::to_list[T](Array[T]) -> List[T]

Expand Down
3 changes: 2 additions & 1 deletion array/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"moonbitlang/core/assertion",
"moonbitlang/core/math",
"moonbitlang/core/coverage",
"moonbitlang/core/vec"
"moonbitlang/core/vec",
"moonbitlang/core/iter"
]
}
14 changes: 8 additions & 6 deletions assertion/assertion.mbti
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
package moonbitlang/core/assertion

alias @moonbitlang/core/bool as @bool

// Values
fn assert_eq[T : @moonbitlang/core/builtin.Debug + @moonbitlang/core/builtin.Eq](T, T, @moonbitlang/core/builtin.SourceLoc) -> Result[Unit, String]
fn assert_eq[T : Debug + Eq](T, T, SourceLoc) -> Result[Unit, String]

fn assert_false[T : @moonbitlang/core/builtin.Debug + @moonbitlang/core/bool.Boolean](T, @moonbitlang/core/builtin.SourceLoc) -> Result[Unit, String]
fn assert_false[T : Debug + @bool.Boolean](T, SourceLoc) -> Result[Unit, String]

fn assert_is[T : @moonbitlang/core/builtin.Debug](T, T, @moonbitlang/core/builtin.SourceLoc) -> Result[Unit, String]
fn assert_is[T : Debug](T, T, SourceLoc) -> Result[Unit, String]

fn assert_is_not[T : @moonbitlang/core/builtin.Debug](T, T, @moonbitlang/core/builtin.SourceLoc) -> Result[Unit, String]
fn assert_is_not[T : Debug](T, T, SourceLoc) -> Result[Unit, String]

fn assert_ne[T : @moonbitlang/core/builtin.Debug + @moonbitlang/core/builtin.Eq](T, T, @moonbitlang/core/builtin.SourceLoc) -> Result[Unit, String]
fn assert_ne[T : Debug + Eq](T, T, SourceLoc) -> Result[Unit, String]

fn assert_true[T : @moonbitlang/core/builtin.Debug + @moonbitlang/core/bool.Boolean](T, @moonbitlang/core/builtin.SourceLoc) -> Result[Unit, String]
fn assert_true[T : Debug + @bool.Boolean](T, SourceLoc) -> Result[Unit, String]

// Types and methods

Expand Down
8 changes: 4 additions & 4 deletions char/char.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub fn hash(self : Char) -> Int {
}

/// Check if the char is a surrogate unit
pub fn is_surrogate(self: Char) -> Bool {
pub fn is_surrogate(self : Char) -> Bool {
let code = self.to_int()
0xD800 <= code && code <= 0xDFFF
}
Expand All @@ -94,7 +94,7 @@ test "is_surrogate" {
}

/// Get high surrogate of the char
pub fn get_high_surrogate(self: Char) -> Option[Char] {
pub fn get_high_surrogate(self : Char) -> Option[Char] {
let code = self.to_int()
if code < 0x10000 {
None
Expand All @@ -105,7 +105,7 @@ pub fn get_high_surrogate(self: Char) -> Option[Char] {
}

/// Get low surrogate of the char
pub fn get_low_surrogate(self: Char) -> Option[Char] {
pub fn get_low_surrogate(self : Char) -> Option[Char] {
let code = self.to_int()
if code < 0x10000 {
None
Expand All @@ -120,4 +120,4 @@ test "get high/low surrogate" {
@assertion.assert_eq('a'.get_low_surrogate(), None)?
@assertion.assert_eq('🤣'.get_high_surrogate(), Some("🤣"[0]))?
@assertion.assert_eq('🤣'.get_low_surrogate(), Some("🤣"[1]))?
}
}
2 changes: 1 addition & 1 deletion char/char.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package moonbitlang/core/char
// Values

// Types and methods
fn Char::debug_write(Char, @moonbitlang/core/builtin.Buffer) -> Unit
fn Char::debug_write(Char, Buffer) -> Unit
fn Char::get_high_surrogate(Char) -> Option[Char]
fn Char::get_low_surrogate(Char) -> Option[Char]
fn Char::hash(Char) -> Int
Expand Down
6 changes: 3 additions & 3 deletions deque/deque.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ test "realloc" {
@assertion.assert_eq(result, [4, 5, 6, 7, 8, 9, 10])?
}

/// Return the fornt element from a deque, or `None` if it is empty.
/// Return the front element from a deque, or `None` if it is empty.
///
/// # Example
/// ```
Expand Down Expand Up @@ -189,7 +189,7 @@ pub fn push_back[T](self : Deque[T], value : T) -> Unit {
self.len += 1
}

/// Removes a fornt element from a deque.
/// Removes a front element from a deque.
///
/// # Example
/// ```
Expand Down Expand Up @@ -221,7 +221,7 @@ pub fn pop_back_exn[T](self : Deque[T]) -> Unit {
self.len -= 1
}

/// Removes a fornt element from a deque and returns it, or `None` if it is empty.
/// Removes a front element from a deque and returns it, or `None` if it is empty.
///
/// # Example
/// ```
Expand Down
Loading

0 comments on commit 2866ceb

Please sign in to comment.