Category talk:Wren-seq: Difference between revisions
Content added Content deleted
(→Source code: Added insertAll, clearPart and truncate methods to Lst lass.) |
(→Source code: Added Lst.lshift/rshift methods and made some existing methods 'chaining' friendly.) |
||
Line 119: | Line 119: | ||
// The following analogous methods refill existing one, two or three dimensional lists with 'filler' |
// The following analogous methods refill existing one, two or three dimensional lists with 'filler' |
||
// or, if appropriate, a copy thereof. |
// or, if appropriate, a copy thereof and return the list. |
||
static refill(a, filler, copier) { |
static refill(a, filler, copier) { |
||
Lst.isList_(a) |
Lst.isList_(a) |
||
for (c in 0...a.count) a[c] = !copier ? filler : copier.call(filer) |
for (c in 0...a.count) a[c] = !copier ? filler : copier.call(filer) |
||
return a |
|||
} |
} |
||
Line 130: | Line 131: | ||
for (c in 0...a[0].count) a[r][c] = !copier ? filler : copier.call(filler) |
for (c in 0...a[0].count) a[r][c] = !copier ? filler : copier.call(filler) |
||
} |
} |
||
return a |
|||
} |
} |
||
Line 139: | Line 141: | ||
} |
} |
||
} |
} |
||
return a |
|||
} |
} |
||
Line 158: | Line 161: | ||
} |
} |
||
// Analogous method to serialFill which refills an existing list with a series of numbers. |
// Analogous method to serialFill which refills an existing list with a series of numbers and returns it. |
||
static serialRefill(a, start, step) { |
static serialRefill(a, start, step) { |
||
isList_(a) |
isList_(a) |
||
Line 164: | Line 167: | ||
if (step.type != Num) Fiber.abort("'step' must be a number.") |
if (step.type != Num) Fiber.abort("'step' must be a number.") |
||
for (i in 0...a.count) a[i] = start + i*step |
for (i in 0...a.count) a[i] = start + i*step |
||
return a |
|||
} |
} |
||
Line 194: | Line 198: | ||
return other |
return other |
||
} |
} |
||
// Performs a circular shift of the elements of 'a' 'n' places to the left. |
|||
// If 'n' is negative performs a circular right shift by '-n' places instead. |
|||
// Returns 'a' after any mutation. |
|||
static lshift(a, n) { |
|||
Lst.isList_(a) |
|||
if (!(n is Num) || !n.isInteger) Fiber.abort("'n' must be an integer.") |
|||
var count = a.count |
|||
if (count < 2) return a |
|||
if (n < 0) return rshift(a, -n) |
|||
n = n % count |
|||
if (n == 0) return a |
|||
for (i in 1..n) { |
|||
var t = a[0] |
|||
for (j in 0..count-2) a[j] = a[j+1] |
|||
a[-1] = t |
|||
⚫ | |||
return a |
|||
} |
|||
// Performs a circular shift of the elements of 'a' 'n' places to the right. |
|||
// If 'n' is negative performs a circular left shift by '-n' places instead. |
|||
// Returns 'a' after any mutation. |
|||
static rshift(a, n) { |
|||
Lst.isList_(a) |
|||
if (!(n is Num) || !n.isInteger) Fiber.abort("'n' must be an integer.") |
|||
var count = a.count |
|||
if (count < 2) return a |
|||
if (n < 0) return lshift(a, -n) |
|||
n = n % count |
|||
if (n == 0) return a |
|||
for (i in 1..n) { |
|||
var t = a[-1] |
|||
for (j in count-2..0) a[j+1] = a[j] |
|||
a[0] = t |
|||
} |
|||
return a |
|||
} |
|||
// Convenience versions of the above methods which shift by just 1 place. |
|||
static lshift(a) { lshift(a, 1) } |
|||
static rshift(a) { rshift(a, 1) } |
|||
// Searches an unsorted list linearly for a particular value from a start index. |
// Searches an unsorted list linearly for a particular value from a start index. |
||
Line 411: | Line 457: | ||
} |
} |
||
// Removes consecutive repeated elements from a list (not a copy). |
// Removes consecutive repeated elements from a list (not a copy) and returns it. |
||
// If the list is sorted, it removes all duplicates. |
// If the list is sorted, it removes all duplicates. |
||
static prune(a) { |
static prune(a) { |
||
isList_(a) |
isList_(a) |
||
var c = a.count |
var c = a.count |
||
if (c < 2) return |
if (c < 2) return a |
||
for (i in c-1..1) { |
for (i in c-1..1) { |
||
if (a[i-1] == a[i]) a.removeAt(i) |
if (a[i-1] == a[i]) a.removeAt(i) |
||
} |
} |
||
return a |
|||
} |
} |
||
Line 472: | Line 519: | ||
} |
} |
||
// Removes all elements of 'a' between indices 'start' and 'end' inclusive |
// Removes all elements of 'a' between indices 'start' and 'end' inclusive and returns it. |
||
static clearPart(a, start, end) { |
static clearPart(a, start, end) { |
||
isList_(a) |
isList_(a) |
||
if (a.isEmpty) return |
if (a.isEmpty) return a |
||
checkStart_(a, start) |
checkStart_(a, start) |
||
checkStart_(a, end) |
checkStart_(a, end) |
||
Line 482: | Line 529: | ||
if (end < start) Fiber.abort("'end' cannot be less than 'start'.") |
if (end < start) Fiber.abort("'end' cannot be less than 'start'.") |
||
for (i in end..start) a.removeAt(i) |
for (i in end..start) a.removeAt(i) |
||
return a |
|||
} |
} |
||
// Removes all elements of 'a' from index 'start' to the end. |
// Removes all elements of 'a' from index 'start' to the end and returns it. |
||
static truncate(a, start) { |
static truncate(a, start) { clearPart(a, start, -1) } |
||
clearPart(a, start, -1) |
|||
⚫ | |||
// Return a map whose keys are 'k' and corresponding values 'v' |
// Return a map whose keys are 'k' and corresponding values 'v' |