Skip to content

Instantly share code, notes, and snippets.

@nhusher
Last active March 27, 2017 15:48
Show Gist options
  • Save nhusher/3109152437b3490df17ce52715380f39 to your computer and use it in GitHub Desktop.
Save nhusher/3109152437b3490df17ce52715380f39 to your computer and use it in GitHub Desktop.
const A = 'A'.charCodeAt(0)
const cipher = offset => character =>
String.fromCharCode(A + (offset + character.toUpperCase().charCodeAt(0) - A) % 26)
const encode = (s, d) => s.replace(/\W/g,'').split('').map(cipher(d)).join('')
const decode = (s, d) => s.split('').map(cipher(26 - d)).join('')
decode(encode("Friends, Romans, Countrymen, lend me your ears!", 10), 10)
// => 'FRIENDSROMANSCOUNTRYMENLENDMEYOUREARS'
const COINS = [ 25, 10, 5, 1 ]
const assoc (o, k, v) => Object.assign({}, o, { [k]: v })
const change = val =>
// Here we're reducing over two values to retain referential transparency.
COINS.reduce(([result, pennies], coin) => (
[ assoc(result, coin, Math.floor(pennies/coin)),
pennies % coin ]),
[{}, (val - Math.floor(val)) * 100])[0]
change(0.83)
// => { 25: 3, 10: 0, 5: 1, 1: 3 }
const invertIndex = files =>
files.reduce((textIndex, file, i) =>
file.split(/\w+/g).reduce((textIndex, word) => {
if(!textIndex[word]) textIndex[word] = new Set()
textIndex[word].add(i)
return textIndex
}, textIndex))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment