Skip to content

Instantly share code, notes, and snippets.

@TrevorBasinger
Last active August 29, 2015 14:08
Show Gist options
  • Save TrevorBasinger/4710bfb22e7c5988d32c to your computer and use it in GitHub Desktop.
Save TrevorBasinger/4710bfb22e7c5988d32c to your computer and use it in GitHub Desktop.
var R = require ('ramda'),
split = R.split,
join = R.join,
zip = R.zip,
zipWith = R.zipWith,
head = R.head,
last = R.last,
map = R.map,
compose = R.compose,
curry = R.curry,
apply = R.apply,
ap = R.ap,
sortBy = R.sortBy,
log = console.log,
msgs = [
"programs Manchester The written ran Mark 1952 1 in Autocode from;6 2 1 7 5 3 11 4 8 9",
"programming to a code machine language automatically used it language, compiler the A convert into;2 8 6 15 14 12 9 5 4 3 7 11 1 10"
],
// Pure Code
// separateEncoding :: String -> [[String], [String]]
separateEncoding = compose (map (split (' ')), split (';')),
// convertAndSort :: [[String], [String]] -> [[String], [Integer]]
convertAndSort = sortBy (compose (parseInt, last)),
// decode :: String -> String
decode = compose (
join (' '), // Joins into a single string
map (head), // Takes the array words
convertAndSort, // Takes number, converts to integer, and sorts.
apply (zip), // Zips the two lists together
separateEncoding // Splits into two arrays on ";". Then splits on each of those arrays by " ".
),
decodeAndJoin = compose (join ("\n"), map (decode));
// Impure Code
log (decodeAndJoin (msgs));
// Outputs:
// The Manchester Mark 1 ran programs written in Autocode 1952
// A programming language, it used a compiler to automatically convert the language machine code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment