Here’s an anagram-finding program in Ruby:

table = Hash.new { |h,k| h[k] = [] } IO.foreach("/usr/share/dict/words") do |line| word = line.chomp key = word.downcase.chars.sort.join table[key] << word end word = ARGV[0].downcase anagrams = table[word.chars.sort.join] anagrams.map!(&:downcase) anagrams.delete(word) if anagrams.any? puts "Anagrams for '#{word}': #{anagrams.join(", ")}" else puts "Sorry, no anagrams for '#{word}'" end

Here's the same program, but in Crystal:

table = Hash(String,Array(String)).new { |h,k| h[k] = [] of String } File.each_line("/usr/share/dict/words") do |line| word = line.chomp key = word.downcase.chars.sort.join table[key] << word end word = ARGV[0].downcase anagrams = table[word.chars.sort.join] anagrams.map!(&.downcase) anagrams.delete(word) if anagrams.any? puts "Anagrams for '#{word}': #{anagrams.join(", ")}" else puts "Sorry, no anagrams for '#{word}'" end

See if you can spot the differences (there are exactly four).

Here are some benchmarks:

$ time ruby anagrams.rb avdi Anagrams for 'avdi': avid, diva real 0m0.621s user 0m0.597s sys 0m0.020s

$ crystal build --release anagrams.cr $ time ./anagrams avdi Anagrams for 'avdi': avid, diva real 0m0.230s user 0m0.305s sys 0m0.017s