Last active
September 15, 2017 00:15
-
-
Save lubien/1752405596a55fc8f07b6e3338aaf0e7 to your computer and use it in GitHub Desktop.
Playing with Ramdascript https://repl.it/LHTR/7
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 R = require('ramda') | |
const | |
divisibleBy = (n, x) => x % n === 0 | |
, odd = R.compose(R.not, R.partial(divisibleBy, [2])) | |
, fizzbuzz = x => | |
R.ifElse( | |
R.pipe(R.length, R.equals(0)) | |
, R.always(x) | |
, R.identity | |
)( | |
R.join('', | |
[ divisibleBy(3, x) ? 'Fizz': '' | |
, divisibleBy(5, x) ? 'Buzz': '' | |
] | |
) | |
) | |
, join2d = | |
R.compose( | |
R.join('\n') | |
, R.map(R.join('')) | |
) | |
, triangle = height => | |
join2d( | |
R.map( | |
R.repeat('#') | |
, R.range(1, height + 1) | |
) | |
) | |
, row = (n, oddChar, evenChar) => | |
R.map( | |
R.ifElse( | |
odd | |
, R.always(oddChar) | |
, R.always(evenChar) | |
) | |
, R.range(1, n + 1) | |
) | |
, grid = side => | |
join2d( | |
R.map( | |
R.ifElse( | |
odd | |
, R.partial(row, [side, '#', ' ']) | |
, R.partial(row, [side, ' ', '#']) | |
) | |
, R.range(1, side + 1) | |
) | |
) | |
console.log(grid(10)) | |
console.log() | |
console.log() | |
console.log(grid(9)) | |
console.log() | |
console.log() | |
console.log(triangle(10)) | |
console.log() | |
console.log() | |
console.log(triangle(9)) | |
console.log() | |
console.log() | |
console.log( | |
join2d( | |
R.range(1, 21) | |
.map(n => [n, ' => ', fizzbuzz(n)]) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment