Forked from Woodsphreaker/operationsWithMatrixV2.js
Last active
March 26, 2018 02:11
-
-
Save suissa/acc5a1a0d700875b6ddb90c8551bfced to your computer and use it in GitHub Desktop.
Operations with Matrix V2
This file contains 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 = 1 ) => ( b = 1 ) => a * b | |
const minus = ( a = 0 ) => ( b = 0 ) => a - b | |
const division = ( a = 1 ) => ( b = 1 ) => a / b | |
const getNumByIndex = ( num, index ) => num[index] | |
const applyFn = ( fn ) => ( x, y ) => fn( x )( y) | |
const applyVerticalOperation = ( fn) => ( acc, cur) => | |
acc.map( ( value, index) => applyFn( fn)( cur[index], value)) | |
const applyHorizontalOrDiagonalOperation = ( fn) => ( list) => | |
list.reduce( applyFn( fn)) | |
const directions = [ | |
'Vertical', 'Horizontal', 'DiagonalLeft', 'DiagonalRigth' | |
] | |
const fns = { | |
Vertical: ( list) => ( fn ) => list.reduce( applyVerticalOperation( fn ) ), | |
Horizontal: ( list ) => ( fn ) => list.map( applyHorizontalOrDiagonalOperation( fn ) ), | |
DiagonalLeft: ( list ) => ( fn ) => applyHorizontalOrDiagonalOperation( fn ) | |
( list.map( getNumByIndex ) ), | |
DiagonalRigth: ( list ) => ( fn ) => applyHorizontalOrDiagonalOperation( fn ) | |
( list.reverse().map( getNumByIndex ) ) | |
} | |
const obj = directions.reduce( ( res, direction ) => | |
Object.assign( {}, res, { [`solve${direction}Matrix`]: fns[direction] }), {}) | |
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 solveThisVerticalMatrixUsing = obj.solveVerticalMatrix( array ) | |
const solveThisHorizontalMatrixUsing = obj.solveHorizontalMatrix( array ) | |
const solveThisiagonalLeftMatrixUsing = obj.solveDiagonalLeftMatrix( array ) | |
const solveThisiagonalRigthMatrixUsing = obj.solveDiagonalRigthMatrix( array ) | |
const plusVerticalMatrix = solveThisVerticalMatrixUsing( plus ) | |
const timesVerticalMatrix = solveThisVerticalMatrixUsing( times ) | |
const minusVerticalMatrix = solveThisVerticalMatrixUsing( minus ) | |
const divisionVerticalMatrix = solveThisVerticalMatrixUsing( division ) | |
const plusHorizontalMatrix = solveThisHorizontalMatrixUsing( plus ) | |
const timesHorizontalMatrix = solveThisHorizontalMatrixUsing( times ) | |
const minusHorizontalMatrix = solveThisHorizontalMatrixUsing( minus ) | |
const divisionHorizontalMatrix = solveThisHorizontalMatrixUsing( division ) | |
const plusDiagonalLeftMatrix = solveThisiagonalLeftMatrixUsing( plus ) | |
const timesDiagonalLeftMatrix = solveThisiagonalLeftMatrixUsing( times ) | |
const minusDiagonalLeftMatrix = solveThisiagonalLeftMatrixUsing( minus ) | |
const divisionDiagonalLeftMatrix = solveThisiagonalLeftMatrixUsing( division ) | |
const plusDiagonalRigthMatrix = solveThisiagonalRigthMatrixUsing( plus ) | |
const timesDiagonalRigthMatrix = solveThisiagonalRigthMatrixUsing( times ) | |
const minusDiagonalRigthMatrix = solveThisiagonalRigthMatrixUsing( minus ) | |
const divisionDiagonalRigthMatrix = solveThisiagonalRigthMatrixUsing( division ) | |
console.log( plusVerticalMatrix, // [ 21, 21, 21, 21, 21, 21 ] | |
timesVerticalMatrix, // [ 216, 1000, 1728, 1728, 1000, 216 ] | |
minusVerticalMatrix, // [ -15, -9, -3, 3, 9, 15 ] | |
divisionVerticalMatrix ) // [ 0.004629629629629629, 0.064, 0.421875, 2.3703703703703702, 15.625, 216 ] | |
console.log( plusHorizontalMatrix, // [ 21, 21, 21, 21, 21, 21 ] | |
timesHorizontalMatrix, // [ 720, 720, 720, 720, 720, 720 ] | |
minusHorizontalMatrix, // [ -19, -9, -19, -9, -19, -9 ] | |
divisionHorizontalMatrix ) // [ 0.001388888888888889, 0.049999999999999996, 0.001388888888888889, 0.049999999999999996, 0.001388888888888889, 0.049999999999999996 ] | |
console.log( plusDiagonalLeftMatrix, // 18 | |
timesDiagonalLeftMatrix, // 225 | |
minusDiagonalLeftMatrix, // -16 | |
divisionDiagonalLeftMatrix ) // 0.0044444444444444444 | |
console.log( plusDiagonalRigthMatrix, // 24 | |
timesDiagonalRigthMatrix, // 225 | |
minusDiagonalRigthMatrix, // -12 | |
divisionDiagonalRigthMatrix ) // 0.0044444444444444444 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment