Skip to content

Commit

Permalink
Merge branch 'main' into fix
Browse files Browse the repository at this point in the history
  • Loading branch information
bobzhang authored Oct 18, 2024
2 parents 5c9fb66 + e725480 commit c3109f0
Show file tree
Hide file tree
Showing 39 changed files with 306 additions and 69 deletions.
15 changes: 1 addition & 14 deletions builtin/string.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,6 @@ pub fn substring(
~start : Int = 0,
~end : Int = self.length()
) -> String {
if start < 0 {
abort("String::substring: start index is negative")
}
if end < 0 {
abort("String::substring: end index is negative")
}
if start > end {
abort("String::substring: start index is greater than end index")
}
if end > self.length() {
abort(
"String::substring: end index is greater than the length of the string",
)
}
guard start >= 0 && start <= end && end <= self.length()
unsafe_substring(self, start, end)
}
14 changes: 14 additions & 0 deletions builtin/stringbuilder_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// when we write the string into the buffer
// we assume the string is utf16 and blit
// we assume the bytes (in valid region [0, len)) is utf16
// so that
fn id(s : String) -> String {
StringBuilder::new()..write_string(s).to_string()
}

test "stringbuilder" {
let buf = StringBuilder::new()
buf.write_string("hello")
buf.write_char(' ')
buf.write_sub_string("world", 0, 3)
inspect!(buf.to_string(), content="hello wor")
}

test {
let data = ["a", "b", "c", "hello world"]
@json.inspect!(data.map(id), content=["a", "b", "c", "hello world"])
// assert_eq!(StringBuilder::new()..write_string("hello").to_string(), "hello")
}
2 changes: 1 addition & 1 deletion bytes/bytes.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn Bytes::from_iter(iter : Iter[Byte]) -> Bytes {
/// ```moonbit
/// let b = @bytes.of([b'\x41', b'\x00', b'\x42'])
/// ```
///
/// TODO: marked as intrinsic, inline if it is constant
pub fn Bytes::of(arr : FixedArray[Byte]) -> Bytes {
let rv = Bytes::new(arr.length())
for i = 0; i < arr.length(); i = i + 1 {
Expand Down
23 changes: 23 additions & 0 deletions bytes/bytes_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,29 @@ test "from_array" {
)
}

test "from_array literal" {
let b = @bytes.of([65, 0, 66, 0, 67, 0])
inspect!(
b,
content=
#|b"\x41\x00\x42\x00\x43\x00"
,
)
}

test "from array literal" {
let b1 = @bytes.of(
[
b'\x41', b'\x00', b'\x42', b'\x00', b'\x43', b'\x00', b'\x44', b'\x00', b'\x45',
b'\x00', b'\x46', b'\x00', b'\x47', b'\x00', b'\x48', b'\x00', b'\x49', b'\x00',
],
)
let b2 = @bytes.of(
[65, 0, 66, 0, 67, 0, 68, 0, 69, 0, 70, 0, 71, 0, 72, 0, 73, 0],
)
assert_eq!(b1, b2)
}

test "hash" {
let b1 = @bytes.of(
[
Expand Down
1 change: 0 additions & 1 deletion char/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/test",
"moonbitlang/core/coverage"
]
}
4 changes: 1 addition & 3 deletions deque/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/test",
"moonbitlang/core/coverage",
"moonbitlang/core/array"
"moonbitlang/core/coverage"
],
"targets": {
"panic_test.mbt": ["not", "native"]
Expand Down
1 change: 0 additions & 1 deletion double/internal/ryu/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/bool",
"moonbitlang/core/int64",
"moonbitlang/core/coverage"
]
}
1 change: 0 additions & 1 deletion double/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/bool",
"moonbitlang/core/int64",
"moonbitlang/core/coverage",
"moonbitlang/core/double/internal/ryu"
Expand Down
1 change: 0 additions & 1 deletion error/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/test",
"moonbitlang/core/coverage"
]
}
1 change: 0 additions & 1 deletion hashmap/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/coverage",
"moonbitlang/core/int",
"moonbitlang/core/test",
"moonbitlang/core/array",
"moonbitlang/core/tuple",
Expand Down
1 change: 0 additions & 1 deletion immut/array/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
"moonbitlang/core/builtin",
"moonbitlang/core/coverage",
"moonbitlang/core/quickcheck",
"moonbitlang/core/test",
{
"path": "moonbitlang/core/array",
"alias": "core/array"
Expand Down
1 change: 0 additions & 1 deletion immut/hashset/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
"moonbitlang/core/array",
"moonbitlang/core/coverage",
"moonbitlang/core/immut/internal/sparse_array",
"moonbitlang/core/test",
"moonbitlang/core/quickcheck",
"moonbitlang/core/tuple"
],
Expand Down
40 changes: 36 additions & 4 deletions immut/list/list.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,39 @@ pub fn T::to_string[A : Show](xs : T[A]) -> String {
Show::to_string(xs)
}

pub impl[A : ToJson] ToJson for T[A] with to_json(self) {
let capacity = self.length()
guard capacity != 0 else { return Array([]) }
let jsons = Array::new(~capacity)
self.each(fn(a) { jsons.push(a.to_json()) })
Array(jsons)
}

pub fn T::to_json[A : ToJson](self : T[A]) -> Json {
ToJson::to_json(self)
}

pub impl[A : @json.FromJson] @json.FromJson for T[A] with from_json(json, path) {
match json {
Array(arr) =>
for i = arr.length() - 1, list = Nil; i >= 0; {
continue i - 1, list.add(A::from_json!(arr[i], path))
} else {
list
}
_ =>
raise @json.JsonDecodeError(
(path, "@immut/list.from_json: expected array"),
)
}
}

pub fn T::from_json[A : @json.FromJson](
json : Json
) -> T[A][email protected] {
@json.from_json!(json)
}

/// Convert array to list.
///
/// # Example
Expand Down Expand Up @@ -224,11 +257,10 @@ pub fn unsafe_last[A](self : T[A]) -> A {
/// println(of([1, 2, 3, 4, 5]).last())
/// // output: Some(5)
/// ```
/// @alert deprecated "In the next release `last` will return an `Option`. Use `unsafe_last` instead"
pub fn last[A](self : T[A]) -> A {
pub fn last[A](self : T[A]) -> A? {
loop self {
Nil => abort("last of empty list")
Cons(head, Nil) => head
Nil => None
Cons(head, Nil) => Some(head)
Cons(_, tail) => continue tail
}
}
Expand Down
9 changes: 8 additions & 1 deletion immut/list/list.mbti
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package moonbitlang/core/immut/list

alias @moonbitlang/core/json as @json
alias @moonbitlang/core/quickcheck as @quickcheck

// Values
Expand Down Expand Up @@ -40,6 +41,7 @@ impl T {
foldi[A, B](Self[A], ~init : B, (Int, B, A) -> B) -> B
from_array[A](Array[A]) -> Self[A]
from_iter[A](Iter[A]) -> Self[A]
from_json[A : @json.FromJson](Json) -> Self[A][email protected]
head[A](Self[A]) -> A?
head_exn[A](Self[A]) -> A //deprecated
init_[A](Self[A]) -> Self[A]
Expand All @@ -50,7 +52,7 @@ impl T {
is_suffix[A : Eq](Self[A], Self[A]) -> Bool
iter[A](Self[A]) -> Iter[A]
iter2[A](Self[A]) -> Iter2[Int, A]
last[A](Self[A]) -> A //deprecated
last[A](Self[A]) -> A?
length[A](Self[A]) -> Int
lookup[A : Eq, B](Self[(A, B)], A) -> B?
map[A, B](Self[A], (A) -> B) -> Self[B]
Expand All @@ -75,6 +77,7 @@ impl T {
take[A](Self[A], Int) -> Self[A]
take_while[A](Self[A], (A) -> Bool) -> Self[A]
to_array[A](Self[A]) -> Array[A]
to_json[A : ToJson](Self[A]) -> Json
to_string[A : Show](Self[A]) -> String
unsafe_head[A](Self[A]) -> A
unsafe_last[A](Self[A]) -> A
Expand All @@ -91,5 +94,9 @@ impl T {
// Extension Methods
impl Show for T

impl ToJson for T

impl @json.FromJson for T

impl @quickcheck.Arbitrary for T

34 changes: 25 additions & 9 deletions immut/list/list_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ test "head" {
inspect!(el.head(), content="None")
}

// test "last" {
// let ls = @list.of([1, 2, 3, 4, 5])
// inspect!(ls.last(), content="5")
// }
test "last" {
let ls = @list.of([1, 2, 3, 4, 5])
inspect!(ls.last(), content="Some(5)")
}

test "init_" {
let ls = @list.of([1, 2, 3, 4, 5])
Expand Down Expand Up @@ -488,17 +488,33 @@ test "List::output with empty list" {
inspect!(buf, content="@list.of([])")
}

test "List::to_json with non-empty list" {
let list = @list.of([1, 2, 3, 4, 5])
@json.inspect!(ToJson::to_json(list), content=[1, 2, 3, 4, 5])
}

test "List::to_json with empty list" {
let list : @list.T[Int] = Nil
@json.inspect!(ToJson::to_json(list), content=[])
}

test "List::from_json" {
for xs in (@quickcheck.samples(20) : Array[@list.T[Int]]) {
assert_eq!(xs, @json.from_json!(xs.to_json()))
}
}

test "List::head_exn with non-empty list" {
let list = @list.of([1, 2, 3, 4, 5])
let head = @list.head(list)
assert_eq!(head, Some(1))
}

// test "List::last with non-empty list" {
// let list = @list.of([1, 2, 3, 4, 5])
// let last = @list.last(list)
// assert_eq!(last, 5)
// }
test "List::last with non-empty list" {
let list = @list.of([1, 2, 3, 4, 5])
let last = @list.last(list)
assert_eq!(last, Some(5))
}

test "List::zip with lists of equal length" {
let list1 = @list.of([1, 2, 3])
Expand Down
4 changes: 2 additions & 2 deletions immut/list/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/test",
"moonbitlang/core/array",
"moonbitlang/core/coverage",
"moonbitlang/core/quickcheck"
"moonbitlang/core/quickcheck",
"moonbitlang/core/json"
],
"targets": {
"panic_test.mbt": ["not", "native"]
Expand Down
6 changes: 3 additions & 3 deletions immut/list/panic_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// test "panic last" {
// @list.Nil.last()
// }
test "last None" {
assert_eq!(@list.Nil.last(), (None : Unit?))
}

test "panic nth_exn" {
@list.Nil.unsafe_nth(0)
Expand Down
1 change: 0 additions & 1 deletion immut/priority_queue/moon.pkg.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/array",
"moonbitlang/core/test",
"moonbitlang/core/coverage",
"moonbitlang/core/immut/list",
"moonbitlang/core/quickcheck"
Expand Down
4 changes: 2 additions & 2 deletions immut/sorted_map/moon.pkg.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"import": [
"moonbitlang/core/builtin",
"moonbitlang/core/test",
"moonbitlang/core/tuple",
"moonbitlang/core/string",
"moonbitlang/core/array",
"moonbitlang/core/coverage",
"moonbitlang/core/quickcheck"
"moonbitlang/core/quickcheck",
"moonbitlang/core/json"
],
"targets": {
"panic_wbtest.mbt": ["not", "native"]
Expand Down
7 changes: 7 additions & 0 deletions immut/sorted_map/sorted_map.mbti
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package moonbitlang/core/immut/sorted_map

alias @moonbitlang/core/json as @json
alias @moonbitlang/core/quickcheck as @quickcheck

// Values
Expand All @@ -19,6 +20,7 @@ impl T {
foldr_with_key[K, V, A](Self[K, V], (A, K, V) -> A, ~init : A) -> A
from_array[K : Compare + Eq, V](Array[(K, V)]) -> Self[K, V]
from_iter[K : Compare + Eq, V](Iter[(K, V)]) -> Self[K, V]
from_json[V : @json.FromJson](Json) -> Self[String, V][email protected]
insert[K : Compare + Eq, V](Self[K, V], K, V) -> Self[K, V]
is_empty[K, V](Self[K, V]) -> Bool
iter[K, V](Self[K, V]) -> Iter[(K, V)]
Expand All @@ -34,6 +36,7 @@ impl T {
singleton[K, V](K, V) -> Self[K, V]
size[K, V](Self[K, V]) -> Int
to_array[K, V](Self[K, V]) -> Array[(K, V)]
to_json[K : Show, V : ToJson](Self[K, V]) -> Json
to_string[K : Show, V : Show](Self[K, V]) -> String
}

Expand All @@ -44,5 +47,9 @@ impl T {
// Extension Methods
impl Show for T

impl ToJson for T

impl @json.FromJson for T

impl @quickcheck.Arbitrary for T

Loading

0 comments on commit c3109f0

Please sign in to comment.