Skip to content

Commit 7ca5b2c

Browse files
Clean up: Array to buffer as Buffer.fromArray, (not Array.buffer). (#389)
* array to buffer. * experiment. * experiment concluded. * review comments. * remove old impl (again); git goof. * review comments.
1 parent d3b1081 commit 7ca5b2c

File tree

4 files changed

+36
-12
lines changed

4 files changed

+36
-12
lines changed

src/Array.mo

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,4 @@ module {
297297
});
298298
};
299299

300-
public func buffer<A>(xs : [A]) : Buffer.Buffer<A> {
301-
let buff = Buffer.Buffer<A>(xs.size());
302-
for (x in xs.vals()) {
303-
buff.add(x)
304-
};
305-
buff
306-
};
307300
}

src/Buffer.mo

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,21 @@ module {
146146
};
147147
};
148148

149+
/// Creates a buffer from immutable array elements.
150+
public func fromArray<X>(elems : [X]) : Buffer<X> {
151+
let buff = Buffer<X>(elems.size());
152+
for (elem in elems.vals()) {
153+
buff.add(elem)
154+
};
155+
buff
156+
};
157+
158+
/// Creates a buffer from the elements of a mutable array.
159+
public func fromVarArray<X>(elems : [var X]) : Buffer<X> {
160+
let buff = Buffer<X>(elems.size());
161+
for (elem in elems.vals()) {
162+
buff.add(elem)
163+
};
164+
buff
165+
};
149166
}

test/arrayTest.mo

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -216,11 +216,6 @@ let suite = Suite.suite("Array", [
216216
"reverse",
217217
Array.reverse<Nat>([0, 1, 2, 3]),
218218
M.equals(T.array<Nat>(T.natTestable, [3, 2, 1, 0]))
219-
),
220-
Suite.test(
221-
"buffer",
222-
Array.buffer<Nat>([0, 1, 2, 3]).toArray(),
223-
M.equals(T.array<Nat>(T.natTestable, [0, 1, 2, 3]))
224219
)
225220
]);
226221

test/bufTest.mo

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ import B "mo:base/Buffer";
33
import I "mo:base/Iter";
44
import O "mo:base/Option";
55

6+
import Suite "mo:matchers/Suite";
7+
import T "mo:matchers/Testable";
8+
import M "mo:matchers/Matchers";
9+
610
// test repeated growing
711
let a = B.Buffer<Nat>(3);
812
for (i in I.range(0, 123)) {
@@ -92,3 +96,18 @@ do {
9296
assert (c.toArray().size() == 2);
9397
assert (c.toVarArray().size() == 2);
9498
};
99+
100+
let {run;test;suite} = Suite;
101+
run(suite("array",
102+
[
103+
test(
104+
"fromArray",
105+
B.fromArray<Nat>([0, 1, 2, 3]).toArray(),
106+
M.equals(T.array<Nat>(T.natTestable, [0, 1, 2, 3]))
107+
),
108+
test(
109+
"fromVarArray",
110+
B.fromVarArray<Nat>([var 0, 1, 2, 3]).toArray(),
111+
M.equals(T.array<Nat>(T.natTestable, [0, 1, 2, 3]))
112+
)
113+
]));

0 commit comments

Comments
 (0)