Last active
July 22, 2017 22:15
-
-
Save tommmyy/d83e3a193188b4c90a22a4d611bac654 to your computer and use it in GitHub Desktop.
Count even digits in a number.
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
import { modulo, o, not, curry, compose, length, filter, split, map } from 'ramda'; | |
// Number -> Number | |
const isOdd = o(Boolean, modulo(2)); | |
// Number -> Boolean | |
const isEven = o(not, isOdd); | |
// (a -> Boolean) -> [a] -> Number | |
const countIf = curry(compose(length, filter)); | |
// Number -> [String] | |
const numberToChars = o(split(''), String); | |
// Number -> [Number] | |
const numberToDigits = o(map(Number), numberToChars); | |
// Number -> Number | |
const countEvenDigits = o(countIf(isEven), numberToDigits); | |
export default countEventDigits; | |
// countEvenDigits(2246221) => 6; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment