Skip to content

Instantly share code, notes, and snippets.

@jeremy-w
Created August 26, 2013 22:27
Show Gist options
  • Save jeremy-w/6347409 to your computer and use it in GitHub Desktop.
Save jeremy-w/6347409 to your computer and use it in GitHub Desktop.
Demonstrates funky fn name mangling and my own confusion with Dict.merge/3 docs.
defmodule MergeProblem do
def count(string) do
words = String.split(string)
|> Enum.reject(&1 == "")
|> Enum.map(&String.downcase/1)
count = HashDict.new(words, fn key -> {key, 0} end)
count = HashDict.merge(count, words, fn _k, v1, _v2 -> v1 + 1 end)
count
end
# Yields error:
# word-count% iex
# Erlang R16B01 (erts-5.10.2) [source] [64-bit] [smp:8:8] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
#
# Interactive Elixir (0.10.1) - press Ctrl+C to exit (type h() ENTER for help)
# iex(1)> c("merge-problem.exs", ".")
# {{path}}/word-count/merge-problem.exs:1: redefining module MergeProblem
# [MergeProblem]
# iex(2)> MergeProblem.test()
# ** (FunctionClauseError) no function clause matching in HashDict."-merge/3-fun-5-"/2
# /private/tmp/elixir-wnuv/elixir-0.10.1/lib/elixir/lib/hash_dict.ex:215: HashDict."-merge/3-fun-5-"("test", #HashDict<[{"a", 0}, {"is", 0}, {"test", 0}, {"this", 0}]>)
# /private/tmp/elixir-wnuv/elixir-0.10.1/lib/elixir/lib/enum.ex:1522: Enumerable.List.reduce/3
# erl_eval.erl:569: :erl_eval.do_apply/6
# src/elixir.erl:147: :elixir.eval_forms/3
def test() do
count("test this is a test")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment