Nasal: std.Vector: add 'contains' method (by onox).
This commit is contained in:
parent
5e2a37be56
commit
d930e3f2fa
2 changed files with 13 additions and 0 deletions
|
@ -120,6 +120,15 @@ var Vector = {
|
||||||
die("ValueError: item not in the vector");
|
die("ValueError: item not in the vector");
|
||||||
},
|
},
|
||||||
|
|
||||||
|
contains: func (item) {
|
||||||
|
# Return true if the vector contains the item, false otherwise
|
||||||
|
|
||||||
|
var err = [];
|
||||||
|
call(Vector.index, [item], me, err);
|
||||||
|
|
||||||
|
return size(err) == 0;
|
||||||
|
},
|
||||||
|
|
||||||
remove: func (item) {
|
remove: func (item) {
|
||||||
# Remove the first occurrence of the given item. Raises a
|
# Remove the first occurrence of the given item. Raises a
|
||||||
# ValueError if the item is not present in the vector.
|
# ValueError if the item is not present in the vector.
|
||||||
|
|
|
@ -108,6 +108,10 @@ REQUIRE_EQUAL(x.size(), 4);
|
||||||
# index():
|
# index():
|
||||||
REQUIRE_EQUAL(x.index("c"), 2);
|
REQUIRE_EQUAL(x.index("c"), 2);
|
||||||
|
|
||||||
|
# contains():
|
||||||
|
REQUIRE_EQUAL(x.contains("c"), 1);
|
||||||
|
REQUIRE_EQUAL(x.contains("e"), 0);
|
||||||
|
|
||||||
# remove():
|
# remove():
|
||||||
x.remove("c");
|
x.remove("c");
|
||||||
REQUIRE_EQUAL(x.vector, ["a", "b", "d"]);
|
REQUIRE_EQUAL(x.vector, ["a", "b", "d"]);
|
||||||
|
|
Loading…
Reference in a new issue