Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add convenience method to build trie from Enumerable #20

Open
roryokane opened this issue Apr 5, 2015 · 1 comment
Open

Add convenience method to build trie from Enumerable #20

roryokane opened this issue Apr 5, 2015 · 1 comment

Comments

@roryokane
Copy link

It could be called something like Trie.from_enumerable. Its definition is simple:

def Trie.from_enumerable(enumerable)
  enumerable.reduce(Trie.new) do |trie, item|
    trie.add(item); trie
  end
end

I would use this in my program to easily build a Trie from a text file:

words_trie = File.open(WORDLIST_FILE_PATH, 'r') do |wordlist_file|
  Trie.from_enumerable(wordlist_file.each_line)
end

If you want, you could also provide a version that adds both keys and values:

def Trie.from_key_value_enumerable(kv_enumerable)
  trie = Trie.new
  kv_enumerable.each do |key, value|
    trie.add(key, value)
  end
  trie
end
@tyler
Copy link
Owner

tyler commented Apr 5, 2015

Love it. Patches accepted. :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants