Category talk:Wren-seq: Difference between revisions
Content added Content deleted
(→Source code: Added methods to create 2d and 3d lists and to create lists of mutable types.) |
(→Source code: Added more methods to assist with initialization.) |
||
Line 117: | Line 117: | ||
static filled2(rows, cols, filler) { filled2(rows, cols, filler, null) } |
static filled2(rows, cols, filler) { filled2(rows, cols, filler, null) } |
||
static filled3(pages, rows, cols, filler) { filled3(pages, rows, cols, filler, null) } |
static filled3(pages, rows, cols, filler) { filled3(pages, rows, cols, filler, null) } |
||
// The following analogous methods refill existing one, two or three dimensional lists with 'filler' |
|||
// or, if appropriate, a copy thereof. |
|||
static refill(a, filler, copier) { |
|||
Lst.isList_(a) |
|||
for (c in 0...a.count) a[c] = !copier ? filler : copier.call(filer) |
|||
} |
|||
static refill2(a, filler, copier) { |
|||
Lst.isList_(a) |
|||
for (r in 0...a.count) { |
|||
for (c in 0...a[0].count) a[r][c] = !copier ? filler : copier.call(filler) |
|||
} |
|||
} |
|||
static refill3(a, filler, copier) { |
|||
isList_(a) |
|||
for (p in 0...a.count) { |
|||
for (r in 0...a[0].count) { |
|||
for (c in 0...a[0][0].count) a[p][r][c] = !copier ? filler : copier.call(filler) |
|||
} |
|||
} |
|||
} |
|||
static refill (a, filler) { refill (a, filler, null) } |
|||
static refill2(a, filler) { refill2(a, filler, null) } |
|||
static refill3(a, filler) { refill3(a, filler, null) } |
|||
// Creates a list and fills it with a series of numbers starting from 'start' |
|||
// with a common difference of 'step'. |
|||
static serialFill(cols, start, step) { |
|||
if (cols.type != Num || !cols.isInteger || cols < 0) { |
|||
Fiber.abort("'cols' must be a non-negative integer.") |
|||
} |
|||
if (start.type != Num) Fiber.abort("'start' must be a number.") |
|||
if (step.type != Num) Fiber.abort("'step' must be a number.") |
|||
var lst = List.filled(cols, 0) |
|||
for (i in 0...cols) lst[i] = start + i*step |
|||
return lst |
|||
} |
|||
// Analogous method to serialFill which refills an existing list with a series of numbers. |
|||
static serialRefill(a, start, step) { |
|||
isList_(a) |
|||
if (start.type != Num) Fiber.abort("'start' 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 |
|||
} |
|||
// Overloads of the above methods |
|||
static serialFill(cols, start) { serialFill(cols, start, 1) } // step 1 |
|||
static serialFill(cols) { serialFill(cols, 0, 1) } // start 0, step 1 |
|||
static serialRefill(a, start) { serialRefill(a, start, 1) } // step 1 |
|||
static serialRefill(a) { serialRefill(a, 0, 1) } // start 0, step 1 |
|||
// Private helper method to check that 'a' is a list and throw an error otherwise. |
// Private helper method to check that 'a' is a list and throw an error otherwise. |