-
-
Save lubien/2c7937ccb3c89b3017e5510ca83d7de2 to your computer and use it in GitHub Desktop.
Basic Operators Challenge
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 | |
composeN = (...fs) => y => | |
fs.reduceRight((x, f) => f(x), y) | |
, concat = ys => xs => | |
xs.concat(ys) | |
, flatten = xs => | |
reduce(uncurry2(concat))(xs) | |
, reduce = f => xs => | |
xs.reduce(f) | |
, map = f => xs => | |
xs.map(f) | |
, zipWith = f => xs => ys => | |
xs.map((x, i) => f(x)(ys[i])) | |
, caseIs = o => k => | |
o[k] | |
, sum = y => x => | |
x + y | |
, sub = y => x => | |
x - y | |
, mul = y => x => | |
x * y | |
, div = y => x => | |
x / y | |
, uncurry2 = f => (x, y) => | |
f(x)(y) | |
, mapNumToFn = | |
caseIs({ | |
2: mul(2), | |
4: div(4), | |
6: sub(6), | |
8: sum(8) | |
}) | |
, M = | |
[ [ 2, 4, 6, 8] | |
, [12, 14, 16, 18] | |
, [20, 24, 28, 32] | |
, [32, 34, 36, 38] | |
, [42, 44, 46, 48] | |
] | |
, solve = M => { | |
let | |
[xs, ...ys] = M | |
, fs = map(mapNumToFn)(xs) | |
//in | |
return composeN( | |
reduce(uncurry2(sum)), | |
concat(xs), | |
flatten, | |
zipWith(map)(fs) | |
)(ys) | |
} | |
solve(M) |
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
Desafio dos operadores básicos | |
Dado o Array matrix, utilizar a primeira posição como indice dos operadores basicos | |
[2,4,6,8] | |
[2] deve multiplicar [12,14,16,18] | |
[4] deve dividir [20,24,28,32] | |
[6] deve subtrair [32,34,36,38] | |
[8] deve somar [42,44,46,48] | |
Quando todos os calculos forem feitos, os novos valores do array devem ser somados juntamente com o [2,4,6,8] | |
O resultado final do cálculo é 494 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Muito legal !!!!
Show