-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
39 changed files
with
306 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"import": [ | ||
"moonbitlang/core/builtin", | ||
"moonbitlang/core/test", | ||
"moonbitlang/core/coverage" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"import": [ | ||
"moonbitlang/core/builtin", | ||
"moonbitlang/core/test", | ||
"moonbitlang/core/coverage" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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] | ||
|
@@ -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] | ||
|
@@ -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 | ||
|
@@ -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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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)] | ||
|
@@ -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 | ||
} | ||
|
||
|
@@ -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 | ||
|
Oops, something went wrong.