Skip to content

Commit

Permalink
Merge branch 'moonbitlang:main' into immutable-vec
Browse files Browse the repository at this point in the history
  • Loading branch information
jialunzhang-psu authored Dec 26, 2024
2 parents 453569c + 154ea8c commit e037236
Show file tree
Hide file tree
Showing 40 changed files with 413 additions and 51 deletions.
17 changes: 16 additions & 1 deletion array/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub fn Array::shuffle[T](arr : Array[T], rand~ : (Int) -> Int) -> Array[T] {
///
/// # Returns
///
pub fn map_option[A, B](self : Array[A], f : (A) -> B?) -> Array[B] {
pub fn filter_map[A, B](self : Array[A], f : (A) -> B?) -> Array[B] {
let result = []
self.each(fn {
x =>
Expand All @@ -123,6 +123,21 @@ pub fn map_option[A, B](self : Array[A], f : (A) -> B?) -> Array[B] {
result
}

///|
/// Returns a new array containing the elements of the original array that satisfy the given predicate.
///
/// # Arguments
///
/// * `self` - The array to filter.
/// * `f` - The predicate function.
///
/// # Returns
///
/// @alert deprecated "Use `Array::filter_map` instead"
pub fn map_option[A, B](self : Array[A], f : (A) -> B?) -> Array[B] {
self.filter_map(f)
}

///|
/// Returns the last element of the array.
pub fn last[A](self : Array[A]) -> A? {
Expand Down
3 changes: 2 additions & 1 deletion array/array.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ impl FixedArray {

impl Array {
copy[T](Self[T]) -> Self[T]
filter_map[A, B](Self[A], (A) -> B?) -> Self[B]
from_iter[T](Iter[T]) -> Self[T]
last[A](Self[A]) -> A?
makei[T](Int, (Int) -> T) -> Self[T]
map_option[A, B](Self[A], (A) -> B?) -> Self[B]
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]
shuffle_in_place[T](Self[T], rand~ : (Int) -> Int) -> Unit
Expand Down
4 changes: 2 additions & 2 deletions array/array_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -513,9 +513,9 @@ test "Array::makei with negative length" {
assert_eq!(arr, [])
}

test "map_option" {
test "filter_map" {
let arr = [1, 2, 3, 4, 5]
let mapped = arr.map_option(fn(x) { if x % 2 == 0 { Some(x) } else { None } })
let mapped = arr.filter_map(fn(x) { if x % 2 == 0 { Some(x) } else { None } })
inspect!(mapped, content="[2, 4]")
}

Expand Down
6 changes: 6 additions & 0 deletions array/deprecated.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
///|
/// Create a new array. Values are lazily built.
/// @alert deprecated "Use `FixedArray::makei` instead"
/// @coverage.skip
pub fn FixedArray::new[T](length : Int, value : () -> T) -> FixedArray[T] {
if length <= 0 {
[]
Expand All @@ -30,6 +31,7 @@ pub fn FixedArray::new[T](length : Int, value : () -> T) -> FixedArray[T] {
///|
/// Create a new array. Values are built from indexes.
/// @alert deprecated "Use `FixedArray::makei` instead"
/// @coverage.skip
pub fn FixedArray::new_with_index[T](
length : Int,
value : (Int) -> T
Expand All @@ -46,6 +48,7 @@ pub fn FixedArray::new_with_index[T](
/// assert_eq!(sum, 15)
/// ```
/// @alert deprecated "Use `fold` instead"
/// @coverage.skip
pub fn fold_left[T, U](self : FixedArray[T], f : (U, T) -> U, init~ : U) -> U {
self.fold(init~, f)
}
Expand All @@ -59,6 +62,7 @@ pub fn fold_left[T, U](self : FixedArray[T], f : (U, T) -> U, init~ : U) -> U {
/// assert_eq!(sum, 15)
/// ```
/// @alert deprecated "Use `rev_fold` instead"
/// @coverage.skip
pub fn fold_right[T, U](self : FixedArray[T], f : (U, T) -> U, init~ : U) -> U {
self.rev_fold(init~, f)
}
Expand All @@ -72,6 +76,7 @@ pub fn fold_right[T, U](self : FixedArray[T], f : (U, T) -> U, init~ : U) -> U {
/// assert_eq!(sum, 10)
/// ```
/// @alert deprecated "Use `foldi` instead"
/// @coverage.skip
pub fn fold_lefti[T, U](
self : FixedArray[T],
f : (Int, U, T) -> U,
Expand All @@ -89,6 +94,7 @@ pub fn fold_lefti[T, U](
/// assert_eq!(sum, 10)
/// ```
/// @alert deprecated "Use `rev_foldi` instead"
/// @coverage.skip
pub fn fold_righti[T, U](
self : FixedArray[T],
f : (Int, U, T) -> U,
Expand Down
3 changes: 3 additions & 0 deletions buffer/deprecated.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
/// Return a new string contains the data in buffer.
///
/// @alert deprecated "Use `Buffer::contents` to read the contents of the buffer"
/// @coverage.skip
pub fn to_string(self : T) -> String {
self.contents().to_unchecked_string(offset=0, length=self.len)
}
Expand All @@ -26,13 +27,15 @@ pub fn to_string(self : T) -> String {
/// it simply copy the bytes into a new String.
///
/// @alert deprecated "Use `Buffer::contents` to read the contents of the buffer"
/// @coverage.skip
pub fn to_unchecked_string(self : T) -> String {
self.contents().to_unchecked_string(offset=0, length=self.len)
}

///|
/// Write a sub-string into buffer.
/// @alert deprecated "Use `Buffer::write_substring` instead"
/// @coverage.skip
pub fn write_sub_string(
self : T,
value : String,
Expand Down
5 changes: 5 additions & 0 deletions builtin/array.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,7 @@ pub fn search[T : Eq](self : Array[T], value : T) -> Int? {

///|
/// @alert deprecated "Use `search_by` instead."
/// @coverage.skip
pub fn find_index[T](self : Array[T], f : (T) -> Bool) -> Int? {
search_by(self, f)
}
Expand Down Expand Up @@ -868,6 +869,7 @@ pub fn rev_foldi[A, B](self : Array[A], init~ : B, f : (Int, B, A) -> B) -> B {
/// assert_eq!(sum, 15)
/// ```
/// @alert deprecated "Use `fold` instead."
/// @coverage.skip
pub fn fold_left[T, U](self : Array[T], f : (U, T) -> U, init~ : U) -> U {
self.fold(init~, f)
}
Expand All @@ -881,6 +883,7 @@ pub fn fold_left[T, U](self : Array[T], f : (U, T) -> U, init~ : U) -> U {
/// assert_eq!(sum, 15)
/// ```
/// @alert deprecated "Use `rev_fold` instead."
/// @coverage.skip
pub fn fold_right[T, U](self : Array[T], f : (U, T) -> U, init~ : U) -> U {
self.rev_fold(init~, f)
}
Expand All @@ -894,6 +897,7 @@ pub fn fold_right[T, U](self : Array[T], f : (U, T) -> U, init~ : U) -> U {
/// assert_eq!(sum, 10)
/// ```
/// @alert deprecated "Use `foldi` instead."
/// @coverage.skip
pub fn fold_lefti[T, U](self : Array[T], f : (Int, U, T) -> U, init~ : U) -> U {
self.foldi(init~, f)
}
Expand All @@ -907,6 +911,7 @@ pub fn fold_lefti[T, U](self : Array[T], f : (Int, U, T) -> U, init~ : U) -> U {
/// assert_eq!(sum, 10)
/// ```
/// @alert deprecated "Use `rev_foldi` instead."
/// @coverage.skip
pub fn fold_righti[T, U](self : Array[T], f : (Int, U, T) -> U, init~ : U) -> U {
self.rev_foldi(init~, f)
}
Expand Down
1 change: 1 addition & 0 deletions builtin/arraycore_js.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ pub fn pop[T](self : Array[T]) -> T? {

///|
/// @alert deprecated "Use `unsafe_pop` instead"
/// @coverage.skip
pub fn pop_exn[T](self : Array[T]) -> T {
self.unsafe_pop()
}
Expand Down
1 change: 1 addition & 0 deletions builtin/arraycore_nonjs.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ pub fn pop[T](self : Array[T]) -> T? {

///|
/// @alert deprecated "Use `unsafe_pop` instead"
/// @coverage.skip
pub fn pop_exn[T](self : Array[T]) -> T {
self.unsafe_pop()
}
Expand Down
4 changes: 4 additions & 0 deletions builtin/bigint_deprecated.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

///|
/// @alert deprecated "Use infix bitwise operator `>>` instead"
/// @coverage.skip
pub fn asr(self : BigInt, n : Int) -> BigInt {
self >> n
}
Expand All @@ -24,6 +25,7 @@ pub fn asr(self : BigInt, n : Int) -> BigInt {
/// Only the absolute value is shifted.
///
/// @alert deprecated "Use infix bitwise operator `<<` instead"
/// @coverage.skip
pub fn shl(self : BigInt, n : Int) -> BigInt {
self << n
}
Expand All @@ -34,6 +36,7 @@ pub fn shl(self : BigInt, n : Int) -> BigInt {
/// Only the absolute value is shifted.
///
/// @alert deprecated "Use infix bitwise operator `<<` instead"
/// @coverage.skip
pub fn lsl(self : BigInt, n : Int) -> BigInt {
self << n
}
Expand All @@ -44,6 +47,7 @@ pub fn lsl(self : BigInt, n : Int) -> BigInt {
/// Only the absolute value is shifted.
///
/// @alert deprecated "Use infix bitwise operator `>>` instead"
/// @coverage.skip
pub fn shr(self : BigInt, n : Int) -> BigInt {
self >> n
}
10 changes: 7 additions & 3 deletions builtin/builtin.mbti
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ fn assert_not_eq[T : Eq + Show](T, T, loc~ : SourceLoc = _) -> Unit!

fn assert_true(Bool, loc~ : SourceLoc = _) -> Unit!

fn dump[T](T, name? : String, loc~ : SourceLoc = _) -> T //deprecated

fn fail[T](String, loc~ : SourceLoc = _) -> T!Failure

fn ignore[T](T) -> Unit
Expand Down Expand Up @@ -179,6 +181,7 @@ impl BigInt {
impl Show for BigInt

pub(all) type! Failure String
impl Show for Failure

type Hasher
impl Hasher {
Expand Down Expand Up @@ -215,6 +218,7 @@ impl Iter {
eachi[T](Self[T], (Int, T) -> Unit) -> Unit
empty[T]() -> Self[T]
filter[T](Self[T], (T) -> Bool) -> Self[T]
filter_map[A, B](Self[A], (A) -> B?) -> Self[B]
find_first[T](Self[T], (T) -> Bool) -> T?
flat_map[T, R](Self[T], (T) -> Self[R]) -> Self[R]
fold[T, B](Self[T], init~ : B, (B, T) -> B) -> B
Expand All @@ -224,7 +228,7 @@ impl Iter {
just_run[T](Self[T], (T) -> IterResult) -> Unit
last[A](Self[A]) -> A?
map[T, R](Self[T], (T) -> R) -> Self[R]
map_option[A, B](Self[A], (A) -> B?) -> Self[B]
map_option[A, B](Self[A], (A) -> B?) -> Self[B] //deprecated
map_while[A, B](Self[A], (A) -> B?) -> Self[B]
new[T](((T) -> IterResult) -> IterResult) -> Self[T]
op_add[T](Self[T], Self[T]) -> Self[T]
Expand All @@ -236,7 +240,7 @@ impl Iter {
singleton[T](T) -> Self[T]
take[T](Self[T], Int) -> Self[T]
take_while[T](Self[T], (T) -> Bool) -> Self[T]
tap[T](Self[T], (T) -> Unit) -> Self[T] //deprecated
tap[T](Self[T], (T) -> Unit) -> Self[T]
to_array[T](Self[T]) -> Array[T]
}
impl[T : Show] Show for Iter[T]
Expand Down Expand Up @@ -621,7 +625,7 @@ impl String {
op_add(String, String) -> String
op_equal(String, String) -> Bool
op_get(String, Int) -> Char
substring(String, start~ : Int = .., end~ : Int = ..) -> String
substring(String, start~ : Int = .., end? : Int) -> String
to_json(String) -> Json
to_string(String) -> String
}
Expand Down
2 changes: 2 additions & 0 deletions builtin/byte.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ pub fn Byte::op_shr(self : Byte, count : Int) -> Byte {
/// Returns the resulting `Byte` value after the bitwise left shift operation.
///
/// @alert deprecated "Use infix operator `<<` instead"
/// @coverage.skip
pub fn Byte::lsl(self : Byte, count : Int) -> Byte {
(self.to_int() << count).to_byte()
}
Expand All @@ -259,6 +260,7 @@ pub fn Byte::lsl(self : Byte, count : Int) -> Byte {
/// Returns the result of the logical shift right operation as a `Byte`.
///
/// @alert deprecated "Use infix operator `>>` instead"
/// @coverage.skip
pub fn Byte::lsr(self : Byte, count : Int) -> Byte {
(self.to_uint() >> count).reinterpret_as_int().to_byte()
}
6 changes: 6 additions & 0 deletions builtin/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ fn unsafe_sub_string(
/// starts at `byte_offset` and has length `byte_length`.
///
/// @alert deprecated "Use `to_unchecked_string` instead"
/// @coverage.skip
pub fn sub_string(self : Bytes, byte_offset : Int, byte_length : Int) -> String {
self.to_unchecked_string(offset=byte_offset, length=byte_length)
}
Expand All @@ -45,6 +46,7 @@ pub fn sub_string(self : Bytes, byte_offset : Int, byte_length : Int) -> String
/// Create a new unchecked string from byte sequence.
///
/// @alert deprecated "Use `to_unchecked_string` instead"
/// @coverage.skip
pub fn to_string(self : Bytes) -> String {
self.to_unchecked_string(offset=0, length=self.length())
}
Expand All @@ -70,6 +72,7 @@ pub fn to_unchecked_string(
/// Copy `length` chars from string `str`, starting at `str_offset`,
/// into byte sequence `self`, starting at `bytes_offset`.
/// @alert deprecated "The type Bytes is about to be changed to be immutable. Use `FixedArray[Byte]` or `Buffer` instead."
/// @coverage.skip
pub fn blit_from_string(
self : Bytes,
bytes_offset : Int,
Expand Down Expand Up @@ -150,6 +153,7 @@ pub fn copy(self : Bytes) -> Bytes {
/// Fill UTF8 encoded char `value` into byte sequence `self`, starting at `offset`.
/// It return the length of bytes has been written.
/// @alert deprecated "The type Bytes is about to be changed to be immutable. Use `FixedArray[Byte]` or `Buffer` instead."
/// @coverage.skip
pub fn set_utf8_char(self : Bytes, offset : Int, value : Char) -> Int {
let code = value.to_uint()
match code {
Expand Down Expand Up @@ -220,6 +224,7 @@ pub fn set_utf8_char(
/// It return the length of bytes has been written.
/// @alert unsafe "Panic if the [value] is out of range"
/// @alert deprecated "The type Bytes is about to be changed to be immutable. Use `FixedArray[Byte]` or `Buffer` instead."
/// @coverage.skip
pub fn set_utf16_char(self : Bytes, offset : Int, value : Char) -> Int {
let code = value.to_uint()
if code < 0x10000 {
Expand All @@ -245,6 +250,7 @@ pub fn set_utf16_char(self : Bytes, offset : Int, value : Char) -> Int {
/// It return the length of bytes has been written.
/// @alert unsafe "Panic if the [value] is out of range"
/// @alert deprecated "Use `set_utf16le_char` instead"
/// @coverage.skip
pub fn set_utf16_char(
self : FixedArray[Byte],
offset : Int,
Expand Down
16 changes: 16 additions & 0 deletions builtin/console.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,33 @@
///|
fn println_mono(s : String) -> Unit = "%println"

///|
fn any_to_string[T](any : T) -> String = "%any.to_string"

///|
pub fn println[T : Show](input : T) -> Unit {
println_mono(input.to_string())
}

///|
/// @alert deprecated "Use `println` instead"
/// @coverage.skip
pub fn print[T : Show](input : T) -> Unit {
println(input)
}

///|
/// Prints and returns the value of a given expression for quick and dirty debugging.
/// @alert deprecated "This function is for debugging only and should not be used in production"
pub fn dump[T](t : T, name? : String, loc~ : SourceLoc = _) -> T {
let name = match name {
Some(name) => name
None => ""
}
println("dump(\{name}@\{loc}) = \{any_to_string(t)}")
t
}

///|
pub fn to_string(self : Bool) -> String {
if self {
Expand Down
Loading

0 comments on commit e037236

Please sign in to comment.