Last active
January 9, 2019 16:42
-
-
Save pete-murphy/27c1a35ce54756f6752c48ee639ed661 to your computer and use it in GitHub Desktop.
Reversing a string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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