Reputation: 14824
I'm trying to do this exercise from this book I'm learning from.
The goal is to read a bunch of words from a random .txt file (Which is supplied) and then find all words that start with D, and capitalize those words and return only them. This is what I have so far:
defmodule ReadFile do
def findD(contents) do
newArray = Enum.filter(contents, (word -> String.starts_with?(word, "D")))
|> Enum.map (word -> String.upcase(word))
end
end
I feel like in theory this should work just fine, but it doesn't seem to. Any information would be fantastic thanks.
I'm trying to use Filter
the way I find it in the Elixir Docs:
filter(t, (element -> as_boolean(term))) :: list
This is my error:
listTest.exs:1: warning: redefining module ReadFile
== Compilation error on file listTest.exs ==
** (CompileError) listTest.exs:3: unhandled operator ->
(stdlib) lists.erl:1353: :lists.mapfoldl/3
(stdlib) lists.erl:1354: :lists.mapfoldl/3
** (exit) shutdown: 1
(elixir) lib/kernel/parallel_compiler.ex:202: Kernel.ParallelCompiler.handle_failure/5
(elixir) lib/kernel/parallel_compiler.ex:185: Kernel.ParallelCompiler.wait_for_messages/8
(elixir) lib/kernel/parallel_compiler.ex:55: Kernel.ParallelCompiler.spawn_compilers/3
(iex) lib/iex/helpers.ex:168: IEx.Helpers.c/2
Upvotes: 0
Views: 773
Reputation: 15736
You have some mistakes in the function syntax, the right one would be this:
contents
|> Enum.filter(fn(word) -> String.starts_with?(word, "D") end)
|> Enum.map(fn(word) -> String.upcase(word) end)
Also, please, read the getting started guide.
Upvotes: 2