Last active
June 8, 2017 03:25
-
-
Save Woodsphreaker/272c234cf32c6f5adea4eb9e12dc9d50 to your computer and use it in GitHub Desktop.
Operations with matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const plus = (a = 0) => (b = 0) => a + b | |
const times = (a = 0) => (b = 0) => a * b | |
const minus = (a = 0) => (b = 0) => a - b | |
const obelus = (a = 1) => (b = 1) => a / b | |
const applyOperation = (fn) => (acc, cur) => acc.map((value, index) => fn(cur[index])(value)) | |
const matrix = (list) => (fn) => list.reduceRight(applyOperation(fn)) | |
const solve = (list) => (fn) => matrix(list)(fn) | |
const array = [ | |
[1,2,3,4,5,6], | |
[6,5,4,3,2,1], | |
[1,2,3,4,5,6], | |
[6,5,4,3,2,1], | |
[1,2,3,4,5,6], | |
[6,5,4,3,2,1], | |
] | |
const plusMatrix = solve(array)(plus) | |
const timesMatrix = solve(array)(times) | |
const minusMatrix = solve(array)(minus) | |
const obelusMatrix = solve(array)(obelus) | |
console.log(plusMatrix, // [ 21, 21, 21, 21, 21, 21 ] | |
timesMatrix, // [ 216, 1000, 1728, 1728, 1000, 216 ] | |
minusMatrix, // [ -15, -9, -3, 3, 9, 15 ] | |
obelusMatrix) // [ 0.004629629629629629, 0.064, 0.421875, 2.3703703703703702, 15.625, 216 ] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment