Array concat => map rolled in a mat.
Install
npm install array-mat
Synopsis
mat
takes an array A₀
, a transform function F(A₀) => A₁
and returns a new array A₀ + A₁
:
A.mat(F) A.concat(A.map(F))
Given an array A₀
and N
number of transforms A₀ + F(A₀) + ... + Fₙ(Fₙ₋₁(A))
A
.mat(F[0])
...
.mat(F[F.length - 1])
A.concat(A.map(F[0]), ..., .map(F[F.length - 1]))
Examples
["A", "B", "C"].mat((k)=>k.toLowerCase())
[].concat.apply([], ["A", "B"]
.mat((file) => file.toLowerCase())
.mat((file) => [file + ".js", file + ".es"]))
Extending the Prototype
Quoting @raynos:
Extending natives is a decision a library user should make, not a decision a library author should make.
You can extend the Array prototype like so:
Array.prototype.mat = Array.prototype.mat || require("array-mat")
console.log(
[0]
.mat((k) => k + 1 )
.mat((k) => k + 2 )
.mat((k) => k + 4 )
)
Or use as a standalone function. The return value of the first call to mat
, will be an array with a non-enmurable mat
method that you can use to create a chain of concat-transforms.
let mat = require("array-mat")
console.log(
mat([0], (k) => k + 1)
.mat((k) => k + 2)
.mat((k) => k + 4)
)
Use node's --harmony
flag for arrow function support.
» node --harmony example.js
[ 0, 1, 2, 3, 4, 5, 6, 7 ]
It's up to you. You can learn more about extending JavaScript natives here.
License
MIT © Jorge Bucaran