Abstract
JavaScript doesn't have a canonical function to create an array. This is a pity, because it discourages developers from adopting a functional style of programming. I propose that we add an Array.create
function to the language. This will lead to cleaner code and the building of better abstractions.
Background
Traditionally, creation of arrays in JavaScript was done using procedural code. Then, with ES6 we got the Array.from
function which made it easier to create arrays in a functional style. However, the canonical purpose of Array.from
is converting iterables into arrays. Therefore, using it to create arrays is wonky.
// Array.create :: (Int, Int -> a) -> Array a
Array.create = (length, mapping) =>
Array.from({ length }, (value, index) => mapping(index));
It would be nicer to have a canonical function to create arrays, such as the Array.create
function shown above. Due to a lack of such a canonical function, we currently have packages such as create-array
in the NPM ecosystem. This leads to package bloat and possible code duplication as developers reinvent the wheel in every new project.
Motivation
Another reason to include the Array.create
function would be to encourage developers to build better abstractions. For example, we can use Array.create
to create and transpose matrices easily.
class Matrix {
// new Matrix :: (Int, Int, Array (Array a)) -> Matrix a
constructor(rows, cols, data) {
this.rows = rows;
this.cols = cols;
this.data = data;
}
// Matrix.create :: (Int, Int, (Int, Int) -> a) -> Matrix a
static create(rows, cols, mapping) {
const data = Array.create(rows, row =>
Array.create(cols, col => mapping(row, col)));
return new Matrix(rows, cols, data);
}
// Matrix#transpose :: Matrix a ~> () -> Matrix a
transpose() {
const { rows, cols, data } = this;
return Matrix.create(cols, rows, (row, col) => data[col][row]);
}
}
Conclusion
The Array.create
function is very useful. It encourages a functional style of programming, we can use it to build better abstractions, and it would prevent developers from reinventing the wheel in every project.