Skip to content

Instantly share code, notes, and snippets.

@pete-murphy
Last active January 9, 2019 16:42
Show Gist options
  • Save pete-murphy/27c1a35ce54756f6752c48ee639ed661 to your computer and use it in GitHub Desktop.
Save pete-murphy/27c1a35ce54756f6752c48ee639ed661 to your computer and use it in GitHub Desktop.
Reversing a string
const Iso = (to, from) => ({ to, from })
const string = Iso(cs => cs.join(""), str => [...str])
const reverse = xs => xs.reverse()
const pipe = (...fns) => x => fns.reduce((acc, fn) => fn(acc), x)
// Reverses a string
const reverseString = pipe(
string.from,
reverse,
string.to
)
// Tests
const assert = require("assert")
const runTests = f => cases => {
cases.forEach(([input, output]) =>
assert(f(input) === output, `Expected: ${output}, Actual: ${f(input)}`)
)
console.log("All tests passed!")
}
const cases = [
["hello world", "dlrow olleh"],
["asdf", "fdsa"],
["CS rocks!", "!skcor SC"]
]
runTests(reverseString)(cases)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment