Category talk:Wren-seq: Difference between revisions
Content added Content deleted
(→Source code: Numerous changes to View class.) |
(→Source code: Added methods to create 2d and 3d lists and to create lists of mutable types.) |
||
Line 46: | Line 46: | ||
/* Lst supplements the List class with various other operations on lists. */ |
/* Lst supplements the List class with various other operations on lists. */ |
||
class Lst { |
class Lst { |
||
// Creates a list with 'cols' columns and assigns a copy of 'filler' to each element. |
|||
// 'copier' is a function which takes a single argument and returns a copy of that argument. |
|||
static filled(cols, filler, copier) { |
|||
if (cols.type != Num || !cols.isInteger || cols < 0) { |
|||
Fiber.abort("'cols' must be a non-negative integer.") |
|||
} |
|||
var lst |
|||
if (!copier) { |
|||
lst = List.filled(cols, filler) |
|||
} else { |
|||
lst = List.filled(cols, null) |
|||
for (c in 0...cols) lst[c] = copier.call(filler) |
|||
} |
|||
return lst |
|||
} |
|||
// Creates a two dimensional list with 'rows' rows and 'cols' columns and assigns a copy |
|||
// of 'filler' to each element. |
|||
// 'copier' is a function which takes a single argument and returns a copy of that argument. |
|||
static filled2(rows, cols, filler, copier) { |
|||
if (rows.type != Num || !rows.isInteger || rows < 0) { |
|||
Fiber.abort("'rows' must be a non-negative integer.") |
|||
} |
|||
if (cols.type != Num || !cols.isInteger || cols < 0) { |
|||
Fiber.abort("'cols' must be a non-negative integer.") |
|||
} |
|||
var lst = List.filled(rows, null) |
|||
for (r in 0...rows) { |
|||
if (!copier) { |
|||
lst[r] = List.filled(cols, filler) |
|||
} else { |
|||
lst[r] = List.filled(cols, null) |
|||
for (c in 0...cols) lst[r][c] = copier.call(filler) |
|||
} |
|||
} |
|||
return lst |
|||
} |
|||
// Creates a three dimensional list with 'pages' pages, 'rows' rows and 'cols' columns and assigns |
|||
// a copy of 'filler' to each element. |
|||
// 'copier' is a function which takes a single argument and returns a copy of that argument. |
|||
static filled3(pages, rows, cols, filler, copier) { |
|||
if (pages.type != Num || !pages.isInteger || pages < 0) { |
|||
Fiber.abort("'pages' must be a non-negative integer.") |
|||
} |
|||
if (rows.type != Num || !rows.isInteger || rows < 0) { |
|||
Fiber.abort("'rows' must be a non-negative integer.") |
|||
} |
|||
if (cols.type != Num || !cols.isInteger || cols < 0) { |
|||
Fiber.abort("'cols' must be a non-negative integer.") |
|||
} |
|||
var lst = List.filled(pages, null) |
|||
for (p in 0...pages) { |
|||
lst[p] = List.filled(rows, null) |
|||
for (r in 0...rows) { |
|||
if (!copier) { |
|||
lst[p][r] = List.filled(cols, filler) |
|||
} else { |
|||
lst[p][r] = List.filled(cols, null) |
|||
for (c in 0...cols) lst[p][r][c] = copier.call(filler) |
|||
} |
|||
} |
|||
} |
|||
return lst |
|||
} |
|||
// The following overloads of the above methods should normally only be used where the 'filler' |
|||
// is of an immutable type and does not need to be copied. |
|||
// No overload is required for 'filled' as List.filled suffices for such types. |
|||
static filled2(rows, cols, filler) { filled2(rows, cols, filler, null) } |
|||
static filled3(pages, rows, cols, filler) { filled3(pages, rows, cols, filler, null) } |
|||
// 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. |
||
static isList_(a) { (a is List) ? true : Fiber.abort("Argument must be a list.") } |
static isList_(a) { (a is List) ? true : Fiber.abort("Argument must be a list.") } |