Last active
October 19, 2021 15:19
-
-
Save mathieueveillard/5b2a8c44bd984835d235d5024c6a5769 to your computer and use it in GitHub Desktop.
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 IdentityFunctor = <S>(value: S) => { | |
return { | |
map: <U>(fn: (s: S) => U) => IdentityFunctor(fn(value)), | |
valueOf: () => value, | |
}; | |
}; | |
const appendIfMultipleOf = (stringToAppend: string) => (m: number) => (n: number) => (s: string): string => { | |
if (n % m === 0) { | |
return (s += stringToAppend); | |
} | |
return s; | |
}; | |
const returnNOtherwise = (n: number) => (s: string): string => s || n.toString(); | |
export function internal(n: number): string { | |
return IdentityFunctor("") | |
.map(appendIfMultipleOf("Fizz")(3)(n)) | |
.map(appendIfMultipleOf("Buzz")(5)(n)) | |
.map(returnNOtherwise(n)) | |
.valueOf(); | |
} | |
export function FizzBuzz(n: number): string[] { | |
return [...Array(n)].map((_, i) => i + 1).map(internal); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment