Skip to content

Instantly share code, notes, and snippets.

@mathieueveillard
Last active October 19, 2021 15:19
Show Gist options
  • Save mathieueveillard/5b2a8c44bd984835d235d5024c6a5769 to your computer and use it in GitHub Desktop.
Save mathieueveillard/5b2a8c44bd984835d235d5024c6a5769 to your computer and use it in GitHub Desktop.
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