import {
map,
filter,
...
} from 'musejs';
Higher order map
map([1, 2, 3, 4], i => i + 1)
// => [2, 3, 4, 5]
Higher order filter
filter([1, 'hey', '3', 4], !isNaN);
// => [1, 3, 4]
Higher order reduce
reduce([1, 2, 3, 4], (i, j) => i + j);
// => 10
Higher order forEach
forEach([1, 2, 3, 4], i => console.log (i))
// => 1
// => 2
// => 3
// => 4
Higher order reverse
reverse([1, 2, 3])
// => [3, 2, 1]
Return true if the value is not 0, undefined or null
filter([1, null, 4, undefined, 'Hello'], id)
// => [1, 4, 'Hello']
Negates a function
let isNumber = complement(isNaN);
isNumber(1)
// => true
Yields a range from min
to max
by step
[...range(0, 10)]
// => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Performs an action n times
times(3, () => console.log ('Hello'))
// => Hello
// => Hello
// => Hello
Flatten an array
flatten([1, 2, 3, [4, [5, [6, [7, [8, 9]]]]]])
// => [1, 2, 3, 4, 5, 6, 7, 8, 9]
Take a function fn and an array of args with empty spaces
and returns a partial function g
that take a single arg
let rgb = (r, g, b) => [r, g, b];
let varyGreen = partial(rgb, 4, null, 5);
varyGreen(4)
// => rgb(4, 4, 5);
Pass a value to a list of function, one function at a time
thread(
1,
i => i + 1,
i => i - 2
)
// => -1
Return a function that takes a value to be threaded in a list of functions
add3AndMultiply3 = compose(add3, multiply3);
add3AndMultiply3(3);
// => 18
Transform a callback style function into a Promise style function
let readfile = promisify(fs.readFile)
let text = readfile('./test.txt')
text.then(console.log.bind(console))
Primitives type checking functions
isNumber('3')
// => true
isString('Hello')
// => true
isArray({name: guillaume})
// => false
Dot product between 2 vectors
dotProduct([1, 2, 3], [4, 5, 6]);
// => 32
Matrix/vector addition
addMatrix(
[[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1]]
)
// => [[2, 2, 2, 2], [2, 2, 2, 2]]
Returns a matrix row as a vector
getRow(
[[1, 1, 1, 1, 1],
[2, 2, 2, 2, 2],
[1, 1, 1, 1, 1],
[1, 1, 1, 1, 1]],
1
)
// => [2, 2, 2, 2, 2]
Returns a matrix column as a vector
getColumn(
[[2, 1, 1, 1, 1],
[2, 1, 1, 1, 1],
[2, 1, 1, 1, 1],
[2, 1, 1, 1, 1]],
0
)
// => [2, 2, 2, 2, 2]
Performs a matricex multiplication
const m1 = [
[1, 2, 3],
[4, 5, 6]
];
const m2 = [
[7, 8],
[9 , 10],
[11, 12]
]
multMatrix(m1, m2)
// => [ [58, 64],
// [139, 154]]
Returns a matrix dimensions [rows, columns]
const [rows, columns] = matrixDimensions(myMatrix);
Transpose a matrix
console.log (transpose([
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]))
// => [ [ 1, 4, 7 ],
// [ 2, 5, 8 ],
// [ 3, 6, 9 ] ]