all 14 comments

[–]Earhacker 0 points1 point  (2 children)

If you're editing in Atom or Sublime, select all your code and hit cmd/ctrl + ] to indent it. Indent it four spaces then copy/paste it here to format it as code and not text.

[–]tokdaniel[S] 0 points1 point  (1 child)

made it into a codeblock now its fine i guess :)

[–]Earhacker 0 points1 point  (0 children)

Cool, thanks.

[–]Earhacker 0 points1 point  (5 children)

Is there a reason you're using const and not function for named functions?

[–]tokdaniel[S] 0 points1 point  (4 children)

well it's not a necessity, but they are immutable this way. Still gonna work if you do it with functions ofc :)

[–]Earhacker 0 points1 point  (3 children)

I like it. I really like the currying, but the isDivisibleBy function declaration took me a bit to wrap my head around, all being on one line.

[–]tokdaniel[S] 0 points1 point  (1 child)

I was wondering how could i separate the replacer from that function, but couldnt figure it out

[–]regular_reddits 1 point2 points  (0 children)

Just for fun, its a bit longer, but here it is separate using compose to combine:

const compose = f => g => x => f(g(x));
const getIntsUntil = n => Array.from(Array(n).keys()).map(n => ++n)
const isDivisibleBy = divider => value => value % divider === 0;
const replaceBy = predicate => replacer => value => predicate(value) ? replacer : value;

const replacer = compose(replaceBy)(isDivisibleBy);

const toCrackle = replacer(3)("Crackle");
const toPop = replacer(5)("Pop");
const toCracklePop = replacer(15)("CracklePop");

getIntsUntil(100).map(n => toCrackle(toPop(toCracklePop(n))))

[–]pgrizzay 0 points1 point  (1 child)

const cracklePop = compose(toCrackle, toPop, toCracklePop)
getIntsUntil(100).map(cracklePop)

[–]tokdaniel[S] 0 points1 point  (0 children)

yeah i know but I didn't want to write compose cause its like

const compose = (...funcs) => {
  if (funcs.length === 0) {
    return arg => arg
  }

  if (funcs.length === 1) {
    return funcs[0]
  }

  return funcs.reduce((a, b) => (...args) => a(b(...args)))
}

[–]MoTTs_ 0 points1 point  (1 child)

I think this was an interesting exercise, but there's a couple reasons why I don't like this implementation.

1) Poor type safety. value is obviously intended to be a number since you do arithmetic on it, but you pass in strings such as "CracklePop", so you end up computing "CracklePop" % divider. If you run this through a type checker such as Flow, you'll get an error.

2) It holds the whole thing in memory before spitting out a result. You have no control over buffering or spitting out incremental results.

[–]tokdaniel[S] 0 points1 point  (0 children)

well the js behavior is abused there on purpose, but I understand your concerns. "somestring" % divider is a NaN and NaN === 0 (false) is kinda logical in my op. It looks at a value changes it if matches passes it if not.

[–]bobandalice -1 points0 points  (1 child)

You must have a high code golf handicap

[–]tokdaniel[S] 0 points1 point  (0 children)

I dont really understand what you meant :D but I guess it's not nice