Skip to content

Instantly share code, notes, and snippets.

@ktilcu
Last active August 15, 2017 20:40
Show Gist options
  • Save ktilcu/b01d4102e7134fbb110c55789795bec6 to your computer and use it in GitHub Desktop.
Save ktilcu/b01d4102e7134fbb110c55789795bec6 to your computer and use it in GitHub Desktop.
kyle-Refactor: Point free with Ramda goodness

The initial function seemed straightforward and very close to functional programming but there was a bit of buried complexity.

function generate(seedWords) {
  const filteredWords = filterInputs(seedWords);
  const initialHash = R.head(filteredWords) || 'thunder';
  const hashWithRandom = appendRandom(initialHash);
  return generateHelper(hashWithRandom, R.tail(filteredWords));
}

The rewrite is point-free and relies on ramda a lot. It isn't highly readable but I think thats because of familiarity with the library and with point free style. The code isn't difficult it's just a bit dense.

const generate = R.pipe(
  filterInputs,
  R.converge(
    generateHelper,
    [
      R.pipe(
        R.ifElse(
          R.isEmpty,
          R.head,
          R.always('thunder')
        ),
        appendRandom
      ),
      R.identity
    ]
  )
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment