Last active
October 5, 2019 15:37
-
-
Save matthandlersux/d5d5933ece968a929ec2c6c2ebdc7e59 to your computer and use it in GitHub Desktop.
Revisions
-
matthandlersux revised this gist
Oct 5, 2019 . No changes.There are no files selected for viewing
-
matthandlersux revised this gist
Oct 5, 2019 . No changes.There are no files selected for viewing
-
matt handler created this gist
Apr 10, 2019 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,41 @@ const { uniq, map, range, zip, mapValues, groupBy, flatMap } = require('lodash'); const expandWordToDeletions = (word) => { const wordArray = word.split(""); const deletionList = map( range(word.length), i => { const copied = [...wordArray]; copied.splice(i, 1); return copied.join(""); } ) return [word, ...deletionList]; }; const createMap = (words) => ( mapValues( groupBy( flatMap(words, word => zip(expandWordToDeletions(word), (new Array(word.length + 1)).fill(word)) ), ([key, value]) => key ), listOfKeyValues => map(listOfKeyValues, ([key, value]) => value) ) ); const search = (word, dictionary) => { if (word in dictionary) return dictionary[word]; else { return uniq( flatMap( expandWordToDeletions(word), deleteWord => dictionary[deleteWord], ).filter(Boolean) ) } }; const dictionary = createMap(["matt handler", "mutt handler"]); console.log(search("patt handler", dictionary));