Skip to content

Instantly share code, notes, and snippets.

@tommmyy
Last active July 22, 2017 22:15
Show Gist options
  • Save tommmyy/d83e3a193188b4c90a22a4d611bac654 to your computer and use it in GitHub Desktop.
Save tommmyy/d83e3a193188b4c90a22a4d611bac654 to your computer and use it in GitHub Desktop.
Count even digits in a number.
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