Skip to content

Commit

Permalink
Upgrade moc/base.
Browse files Browse the repository at this point in the history
  • Loading branch information
q-uint committed Dec 15, 2022
1 parent fc50c18 commit 89bb25b
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 72 deletions.
14 changes: 0 additions & 14 deletions .github/workflows/src.yml

This file was deleted.

17 changes: 8 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
on:
push:
paths:
- 'src/**'
- 'test/**'
on: push
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: aviate-labs/setup-dfx@v0.2.2
- uses: actions/checkout@v3
- uses: aviate-labs/setup-dfx@v0.2.3
with:
dfx-version: 0.8.1
dfx-version: 0.12.1
install-moc: true
- run: for i in test/*.mo ; do moc --package base $(dfx cache show)/base -r $i ; done
vessel-version: 0.6.3
- run: |
make check
make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.vessel/
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.PHONY: check test
check:
find src -type f -name '*.mo' -print0 | xargs -0 $(shell vessel bin)/moc $(shell vessel sources 2>/dev/null) --check
test:
find test -type f -name '*.mo' -print0 | xargs -0 $(shell vessel bin)/moc $(shell vessel sources 2>/dev/null) -r
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,30 @@ Contains some (complementing) functions in addition to `mo:base/Array`.

Checks whether an array contains a given value.

```
```motoko
contains : (xs : [T], y : T, equal : (T, T) -> Bool) -> Bool
```

### Drop

Drops the first 'n' elements of an array, returns the remainder of that array.

```
```motoko
drop<T> : (xs : [T], n : Nat) -> [T]
```

### Split

Splits an array in two parts, based on the given element index.

```
```motoko
split<T> : (xs : [T], n : Nat) -> ([T], [T])
```

### Take

Returns the first 'n' elements of an array.

```
```motoko
take<T> : (xs : [T], n : Nat) -> [T]
```
2 changes: 2 additions & 0 deletions package-set.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let base = https://github.com/internet-computer/base-package-set/releases/download/moc-0.7.4/package-set.dhall sha256:3a20693fc597b96a8c7cf8645fda7a3534d13e5fbda28c00d01f0b7641efe494
in base
12 changes: 6 additions & 6 deletions src/Array.mo
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Prim "mo:⛔";
import { Array_tabulate } = "mo:⛔";

module {
// Checks whether an array contains a given value.
Expand All @@ -13,14 +13,14 @@ module {
let xS = xs.size();
if (xS <= n) return [];
let s = xS - n : Nat;
Prim.Array_tabulate<T>(s, func (i : Nat) : T { xs[n + i]; });
Array_tabulate<T>(s, func (i : Nat) : T { xs[n + i]; });
};

// Slices out [i-j[ elements of an array.
public func slice<T>(xs : [T], i : Nat, j : Nat) : [T] {
if (j < i) return [];
if (j == i) return [xs[i]];
Prim.Array_tabulate<T>(j - i, func (k : Nat) : T { xs[i+k]; });
Array_tabulate<T>(j - i, func (k : Nat) : T { xs[i+k]; });
};

// Splits an array in two parts, based on the given element index.
Expand All @@ -30,15 +30,15 @@ module {
if (xS <= n) { return ([] : [T], xs); };
let s = xS - n : Nat;
(
Prim.Array_tabulate<T>(n, func (i : Nat) : T { xs[i]; }),
Prim.Array_tabulate<T>(s, func (i : Nat) : T { xs[n + i]; })
Array_tabulate<T>(n, func (i : Nat) : T { xs[i]; }),
Array_tabulate<T>(s, func (i : Nat) : T { xs[n + i]; })
);
};

// Returns the first 'n' elements of an array.
public func take<T>(xs : [T], n : Nat) : [T] {
let xS = xs.size();
if (xS <= n) return xs;
Prim.Array_tabulate<T>(n, func (i : Nat) : T { xs[i]; });
Array_tabulate<T>(n, func (i : Nat) : T { xs[i]; });
};
};
6 changes: 3 additions & 3 deletions src/Copy.mo
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Nat "mo:base/Nat";
import { min } = "mo:base-0.7.3/Nat";

module {
public func copy<T>(dst : [var T], src : [T]) {
copyOffset<T>(dst, 0, src, 0);
};

public func copyOffset<T>(dst : [var T], startDst : Nat, src : [T], startSrc : Nat) {
let n = Nat.min(
let n = min(
dst.size() - startDst,
src.size() - startSrc,
);
Expand All @@ -22,7 +22,7 @@ module {
};

public func copyOffsetVar<T>(dst : [var T], startDst : Nat, src : [var T], startSrc : Nat) {
let n = Nat.min(
let n = min(
dst.size() - startDst,
src.size() - startSrc,
);
Expand Down
44 changes: 22 additions & 22 deletions test/Array.mo
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
import Nat "mo:base/Nat";
import { equal } = "mo:base-0.7.3/Nat";

import Array "../src/Array";
import { contains; drop; slice; split; take } = "../src/Array";

let xs : [Nat] = [1, 2, 3];
for (x in xs.vals()) {
assert(Array.contains<Nat>(xs, x, Nat.equal));
assert(not Array.contains<Nat>(xs, x + 3, Nat.equal));
assert(contains<Nat>(xs, x, equal));
assert(not contains<Nat>(xs, x + 3, equal));
};

assert(Array.drop<Nat>(xs, 0) == xs);
assert(Array.drop<Nat>(xs, 1) == [2, 3]);
assert(Array.drop<Nat>(xs, 2) == [3]);
assert(Array.drop<Nat>(xs, 3) == []);
assert(Array.drop<Nat>(xs, 9) == []);
assert(drop<Nat>(xs, 0) == xs);
assert(drop<Nat>(xs, 1) == [2, 3]);
assert(drop<Nat>(xs, 2) == [3]);
assert(drop<Nat>(xs, 3) == []);
assert(drop<Nat>(xs, 9) == []);

assert(Array.take<Nat>(xs, 0) == []);
assert(Array.take<Nat>(xs, 1) == [1]);
assert(Array.take<Nat>(xs, 2) == [1, 2]);
assert(Array.take<Nat>(xs, 3) == xs);
assert(Array.take<Nat>(xs, 9) == xs);
assert(take<Nat>(xs, 0) == []);
assert(take<Nat>(xs, 1) == [1]);
assert(take<Nat>(xs, 2) == [1, 2]);
assert(take<Nat>(xs, 3) == xs);
assert(take<Nat>(xs, 9) == xs);

assert(Array.split<Nat>(xs, 0) == (xs, []));
assert(Array.split<Nat>(xs, 1) == ([1], [2, 3]));
assert(Array.split<Nat>(xs, 2) == ([1, 2], [3]));
assert(Array.split<Nat>(xs, 3) == ([], xs));
assert(Array.split<Nat>(xs, 9) == ([], xs));
assert(split<Nat>(xs, 0) == (xs, []));
assert(split<Nat>(xs, 1) == ([1], [2, 3]));
assert(split<Nat>(xs, 2) == ([1, 2], [3]));
assert(split<Nat>(xs, 3) == ([], xs));
assert(split<Nat>(xs, 9) == ([], xs));

assert(Array.slice<Nat>(xs, 0, 0) == [1]);
assert(Array.slice<Nat>(xs, 1, 0) == []);
assert(Array.slice<Nat>(xs, 0, 3) == xs);
assert(slice<Nat>(xs, 0, 0) == [1]);
assert(slice<Nat>(xs, 1, 0) == []);
assert(slice<Nat>(xs, 0, 3) == xs);
25 changes: 11 additions & 14 deletions test/Copy.mo
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import Array "mo:base/Array";
import Iter "mo:base/Iter";
import { freeze; init } = "mo:base-0.7.3/Array";
import { range; toArray } = "mo:base-0.7.3/Iter";

import Debug "mo:base/Debug";
import { copy; copyOffset; copyOffsetVar } = "../src/Copy";

import Array_ "mo:base/Array";
import Copy "../src/Copy";
let n = init<Nat>(10, 0);
let m = toArray(range(0, 9));

let n = Array.init<Nat>(10, 0);
let m = Iter.toArray(Iter.range(0, 9));

Copy.copy(n, m);
assert(Array.freeze(n) == m);
copy(n, m);
assert(freeze(n) == m);

// copy(n[5:], m)
Copy.copyOffset(n, 5, m, 0);
assert(Array.freeze(n) == [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]);
copyOffset(n, 5, m, 0);
assert(freeze(n) == [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]);

// copy(n[6:], n)
Copy.copyOffsetVar(n, 6, n, 0);
assert(Array.freeze(n) == [0, 1, 2, 3, 4, 0, 0, 1, 2, 3]);
copyOffsetVar(n, 6, n, 0);
assert(freeze(n) == [0, 1, 2, 3, 4, 0, 0, 1, 2, 3]);
4 changes: 4 additions & 0 deletions vessel.dhall
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
dependencies = [ "base-0.7.3" ],
compiler = Some "0.7.3"
}

0 comments on commit 89bb25b

Please sign in to comment.