Category talk:Wren-seq: Difference between revisions
→Source code: Changes to make this module more consistent with other Wren modules.
(→Source code: Lst.indicesOf should return a list of 3 arguments, not 2.) |
(→Source code: Changes to make this module more consistent with other Wren modules.) |
||
Line 48:
// Private helper method to check that 'a' is a list and throw an error otherwise.
static isList_(a) { (a is List) ? true : Fiber.abort("Argument must be a list.") }
// Private helper method to check whether a start index is valid.
static checkStart_(a, start) {
if (start.type != Num || !start.isInteger) Fiber.abort("Start must be an integer.")
if (start >= c || start < -c) Fiber.abort("Start is out of bounds.")
}
// Searches an unsorted list linearly for a particular value from a start index.
// If the start index is negative,
// Returns a list of three items:
// The first item is a Bool indicating whether the value was found.
Line 57 ⟶ 64:
static indicesOf(a, value, start) {
isList_(a)
var count = a.count
var indices = []
if (count == 0) return [false, 0, indices]
if (start
for (i in
if (a[i] == value) indices.add(i)▼
▲ }
} else {▼
if (start < 0) return [false, 0, indices]▼
▲ for (i in start..0) {
}▼
}
if (indices.isEmpty) return [false, 0, indices]
Line 80:
static indexOf(a, value, start) {
isList_(a)
return indexOf_(a, value, start)
}
// Private helper method for 'indexOf' which avoids type and bounds checks.
static indexOf_(a, value, start) {
var count = a.count
if (count == 0) return -1
if (start
for (i in
}▼
} else {▼
▲ for (i in start..0) {
}
return -1
}
// Returns the index of the last occurrence of 'value' in 'a' or -1 if no matches.
static lastIndexOf(a, value) {
for (i in a.count-1..0) {
// Works similarly to 'indexOf' but returns the index of the first match
// of ANY of a sequence of values or -1 if none of them matched.
static indexOfAny(a, values, start) {
var i
for (value in values) {
if ((i =
}
return -1
|