Last active
March 27, 2017 15:48
-
-
Save nhusher/3109152437b3490df17ce52715380f39 to your computer and use it in GitHub Desktop.
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 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' |
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 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 } | |
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 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