Skip to content

Instantly share code, notes, and snippets.

@kwijibo
Created April 1, 2016 09:59
Show Gist options
  • Save kwijibo/2069e0ec6f3e4b320c0124dc0b25c991 to your computer and use it in GitHub Desktop.
Save kwijibo/2069e0ec6f3e4b320c0124dc0b25c991 to your computer and use it in GitHub Desktop.
Search Trie
const branchLetter = ({full, sub},letter) => {
sub[letter] = sub[letter] || {}
return {full, sub: sub[letter] }
}
const indexWord = (trie, word) => {
const {full, sub} = word.split('').reduce(branchLetter, {full:trie, sub: trie})
return full
}
const indexText = text => text.split(' ').reduce(indexWord, {})
const index = indexText('It would be easy to say that error handling is a black art in software development but that implies that there is some secret stash of knowledge out there. The truth is that we tend to think of error handling as a lesser concern.')
console.log(index)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment