Skip to content

Commit a6ccbc0

Browse files
rvanasachristoph-dfinitycrusso
authored
Implicit pattern across entire core package (#398)
* First pass * Another pass * Fix stragglers * Fix more stragglers * Apply suggestions from code review Co-authored-by: Christoph <christoph.hegemann@dfinity.org> Co-authored-by: Claudio Russo <claudio@dfinity.org> * Apply suggestions from code review Co-authored-by: Claudio Russo <claudio@dfinity.org> * Various fixes * Apply suggestions from code review Co-authored-by: Claudio Russo <claudio@dfinity.org> * Fix * Update API lockfile --------- Co-authored-by: Christoph <christoph.hegemann@dfinity.org> Co-authored-by: Claudio Russo <claudio@dfinity.org>
1 parent 69a21d5 commit a6ccbc0

File tree

16 files changed

+272
-234
lines changed

16 files changed

+272
-234
lines changed

src/Array.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ module {
122122
/// Space: O(1)
123123
///
124124
/// *Runtime and space assumes that `equal` runs in O(1) time and space.
125-
public func equal<T>(array1 : [T], array2 : [T], equal : (T, T) -> Bool) : Bool {
125+
public func equal<T>(array1 : [T], array2 : [T], equal : (implicit : (T, T) -> Bool)) : Bool {
126126
let size1 = array1.size();
127127
let size2 = array2.size();
128128
if (size1 != size2) {
@@ -1032,7 +1032,7 @@ module {
10321032
/// Space: O(size)
10331033
///
10341034
/// *Runtime and space assumes that `f` runs in O(1) time and space.
1035-
public func toText<T>(array : [T], f : T -> Text) : Text {
1035+
public func toText<T>(array : [T], f : (implicit : (toText : T -> Text))) : Text {
10361036
let size = array.size();
10371037
if (size == 0) { return "[]" };
10381038
var text = "[";

src/Iter.mo

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ module {
662662
/// let iter = [1, 2, 3].values();
663663
/// assert ?3 == Iter.max<Nat>(iter, Nat.compare);
664664
/// ```
665-
public func max<T>(iter : Iter<T>, compare : (T, T) -> Order.Order) : ?T {
665+
public func max<T>(iter : Iter<T>, compare : (implicit : (T, T) -> Order.Order)) : ?T {
666666
reduce<T>(
667667
iter,
668668
func(a, b) {
@@ -683,7 +683,7 @@ module {
683683
/// let iter = [1, 2, 3].values();
684684
/// assert ?1 == Iter.min<Nat>(iter, Nat.compare);
685685
/// ```
686-
public func min<T>(iter : Iter<T>, compare : (T, T) -> Order.Order) : ?T {
686+
public func min<T>(iter : Iter<T>, compare : (implicit : (T, T) -> Order.Order)) : ?T {
687687
reduce<T>(
688688
iter,
689689
func(a, b) {
@@ -804,7 +804,7 @@ module {
804804
};
805805

806806
/// Sorted iterator. Will iterate over *all* elements to sort them, necessarily.
807-
public func sort<T>(iter : Iter<T>, compare : (T, T) -> Order.Order) : Iter<T> {
807+
public func sort<T>(iter : Iter<T>, compare : (implicit : (T, T) -> Order.Order)) : Iter<T> {
808808
let array = toVarArray<T>(iter);
809809
VarArray.sortInPlace<T>(array, compare);
810810
fromVarArray<T>(array)

src/List.mo

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ module {
596596
///
597597
/// Space: O(size)
598598
/// *Runtime and space assumes that `compare` runs in O(1) time and space.
599-
public func sort<T>(list : List<T>, compare : (T, T) -> Order.Order) {
599+
public func sort<T>(list : List<T>, compare : (implicit : (T, T) -> Order.Order)) {
600600
if (size(list) < 2) return;
601601
let arr = toVarArray(list);
602602
VarArray.sortInPlace(arr, compare);
@@ -785,7 +785,7 @@ module {
785785
/// Space: `O(1)`
786786
///
787787
/// *Runtime and space assumes that `compare` runs in `O(1)` time and space.
788-
public func binarySearch<T>(list : List<T>, compare : (T, T) -> Order.Order, element : T) : {
788+
public func binarySearch<T>(list : List<T>, compare : (implicit : (T, T) -> Order.Order), element : T) : {
789789
#found : Nat;
790790
#insertionIndex : Nat
791791
} {
@@ -1572,7 +1572,7 @@ module {
15721572
/// Space: `O(1)`
15731573
///
15741574
/// *Runtime and space assumes that `compare` runs in O(1) time and space.
1575-
public func max<T>(list : List<T>, compare : (T, T) -> Order.Order) : ?T {
1575+
public func max<T>(list : List<T>, compare : (implicit : (T, T) -> Order.Order)) : ?T {
15761576
if (isEmpty(list)) return null;
15771577

15781578
var maxSoFar = at(list, 0);
@@ -1607,7 +1607,7 @@ module {
16071607
/// Space: `O(1)`
16081608
///
16091609
/// *Runtime and space assumes that `compare` runs in O(1) time and space.
1610-
public func min<T>(list : List<T>, compare : (T, T) -> Order.Order) : ?T {
1610+
public func min<T>(list : List<T>, compare : (implicit : (T, T) -> Order.Order)) : ?T {
16111611
if (isEmpty(list)) return null;
16121612

16131613
var minSoFar = at(list, 0);
@@ -1681,7 +1681,7 @@ module {
16811681
/// Space: `O(1)`
16821682
///
16831683
/// *Runtime and space assumes that `compare` runs in O(1) time and space.
1684-
public func compare<T>(list1 : List<T>, list2 : List<T>, compare : (T, T) -> Order.Order) : Order.Order {
1684+
public func compare<T>(list1 : List<T>, list2 : List<T>, compare : (implicit : (T, T) -> Order.Order)) : Order.Order {
16851685
let size1 = size(list1);
16861686
let size2 = size(list2);
16871687
let minSize = if (size1 < size2) { size1 } else { size2 };
@@ -1717,7 +1717,7 @@ module {
17171717
/// Space: `O(size)`
17181718
///
17191719
/// *Runtime and space assumes that `toText` runs in O(1) time and space.
1720-
public func toText<T>(list : List<T>, f : T -> Text) : Text {
1720+
public func toText<T>(list : List<T>, f : (implicit : (toText : T -> Text))) : Text {
17211721
let vsize : Int = size(list);
17221722
let next = values_(list).unsafe_next;
17231723
var i = 0;

src/Map.mo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,11 +1260,11 @@ module {
12601260
/// assuming that `keyFormat` and `valueFormat` have runtime and space costs of `O(1)`.
12611261
///
12621262
/// Note: Creates `O(log(n))` temporary objects that will be collected as garbage.
1263-
public func toText<K, V>(map : Map<K, V>, toText : (implicit : K -> Text), valueFormat : V -> Text) : Text {
1263+
public func toText<K, V>(map : Map<K, V>, keyFormat : (implicit : (toText : K -> Text)), valueFormat : (implicit : (toText : V -> Text))) : Text {
12641264
var text = "Map{";
12651265
var sep = "";
12661266
for ((key, value) in entries(map)) {
1267-
text #= sep # "(" # toText(key) # ", " # valueFormat(value) # ")";
1267+
text #= sep # "(" # keyFormat(key) # ", " # valueFormat(value) # ")";
12681268
sep := ", "
12691269
};
12701270
text # "}"
@@ -1311,7 +1311,7 @@ module {
13111311
/// assuming that `compareKey` and `compareValue` have runtime and space costs of `O(1)`.
13121312
///
13131313
/// Note: Creates `O(log(n))` temporary objects that will be collected as garbage.
1314-
public func compare<K, V>(map1 : Map<K, V>, map2 : Map<K, V>, compareKey : (K, K) -> Order.Order, compareValue : (V, V) -> Order.Order) : Order.Order {
1314+
public func compare<K, V>(map1 : Map<K, V>, map2 : Map<K, V>, compareKey : (implicit : (compare : (K, K) -> Order.Order)), compareValue : (implicit : (compare : (V, V) -> Order.Order))) : Order.Order {
13151315
let iterator1 = entries(map1);
13161316
let iterator2 = entries(map2);
13171317
loop {
@@ -2314,7 +2314,7 @@ module {
23142314
/// * compare - the comparator used to perform the search
23152315
/// * searchKey - the key being compared against in the search
23162316
/// * maxIndex - the right-most index (bound) from which to begin the search
2317-
public func binarySearchNode<K, V>(array : [var ?(K, V)], compare : (K, K) -> Order.Order, searchKey : K, maxIndex : Nat) : SearchResult {
2317+
public func binarySearchNode<K, V>(array : [var ?(K, V)], compare : (implicit : (K, K) -> Order.Order), searchKey : K, maxIndex : Nat) : SearchResult {
23182318
// TODO: get rid of this check?
23192319
// Trap if array is size 0 (should not happen)
23202320
if (array.size() == 0) {

src/Option.mo

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ module {
4040

4141
/// Unwraps an optional value using a function, or returns the default, i.e.
4242
/// `option(?x, f, d) = f x` and `option(null, f, d) = d`.
43-
public func getMapped<A, B>(x : ?A, f : A -> B, default : B) : B = switch x {
43+
public func getMapped<T, R>(x : ?T, f : T -> R, default : R) : R = switch x {
4444
case null { default };
4545
case (?x_) { f(x_) }
4646
};
@@ -51,7 +51,7 @@ module {
5151
/// assert Option.map<Nat, Nat>(?42, func x = x + 1) == ?43;
5252
/// assert Option.map<Nat, Nat>(null, func x = x + 1) == null;
5353
/// ```
54-
public func map<A, B>(x : ?A, f : A -> B) : ?B = switch x {
54+
public func map<T, R>(x : ?T, f : T -> R) : ?R = switch x {
5555
case null { null };
5656
case (?x_) { ?f(x_) }
5757
};
@@ -67,14 +67,14 @@ module {
6767
/// Option.forEach(null, func (x : Nat) { counter += x });
6868
/// assert counter == 5;
6969
/// ```
70-
public func forEach<A>(x : ?A, f : A -> ()) = switch x {
70+
public func forEach<T>(x : ?T, f : T -> ()) = switch x {
7171
case null {};
7272
case (?x_) { f(x_) }
7373
};
7474

7575
/// Applies an optional function to an optional value. Returns `null` if at
7676
/// least one of the arguments is `null`.
77-
public func apply<A, B>(x : ?A, f : ?(A -> B)) : ?B {
77+
public func apply<T, R>(x : ?T, f : ?(T -> R)) : ?R {
7878
switch (f, x) {
7979
case (?f_, ?x_) { ?f_(x_) };
8080
case (_, _) { null }
@@ -83,7 +83,7 @@ module {
8383

8484
/// Applies a function to an optional value. Returns `null` if the argument is
8585
/// `null`, or the function returns `null`.
86-
public func chain<A, B>(x : ?A, f : A -> ?B) : ?B {
86+
public func chain<T, R>(x : ?T, f : T -> ?R) : ?R {
8787
switch (x) {
8888
case (?x_) { f(x_) };
8989
case (null) { null }
@@ -97,16 +97,16 @@ module {
9797
/// assert Option.flatten(?(null)) == null;
9898
/// assert Option.flatten(null) == null;
9999
/// ```
100-
public func flatten<A>(x : ??A) : ?A {
101-
chain<?A, A>(x, func(x_ : ?A) : ?A = x_)
100+
public func flatten<T>(x : ??T) : ?T {
101+
chain<?T, T>(x, func(x_ : ?T) : ?T = x_)
102102
};
103103

104104
/// Creates an optional value from a definite value.
105105
/// ```motoko
106106
/// import Option "mo:core/Option";
107107
/// assert Option.some(42) == ?42;
108108
/// ```
109-
public func some<A>(x : A) : ?A = ?x;
109+
public func some<T>(x : T) : ?T = ?x;
110110

111111
/// Returns true if the argument is not `null`, otherwise returns false.
112112
public func isSome(x : ?Any) : Bool {
@@ -119,7 +119,7 @@ module {
119119
};
120120

121121
/// Returns true if the optional arguments are equal according to the equality function provided, otherwise returns false.
122-
public func equal<A>(x : ?A, y : ?A, eq : (A, A) -> Bool) : Bool = switch (x, y) {
122+
public func equal<T>(x : ?T, y : ?T, eq : (implicit : (equal : (T, T) -> Bool))) : Bool = switch (x, y) {
123123
case (null, null) { true };
124124
case (?x_, ?y_) { eq(x_, y_) };
125125
case (_, _) { false }
@@ -132,7 +132,7 @@ module {
132132
/// - `#less` if the first value is `null` and the second is not,
133133
/// - `#greater` if the first value is not `null` and the second is,
134134
/// - the result of the comparison function when both values are not `null`.
135-
public func compare<A>(x : ?A, y : ?A, cmp : (A, A) -> Types.Order) : Types.Order = switch (x, y) {
135+
public func compare<T>(x : ?T, y : ?T, cmp : (implicit : (compare : (T, T) -> Types.Order))) : Types.Order = switch (x, y) {
136136
case (null, null) #equal;
137137
case (null, _) #less;
138138
case (_, null) #greater;
@@ -148,7 +148,7 @@ module {
148148
};
149149

150150
/// Returns the textural representation of an optional value for debugging purposes.
151-
public func toText<A>(x : ?A, toText : A -> Text) : Text = switch x {
151+
public func toText<T>(x : ?T, toText : (implicit : T -> Text)) : Text = switch x {
152152
case null { "null" };
153153
case (?x_) { "?" # toText(x_) }
154154
};

src/Queue.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -736,7 +736,7 @@ module {
736736
/// Runtime: O(n)
737737
/// Space: O(n)
738738
/// `n` denotes the number of elements stored in the queue.
739-
public func toText<T>(queue : Queue<T>, format : T -> Text) : Text {
739+
public func toText<T>(queue : Queue<T>, format : (implicit : (toText : T -> Text))) : Text {
740740
var text = "Queue[";
741741
var sep = "";
742742
for (element in values(queue)) {
@@ -765,7 +765,7 @@ module {
765765
/// Runtime: O(n)
766766
/// Space: O(1)
767767
/// `n` denotes the number of elements stored in the queue.
768-
public func compare<T>(queue1 : Queue<T>, queue2 : Queue<T>, compare : (T, T) -> Order.Order) : Order.Order {
768+
public func compare<T>(queue1 : Queue<T>, queue2 : Queue<T>, compare : (implicit : (T, T) -> Order.Order)) : Order.Order {
769769
let iterator1 = values(queue1);
770770
let iterator2 = values(queue2);
771771
loop {

src/Result.mo

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ module {
6363
public func equal<Ok, Err>(
6464
result1 : Result<Ok, Err>,
6565
result2 : Result<Ok, Err>,
66-
equalOk : (Ok, Ok) -> Bool,
67-
equalErr : (Err, Err) -> Bool
66+
equalOk : (implicit : (equal : Ok, Ok) -> Bool),
67+
equalErr : (implicit : (equal : (Err, Err) -> Bool))
6868
) : Bool {
6969
switch (result1, result2) {
7070
case (#ok(ok1), #ok(ok2)) {
@@ -96,8 +96,8 @@ module {
9696
public func compare<Ok, Err>(
9797
result1 : Result<Ok, Err>,
9898
result2 : Result<Ok, Err>,
99-
compareOk : (Ok, Ok) -> Order.Order,
100-
compareErr : (Err, Err) -> Order.Order
99+
compareOk : (implicit : (compare : (Ok, Ok) -> Order.Order)),
100+
compareErr : (implicit : (compare : (Err, Err) -> Order.Order))
101101
) : Order.Order {
102102
switch (result1, result2) {
103103
case (#ok(ok1), #ok(ok2)) {

src/Stack.mo

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -741,7 +741,7 @@ module {
741741
/// Space: O(n)
742742
/// where `n` denotes the number of elements stored on the stack and
743743
/// assuming that `format` has O(1) costs.
744-
public func toText<T>(stack : Stack<T>, format : T -> Text) : Text {
744+
public func toText<T>(stack : Stack<T>, format : (implicit : (toText : T -> Text))) : Text {
745745
var text = "Stack[";
746746
var sep = "";
747747
for (element in values(stack)) {
@@ -770,7 +770,7 @@ module {
770770
/// Space: O(1)
771771
/// where `n` denotes the number of elements stored on the stack and
772772
/// assuming that `compare` has O(1) costs.
773-
public func compare<T>(stack1 : Stack<T>, stack2 : Stack<T>, compare : (T, T) -> Order.Order) : Order.Order {
773+
public func compare<T>(stack1 : Stack<T>, stack2 : Stack<T>, compare : (implicit : (T, T) -> Order.Order)) : Order.Order {
774774
let iterator1 = values(stack1);
775775
let iterator2 = values(stack2);
776776
loop {

src/Text.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -903,7 +903,7 @@ module {
903903
public func compareWith(
904904
t1 : Text,
905905
t2 : Text,
906-
cmp : (Char, Char) -> Order.Order
906+
cmp : (implicit : (compare : (Char, Char) -> Order.Order))
907907
) : Order.Order {
908908
let cs1 = t1.chars();
909909
let cs2 = t2.chars();

src/VarArray.mo

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,7 @@ module {
11701170
/// Space: O(size)
11711171
///
11721172
/// *Runtime and space assumes that `f` runs in O(1) time and space.
1173-
public func toText<T>(array : [var T], f : T -> Text) : Text {
1173+
public func toText<T>(array : [var T], f : (implicit : (toText : T -> Text))) : Text {
11741174
let size = array.size();
11751175
if (size == 0) { return "[var]" };
11761176
var text = "[var ";

0 commit comments

Comments
 (0)