Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Blob.mo
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ module {
/// let result = Blob.compare(blob1, blob2);
/// assert result == #less;
/// ```
public func compare(b1 : Blob, b2 : Blob) : Order.Order {
public persistent func compare(b1 : Blob, b2 : Blob) : Order.Order {
let c = Prim.blobCompare(b1, b2);
if (c < 0) #less else if (c == 0) #equal else #greater
};
Expand Down
2 changes: 1 addition & 1 deletion src/Bool.mo
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ module {
/// assert Bool.compare(true, true) == #equal;
/// assert Bool.compare(false, true) == #less;
/// ```
public func compare(a : Bool, b : Bool) : Order.Order {
public persistent func compare(a : Bool, b : Bool) : Order.Order {
if (a == b) #equal else if a #greater else #less
};

Expand Down
2 changes: 1 addition & 1 deletion src/Char.mo
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ module {
/// assert Char.compare('B', 'A') == #greater;
/// assert Char.compare('A', 'A') == #equal;
/// ```
public func compare(a : Char, b : Char) : { #less; #equal; #greater } {
public persistent func compare(a : Char, b : Char) : { #less; #equal; #greater } {
if (a < b) { #less } else if (a == b) { #equal } else { #greater }
};

Expand Down
10 changes: 6 additions & 4 deletions src/Float.mo
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ module {
/// ```motoko include=import
/// assert Float.isNaN(0.0/0.0);
/// ```
public func isNaN(number : Float) : Bool {
public persistent func isNaN(number : Float) : Bool {
number != number
};

Expand Down Expand Up @@ -194,7 +194,9 @@ module {
/// let epsilon = 1e-6;
/// assert Float.equal(Float.copySign(1.2, -2.3), -1.2, epsilon);
/// ```
public let copySign : (x : Float, y : Float) -> Float = Prim.floatCopySign;
public persistent func copySign(x : Float, y : Float): Float {
Prim.floatCopySign(x, y)
};

/// Returns the smaller value of `x` and `y`.
///
Expand Down Expand Up @@ -608,7 +610,7 @@ module {
/// ```motoko include=import
/// assert Float.compare(0.123, 0.1234) == #less;
/// ```
public func compare(x : Float, y : Float) : Order.Order {
public persistent func compare(x : Float, y : Float) : Order.Order {
if (isNaN(x)) {
if (isNegative(x)) {
if (isNaN(y) and isNegative(y)) { #equal } else { #less }
Expand All @@ -626,7 +628,7 @@ module {
}
};

func isNegative(number : Float) : Bool {
persistent func isNegative(number : Float) : Bool {
copySign(1.0, number) < 0.0
};

Expand Down
2 changes: 1 addition & 1 deletion src/Int.mo
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([1, -2, -3], Int.compare) == [-3, -2, 1];
/// ```
public func compare(x : Int, y : Int) : Order.Order {
public persistent func compare(x : Int, y : Int) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Int16.mo
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([1, -2, -3] : [Int16], Int16.compare) == [-3, -2, 1];
/// ```
public func compare(x : Int16, y : Int16) : Order.Order {
public persistent func compare(x : Int16, y : Int16) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Int32.mo
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([1, -2, -3] : [Int32], Int32.compare) == [-3, -2, 1];
/// ```
public func compare(x : Int32, y : Int32) : Order.Order {
public persistent func compare(x : Int32, y : Int32) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Int64.mo
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([1, -2, -3] : [Int64], Int64.compare) == [-3, -2, 1];
/// ```
public func compare(x : Int64, y : Int64) : Order.Order {
public persistent func compare(x : Int64, y : Int64) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Int8.mo
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([1, -2, -3] : [Int8], Int8.compare) == [-3, -2, 1];
/// ```
public func compare(x : Int8, y : Int8) : Order.Order {
public persistent func compare(x : Int8, y : Int8) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Nat.mo
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([2, 3, 1], Nat.compare) == [1, 2, 3];
/// ```
public func compare(x : Nat, y : Nat) : Order.Order {
public persistent func compare(x : Nat, y : Nat) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Nat16.mo
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([2, 3, 1] : [Nat16], Nat16.compare) == [1, 2, 3];
/// ```
public func compare(x : Nat16, y : Nat16) : Order.Order {
public persistent func compare(x : Nat16, y : Nat16) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Nat32.mo
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([2, 3, 1] : [Nat32], Nat32.compare) == [1, 2, 3];
/// ```
public func compare(x : Nat32, y : Nat32) : Order.Order {
public persistent func compare(x : Nat32, y : Nat32) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Nat64.mo
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([2, 3, 1] : [Nat64], Nat64.compare) == [1, 2, 3];
/// ```
public func compare(x : Nat64, y : Nat64) : Order.Order {
public persistent func compare(x : Nat64, y : Nat64) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Nat8.mo
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ module {
/// import Array "mo:core/Array";
/// assert Array.sort([2, 3, 1] : [Nat8], Nat8.compare) == [1, 2, 3];
/// ```
public func compare(x : Nat8, y : Nat8) : Order.Order {
public persistent func compare(x : Nat8, y : Nat8) : Order.Order {
if (x < y) { #less } else if (x == y) { #equal } else { #greater }
};

Expand Down
2 changes: 1 addition & 1 deletion src/Option.mo
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ module {
/// - `#less` if the first value is `null` and the second is not,
/// - `#greater` if the first value is not `null` and the second is,
/// - the result of the comparison function when both values are not `null`.
public func compare<A>(x : ?A, y : ?A, cmp : (A, A) -> Types.Order) : Types.Order = switch (x, y) {
public persistent func compare<A>(x : ?A, y : ?A, cmp : (A, A) -> Types.Order) : Types.Order = switch (x, y) {
case (null, null) #equal;
case (null, _) #less;
case (_, null) #greater;
Expand Down
2 changes: 1 addition & 1 deletion src/Principal.mo
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ module {
/// let principal2 = Principal.fromText("un4fu-tqaaa-aaaab-qadjq-cai");
/// assert Principal.compare(principal1, principal2) == #equal;
/// ```
public func compare(principal1 : Principal, principal2 : Principal) : {
public persistent func compare(principal1 : Principal, principal2 : Principal) : {
#less;
#equal;
#greater
Expand Down
34 changes: 34 additions & 0 deletions src/Queue.mo
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,40 @@ module {
}
};

/// Returns an iterator over the elements in the queue, in reverse order.
/// Iterates from back to front.
///
/// Example:
/// ```motoko
/// import Queue "mo:core/Queue";
/// persistent actor {
/// let queue = Queue.fromIter<Text>(["A", "B", "C"].values());
/// transient let iter = Queue.reverseValues(queue);
/// assert iter.next() == ?"C";
/// assert iter.next() == ?"B";
/// assert iter.next() == ?"A";
/// assert iter.next() == null;
/// }
/// ```
///
/// Runtime: O(1) for iterator creation, O(n) for full iteration
/// Space: O(1)
public func reverseValues<T>(queue : Queue<T>) : Iter.Iter<T> {
object {
var current = queue.back;

public func next() : ?T {
switch (current) {
case null null;
case (?node) {
current := node.previous;
?node.value
}
}
}
}
};

/// Tests whether all elements in the queue satisfy the given predicate.
///
/// Example:
Expand Down
2 changes: 1 addition & 1 deletion src/Text.mo
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ module {
/// assert Text.compare("abc", "def") == #less;
/// assert Text.compare("abc", "ABC") == #greater;
/// ```
public func compare(t1 : Text, t2 : Text) : Order.Order {
public persistent func compare(t1 : Text, t2 : Text) : Order.Order {
let c = Prim.textCompare(t1, t2);
if (c < 0) #less else if (c == 0) #equal else #greater
};
Expand Down
Loading
Loading