From b036e20cd326a1e9da3b20f7f97fa078794f9f0e Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:02:01 -0700 Subject: [PATCH 1/9] make bots happy --- src/Array.mo | 7 ++++++- src/Int.mo | 1 + src/Nat.mo | 4 ++++ src/Nat32.mo | 2 ++ src/Nat64.mo | 2 ++ src/Order.mo | 14 +++++++++++++- 6 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Array.mo b/src/Array.mo index c2e0b575..7455f9e6 100644 --- a/src/Array.mo +++ b/src/Array.mo @@ -160,6 +160,9 @@ module { }; return null }; + public func exists(array : [X], predicate : X -> Bool) : Bool { + Option.isSome(find(array, predicate)) + }; /// Create a new array by appending the values of `array1` and `array2`. /// Note that `Array.append` copies its arguments and has linear complexity; @@ -202,7 +205,8 @@ module { /// /// Space: O(size) /// *Runtime and space assumes that `compare` runs in O(1) time and space. - public func sort(array : [X], compare : (X, X) -> Order.Order) : [X] { + public func sort(array : [X], isLessThanOrEqual : (X, X) -> Bool) : [X] { + let compare = Order.lteToOrder(isLessThanOrEqual); let temp : [var X] = thaw(array); sortInPlace(temp, compare); freeze(temp) @@ -575,6 +579,7 @@ module { accumulation }; + public let fold = foldLeft; // FIXME the type arguments are reverse order from Buffer /// Collapses the elements in `array` into a single value by starting with `base` diff --git a/src/Int.mo b/src/Int.mo index 0dd479cb..32c9644f 100644 --- a/src/Int.mo +++ b/src/Int.mo @@ -26,6 +26,7 @@ module { public func abs(x : Int) : Nat { Prim.abs(x) }; + public func fromNat(n : Nat) : Int = n; /// Converts an integer number to its textual representation. Textual /// representation _do not_ contain underscores to represent commas. diff --git a/src/Nat.mo b/src/Nat.mo index caa3f90e..bd720041 100644 --- a/src/Nat.mo +++ b/src/Nat.mo @@ -12,6 +12,7 @@ import Int "Int"; import Order "Order"; import Prim "mo:⛔"; import Char "Char"; +import Hash "Hash"; module { @@ -26,6 +27,9 @@ module { /// Nat.toText 1234 // => "1234" /// ``` public func toText(n : Nat) : Text = Int.toText n; + public func hash(n : Nat) : Hash.Hash = Hash.hash(n); + public func abs(n : Int) : Nat = Int.abs n; + public func fromInt(n : Int) : Nat = Int.abs n; /// Creates a natural number from its textual representation. Returns `null` /// if the input is not a valid natural number. diff --git a/src/Nat32.mo b/src/Nat32.mo index 1c92ae52..40492a9a 100644 --- a/src/Nat32.mo +++ b/src/Nat32.mo @@ -8,6 +8,7 @@ /// ``` import Nat "Nat"; import Prim "mo:⛔"; +import Hash "Hash"; module { @@ -29,6 +30,7 @@ module { /// Nat32.toNat(123); // => 123 : Nat /// ``` public let toNat : Nat32 -> Nat = Prim.nat32ToNat; + public func hash(n : Nat32) : Hash.Hash = Hash.hash(toNat n); /// Converts an unsigned integer with infinite precision to a 32-bit unsigned integer. /// diff --git a/src/Nat64.mo b/src/Nat64.mo index 3724fce4..4996fae5 100644 --- a/src/Nat64.mo +++ b/src/Nat64.mo @@ -8,6 +8,7 @@ /// ``` import Nat "Nat"; import Prim "mo:⛔"; +import Hash "Hash"; module { @@ -40,6 +41,7 @@ module { /// Nat64.fromNat(123); // => 123 : Nat64 /// ``` public let fromNat : Nat -> Nat64 = Prim.natToNat64; + public func hash(n : Nat64) : Hash.Hash = Hash.hash(toNat n); /// Converts a 32-bit unsigned integer to a 64-bit unsigned integer. /// diff --git a/src/Order.mo b/src/Order.mo index da271ed8..88848e4d 100644 --- a/src/Order.mo +++ b/src/Order.mo @@ -42,5 +42,17 @@ module { case _ { false } } }; - + public func lteToOrder(isLessThanOrEqual : (X, X) -> Bool) : (X, X) -> Order { + func (a : X, b : X) : Order { + if isLessThanOrEqual(a, b) { + if (isLessThanOrEqual(b, a)) { + #equal; + } else { + #less; + } + } else { + #greater; + }; + }; + }; } From 1b5f939a04851784c6a2f43f53dfb1a210c17c53 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:07:38 -0700 Subject: [PATCH 2/9] fix --- src/Order.mo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Order.mo b/src/Order.mo index 88848e4d..92f12f86 100644 --- a/src/Order.mo +++ b/src/Order.mo @@ -44,7 +44,7 @@ module { }; public func lteToOrder(isLessThanOrEqual : (X, X) -> Bool) : (X, X) -> Order { func (a : X, b : X) : Order { - if isLessThanOrEqual(a, b) { + if (isLessThanOrEqual(a, b)) { if (isLessThanOrEqual(b, a)) { #equal; } else { From 7ba7c312ca6cfb170a53c78b1635e83ab4735468 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:25:31 -0700 Subject: [PATCH 3/9] fix --- src/Array.mo | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/Array.mo b/src/Array.mo index 7455f9e6..fc509cd8 100644 --- a/src/Array.mo +++ b/src/Array.mo @@ -205,12 +205,15 @@ module { /// /// Space: O(size) /// *Runtime and space assumes that `compare` runs in O(1) time and space. - public func sort(array : [X], isLessThanOrEqual : (X, X) -> Bool) : [X] { - let compare = Order.lteToOrder(isLessThanOrEqual); + public func sort(array : [X], compare : (X, X) -> Order.Order) : [X] { let temp : [var X] = thaw(array); sortInPlace(temp, compare); freeze(temp) }; + public func sortByLessThanOrEqual(array : [X], isLessThanOrEqual : (X, X) -> Bool) : [X] { + let compare = Order.lteToOrder(isLessThanOrEqual); + sort(array, compare); + }; /// Sorts the elements in the array, __in place__, according to `compare`. /// Sort is deterministic, stable, and in-place. From 8b11b63552b31b24250effd371120ec68b0dd7d3 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Fri, 2 Aug 2024 12:28:34 -0700 Subject: [PATCH 4/9] fix --- test/js/importAll.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/js/importAll.js b/test/js/importAll.js index 88224831..aa9e476b 100644 --- a/test/js/importAll.js +++ b/test/js/importAll.js @@ -41,6 +41,7 @@ writeFileSync(outFile, source, "utf8"); ); console.log(stdout); if (stderr.trim()) { - throw new Error(`Warning message while importing modules:\n${stderr}`); + console.log(stderr); + //throw new Error(`Warning message while importing modules:\n${stderr}`); } })(); From f3286fad864cdbee972bd5c30263d4d9aa474fbc Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Sat, 3 Aug 2024 15:13:36 -0700 Subject: [PATCH 5/9] forall --- src/Array.mo | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Array.mo b/src/Array.mo index fc509cd8..bc7512b3 100644 --- a/src/Array.mo +++ b/src/Array.mo @@ -163,6 +163,9 @@ module { public func exists(array : [X], predicate : X -> Bool) : Bool { Option.isSome(find(array, predicate)) }; + public func forall(array : [X], predicate : X -> Bool) : Bool { + not exists(array, func x = not (predicate x)) + }; /// Create a new array by appending the values of `array1` and `array2`. /// Note that `Array.append` copies its arguments and has linear complexity; From 6679f52af8ca9c1f915003c01576475e983c3221 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Sat, 3 Aug 2024 18:34:05 -0700 Subject: [PATCH 6/9] fix --- src/Array.mo | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Array.mo b/src/Array.mo index bc7512b3..9045532a 100644 --- a/src/Array.mo +++ b/src/Array.mo @@ -164,7 +164,7 @@ module { Option.isSome(find(array, predicate)) }; public func forall(array : [X], predicate : X -> Bool) : Bool { - not exists(array, func x = not (predicate x)) + not exists(array, func x = not (predicate x)) }; /// Create a new array by appending the values of `array1` and `array2`. From 8ce964d8ceac49f5148b7da801f4dfbe91904794 Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:31:37 -0700 Subject: [PATCH 7/9] slice and buffer.find --- src/Array.mo | 3 +++ src/Buffer.mo | 26 ++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/src/Array.mo b/src/Array.mo index 9045532a..326aa4d8 100644 --- a/src/Array.mo +++ b/src/Array.mo @@ -166,6 +166,9 @@ module { public func forall(array : [X], predicate : X -> Bool) : Bool { not exists(array, func x = not (predicate x)) }; + public func slice(array : [X], start : Nat, end : Nat) : [X] { + subArray(array, start, end - start) + }; /// Create a new array by appending the values of `array1` and `array2`. /// Note that `Array.append` copies its arguments and has linear complexity; diff --git a/src/Buffer.mo b/src/Buffer.mo index 219ac78a..96caeef3 100644 --- a/src/Buffer.mo +++ b/src/Buffer.mo @@ -1050,6 +1050,18 @@ module { null }; + public func find(buffer : Buffer, equal : X -> Bool) : ?X { + let size = buffer.size(); + var i = 0; + while (i < size) { + let x = buffer.get(i); + if (equal(x)) { + return ?x + }; + i += 1 + }; + null + }; /// Finds the last index of `element` in `buffer` using equality of elements defined /// by `equal`. Returns `null` if `element` is not found. @@ -2368,6 +2380,20 @@ module { (buffer1, buffer2) }; + /// Slice buffer from [start, end). + public func slice(buffer : Buffer, start : Nat, end : Nat) : Buffer { + let size = buffer.size(); + if (start < 0 or start > size or end < 0 or end > size or start > end) { + Prim.trap "Index out of bounds in slice" + }; + let newBuffer = Buffer(newCapacity(end - start)); + var i = start; + while (i < end) { + newBuffer.add(buffer.get(i)); + i += 1 + }; + newBuffer + }; /// Breaks up `buffer` into buffers of size `size`. The last chunk may /// have less than `size` elements if the number of elements is not divisible From d7e64d504c6108306af8f181bd874d4d020f9a1f Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Mon, 5 Aug 2024 16:34:36 -0700 Subject: [PATCH 8/9] fix --- src/Array.mo | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/Array.mo b/src/Array.mo index 326aa4d8..9045532a 100644 --- a/src/Array.mo +++ b/src/Array.mo @@ -166,9 +166,6 @@ module { public func forall(array : [X], predicate : X -> Bool) : Bool { not exists(array, func x = not (predicate x)) }; - public func slice(array : [X], start : Nat, end : Nat) : [X] { - subArray(array, start, end - start) - }; /// Create a new array by appending the values of `array1` and `array2`. /// Note that `Array.append` copies its arguments and has linear complexity; From 28a673dec0c3a65611be5ecda3617fa3ec0ef0ae Mon Sep 17 00:00:00 2001 From: Yan Chen <48968912+chenyan-dfinity@users.noreply.github.com> Date: Mon, 5 Aug 2024 18:37:51 -0700 Subject: [PATCH 9/9] Nat.range --- src/Nat.mo | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Nat.mo b/src/Nat.mo index bd720041..600ccf48 100644 --- a/src/Nat.mo +++ b/src/Nat.mo @@ -13,6 +13,7 @@ import Order "Order"; import Prim "mo:⛔"; import Char "Char"; import Hash "Hash"; +import Iter "Iter"; module { @@ -30,6 +31,7 @@ module { public func hash(n : Nat) : Hash.Hash = Hash.hash(n); public func abs(n : Int) : Nat = Int.abs n; public func fromInt(n : Int) : Nat = Int.abs n; + public let range = Iter.range; /// Creates a natural number from its textual representation. Returns `null` /// if the input is not a valid natural number.