Skip to content

Instantly share code, notes, and snippets.

@abuseofnotation
Created October 19, 2016 14:03
Show Gist options
  • Save abuseofnotation/a0bfc6a244c1ae47248dd50b976d638c to your computer and use it in GitHub Desktop.
Save abuseofnotation/a0bfc6a244c1ae47248dd50b976d638c to your computer and use it in GitHub Desktop.
//Converts a value to string
const valueAsString = (value) => value.toString()
//Returns true if 'num' is divided without remainder by 'divider'
const dividesBy = (divider) => (num) => num % divider === 0
//Prints 'i' only if it obeys the condition given by 'cond', using 'f' as a formatting function
const printIf = (cond, f) => (i) => cond(i) ? f(i) : ""
//Applies an array of functions to a value and concats the result
const applyOnAll = (functions, value) => functions.map((f)=> f(value))
//Applies several 'functions' to 'i' and concats the results.
const concat = (...functions) => (value) => applyOnAll(functions, value).join('')
//Applies several functions to a value and returns true if some of them return true
const not = (...functions) => (value) => !_.find(applyOnAll(functions, value))
//The solution itself. No need for description here.
console.log(_.range(1,100).map(concat(
printIf(dividesBy(3), _.constant("fizz")),
printIf(dividesBy(5), _.constant("buzz")),
printIf(not(dividesBy(5), dividesBy(3)), valueAsString)
)).join('\n'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment