diff --git a/src/Array.mo b/src/Array.mo index c2e0b575..9045532a 100644 --- a/src/Array.mo +++ b/src/Array.mo @@ -160,6 +160,12 @@ module { }; return null }; + 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; @@ -207,6 +213,10 @@ module { 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. @@ -575,6 +585,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/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 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..600ccf48 100644 --- a/src/Nat.mo +++ b/src/Nat.mo @@ -12,6 +12,8 @@ import Int "Int"; import Order "Order"; import Prim "mo:⛔"; import Char "Char"; +import Hash "Hash"; +import Iter "Iter"; module { @@ -26,6 +28,10 @@ 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; + public let range = Iter.range; /// 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..92f12f86 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; + }; + }; + }; } 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}`); } })();