Skip to content

Instantly share code, notes, and snippets.

@mattbierner
Last active December 19, 2015 02:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbierner/5882460 to your computer and use it in GitHub Desktop.
Save mattbierner/5882460 to your computer and use it in GitHub Desktop.
Javascript build trie from array-like
var reduce = Function.prototype.call.bind(Array.prototype.reduce);
var trie = (function(){
var wordReduce = function(parent, l) {
return (parent[l] = (parent[l] || {}));
};
var wordsReduce = function(trie, word) {
var node = reduce(word, wordReduce, trie);
node[''] = null;
return trie;
};
return function(words) {
return reduce(words, wordsReduce, {});
};
}());
@mattbierner
Copy link
Author

Converting word to a string on line 8 will alternatively allow words to be an array-like of objects with toString implementations instead of an array-like of array-likes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment