Last active
December 19, 2015 02:19
-
-
Save mattbierner/5882460 to your computer and use it in GitHub Desktop.
Javascript build trie from array-like
This file contains 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
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, {}); | |
}; | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.