Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make bots happy #651

Draft
wants to merge 9 commits into
base: master
Choose a base branch
from
Draft
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
11 changes: 11 additions & 0 deletions src/Array.mo
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ module {
};
return null
};
public func exists<X>(array : [X], predicate : X -> Bool) : Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called some in Trie.mo and List.mo. I'd hate to introduce inconsistency. Does the bot insist one this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also would need doc and some tests, ideally.

Option.isSome(find(array, predicate))
};
public func forall<X>(array : [X], predicate : X -> Bool) : Bool {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is called all in Trie.mo and List.mo. Also would need doc and tests.

not exists<X>(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;
Expand Down Expand Up @@ -207,6 +213,10 @@ module {
sortInPlace(temp, compare);
freeze(temp)
};
public func sortByLessThanOrEqual<X>(array : [X], isLessThanOrEqual : (X, X) -> Bool) : [X] {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this still an isse with the revised prompt? The bot was having trouble with defining functions returning Order.order because the prompt was actually misleadingly broken.

Maybe we could call lessThanOrEqual totalOrder?

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.
Expand Down Expand Up @@ -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`
Expand Down
26 changes: 26 additions & 0 deletions src/Buffer.mo
Original file line number Diff line number Diff line change
Expand Up @@ -1050,6 +1050,18 @@ module {

null
};
public func find<X>(buffer : Buffer<X>, 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.
Expand Down Expand Up @@ -2368,6 +2380,20 @@ module {

(buffer1, buffer2)
};
/// Slice buffer from [start, end).
public func slice<X>(buffer : Buffer<X>, start : Nat, end : Nat) : Buffer<X> {
let size = buffer.size();
if (start < 0 or start > size or end < 0 or end > size or start > end) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (start < 0 or start > size or end < 0 or end > size or start > end) {
if (start > size or end > size or start > end) {

Nat's are never < 0, right?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs better doc and test.

Prim.trap "Index out of bounds in slice"
};
let newBuffer = Buffer<X>(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
Expand Down
1 change: 1 addition & 0 deletions src/Int.mo
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ module {
public func abs(x : Int) : Nat {
Prim.abs(x)
};
public func fromNat(n : Nat) : Int = n;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needs doc .
Maybe we could add a deprecation warning to say its not really necessary (due to subtyping)


/// Converts an integer number to its textual representation. Textual
/// representation _do not_ contain underscores to represent commas.
Expand Down
6 changes: 6 additions & 0 deletions src/Nat.mo
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Int "Int";
import Order "Order";
import Prim "mo:⛔";
import Char "Char";
import Hash "Hash";
import Iter "Iter";

module {

Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hash.hash is suboptimal. Do we really want to spread it's use?

public func abs(n : Int) : Nat = Int.abs n;
public func fromInt(n : Int) : Nat = Int.abs n;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fromInt should trap on negative?

public let range = Iter.range;

/// Creates a natural number from its textual representation. Returns `null`
/// if the input is not a valid natural number.
Expand Down
2 changes: 2 additions & 0 deletions src/Nat32.mo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/// ```
import Nat "Nat";
import Prim "mo:⛔";
import Hash "Hash";

module {

Expand All @@ -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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same argument about Hash.hash


/// Converts an unsigned integer with infinite precision to a 32-bit unsigned integer.
///
Expand Down
2 changes: 2 additions & 0 deletions src/Nat64.mo
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
/// ```
import Nat "Nat";
import Prim "mo:⛔";
import Hash "Hash";

module {

Expand Down Expand Up @@ -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.
///
Expand Down
14 changes: 13 additions & 1 deletion src/Order.mo
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,17 @@ module {
case _ { false }
}
};

public func lteToOrder<X>(isLessThanOrEqual : (X, X) -> Bool) : (X, X) -> Order {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if there is a better name for this...

totalOrderToComparer

or

lessThanOrEqualToCompare

or
compareBy

func (a : X, b : X) : Order {
if (isLessThanOrEqual(a, b)) {
if (isLessThanOrEqual(b, a)) {
#equal;
} else {
#less;
}
} else {
#greater;
};
};
};
}
3 changes: 2 additions & 1 deletion test/js/importAll.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
}
})();