I’m implementing a custom log formatter and I wanted to parse the lists returned by SASL except they seem to be weird:
iex(1)> msg = ["Application ", "logger", " started at " | ":nonode@nohost"]
["Application ", "logger", " started at " | ":nonode@nohost"]
iex(2)> is_list msg
true
iex(3)> is_binary msg
false
iex(4)> Enum.map(msg, &IO.inspect/1)
"Application "
"logger"
" started at "
** (FunctionClauseError) no function clause matching in Enum."-map/2-lists^map/1-0-"/2
(elixir) lib/enum.ex:1088: Enum."-map/2-lists^map/1-0-"(&IO.inspect/1, ":nonode@nohost")
(elixir) lib/enum.ex:1088: Enum."-map/2-lists^map/1-0-"/2
(elixir) lib/enum.ex:1088: Enum."-map/2-lists^map/1-0-"/2
iex(4)> List.flatten(msg)
** (FunctionClauseError) no function clause matching in :lists.do_flatten/2
(stdlib) lists.erl:626: :lists.do_flatten(":nonode@nohost", [])
(stdlib) lists.erl:629: :lists.do_flatten/2
(stdlib) lists.erl:629: :lists.do_flatten/2
iex(5)> for x <- msg do
...(5)> IO.inspect x
...(5)> end
"Application "
"logger"
" started at "
** (FunctionClauseError) no function clause matching in Enum."-reduce/3-lists^foldl/2-0-"/3
(elixir) lib/enum.ex:1473: Enum."-reduce/3-lists^foldl/2-0-"(#Function<12.54118792/2 in :erl_eval.expr/5>, [" started at ", "logger", "Application "], ":nonode@nohost")
I notice the following: The “list” returned here is not of the shape [ :item1 | [ :item2 | [] ]] in that its final element doesn’t seem to be a []. My personal guess would be that’s why functions from the Enum and List modules seem to fail on it.
But: is_list still returns true even though this is for most purposes a malformed list (in that it cannot be used in most List and Enum module applications).
Is this how things should be? How to best spot such a list and deal with it?