为什么Elixir的Enum.count不返回数字?

时间:2018-09-03 16:27:03

标签: functional-programming elixir

我有以下输入内容:

[
  [
    {"title", "Aaaaaaaaa aaaaaaaaaaa aa aaa oaaaaaaaa"},
    {"title", "Aaaaaaa aa aaaa aaa.aa"},
    {"title", "Aaaaaaaaaaaa Aaaaaaa, aaa Aaaaaaa"},
    {"title", "Aaaa Aaaaaaaa aaaa Aaaaa"},
    {"title", "Aaaaaaa Aaaaaaa aaaa Aaaaa"},
    {"title", "Aaaaa Aaaaaaa aa Aaaaaa"},
    {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaaaaa, Aaa Aaaaaaa"},
    {"title",
     "Aaaaaaaaa Aaaaaaa Aaaaaa aaaaaaaa Aaaaaaaaaaaaa aaa Aaaaaaaaaa Aaaaaa Aaaaaa Aaaaaaa"},
    {"title", "Aaaaaaaaaaaa Aaaaaaa"},
    {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaa"}
  ],
  [
    {"url",
     "http://localhost:3000/aaaaaaaaa-aaaaaaaaaaa-aa-aaa-aaaaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaa-aa-aaaa-aaa-aa/"},
    {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa-aaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaa-aaaaaaaa-aaaa-aaaaa/"},
    {"url", "http://localhost:3000/aaaaaaa-aaaaaaa-aaaa-aaaaa/"},
    {"url", "http://localhost:3000/aaaaa-aaaaaaa-aa-aaaaaa/"},
    {"url",
     "http://localhost:3000/aaaaaaaaa-aaaa-aaaa-aaaaaaa-aaa-aaaaaaa/"},
    {"url",
     "http://localhost:3000/aaaaaaaaa-aaaaaaa-aaaaaa-aa-aaa-aaaaaaaaaaaaa-aaa-aaaaaaaaaa-aaaaa-aaaaaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaa-aaaaa-aaaa-aaaa/"}
  ]
]

然后我启动iex,并将其分配给变量,然后执行计数映射,例如在此iex会话中:

Interactive Elixir (1.7.3) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> input = [
...(1)>   [
...(1)>     {"title", "Aaaaaaaaa aaaaaaaaaaa aa aaa oaaaaaaaa"},
...(1)>     {"title", "Aaaaaaa aa aaaa aaa.aa"},
...(1)>     {"title", "Aaaaaaaaaaaa Aaaaaaa, aaa Aaaaaaa"},
...(1)>     {"title", "Aaaa Aaaaaaaa aaaa Aaaaa"},
...(1)>     {"title", "Aaaaaaa Aaaaaaa aaaa Aaaaa"},
...(1)>     {"title", "Aaaaa Aaaaaaa aa Aaaaaa"},
...(1)>     {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaaaaa, Aaa Aaaaaaa"},
...(1)>     {"title",
...(1)>      "Aaaaaaaaa Aaaaaaa Aaaaaa aaaaaaaa Aaaaaaaaaaaaa aaa Aaaaaaaaaa Aaaaaa Aaaaaa Aaaaaaa"},
...(1)>     {"title", "Aaaaaaaaaaaa Aaaaaaa"},
...(1)>     {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaa"}
...(1)>   ],
...(1)>   [
...(1)>     {"url",
...(1)>      "http://localhost:3000/aaaaaaaaa-aaaaaaaaaaa-aa-aaa-aaaaaaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaa-aa-aaaa-aaa-aa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa-aaa-aaaaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaa-aaaaaaaa-aaaa-aaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaa-aaaaaaa-aaaa-aaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaa-aaaaaaa-aa-aaaaaa/"},
...(1)>     {"url",
...(1)>      "http://localhost:3000/aaaaaaaaa-aaaa-aaaa-aaaaaaa-aaa-aaaaaaa/"},
...(1)>     {"url",
...(1)>      "http://localhost:3000/aaaaaaaaa-aaaaaaa-aaaaaa-aa-aaa-aaaaaaaaaaaaa-aaa-aaaaaaaaaa-aaaaa-aaaaaa-aaaaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa/"},
...(1)>     {"url", "http://localhost:3000/aaaaaaaaa-aaaaa-aaaa-aaaa/"}
...(1)>   ]
...(1)> ]
[
  [
    {"title", "Aaaaaaaaa aaaaaaaaaaa aa aaa oaaaaaaaa"},
    {"title", "Aaaaaaa aa aaaa aaa.aa"},
    {"title", "Aaaaaaaaaaaa Aaaaaaa, aaa Aaaaaaa"},
    {"title", "Aaaa Aaaaaaaa aaaa Aaaaa"},
    {"title", "Aaaaaaa Aaaaaaa aaaa Aaaaa"},
    {"title", "Aaaaa Aaaaaaa aa Aaaaaa"},
    {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaaaaa, Aaa Aaaaaaa"},
    {"title",
     "Aaaaaaaaa Aaaaaaa Aaaaaa aaaaaaaa Aaaaaaaaaaaaa aaa Aaaaaaaaaa Aaaaaa Aaaaaa Aaaaaaa"},
    {"title", "Aaaaaaaaaaaa Aaaaaaa"},
    {"title", "Aaaaaaaaa Aaaaa Aaaa Aaaa"}
  ],
  [
    {"url", "http://localhost:3000/aaaaaaaaa-aaaaaaaaaaa-aa-aaa-aaaaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaa-aa-aaaa-aaa-aa/"},
    {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa-aaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaa-aaaaaaaa-aaaa-aaaaa/"},
    {"url", "http://localhost:3000/aaaaaaa-aaaaaaa-aaaa-aaaaa/"},
    {"url", "http://localhost:3000/aaaaa-aaaaaaa-aa-aaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaa-aaaa-aaaa-aaaaaaa-aaa-aaaaaaa/"},
    {"url",
     "http://localhost:3000/aaaaaaaaa-aaaaaaa-aaaaaa-aa-aaa-aaaaaaaaaaaaa-aaa-aaaaaaaaaa-aaaaa-aaaaaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaaaaa-aaaaaaa/"},
    {"url", "http://localhost:3000/aaaaaaaaa-aaaaa-aaaa-aaaa/"}
  ]
]
iex(2)> input |> Enum.map(fn item -> Enum.count(item) end)
'\n\n'

不清楚上面的iex会话为什么不返回包含[10,10]的列表。我究竟做错了什么?

谢谢!

1 个答案:

答案 0 :(得分:2)

iex使用协议IO.Inspect,该协议将结果视为字符列表,因此将其视为\n\n。如果您使用以下行:

iex> input |> Enum.map(fn item -> Enum.count(item) end) |> hd |> to_string |> String.to_integer

您将得到:

iex> 10

与显示此列表时遇到的问题相同:

iex> list = [7,8,9]
'\a\b\t'

解决方案:您可以在地图中添加数字(例如0),以强制结果显示为数字列表:

iex> [0] ++ (input |> Enum.map(fn item -> Enum.count(item) end))
[0,10,10]

这个话题已经讨论过here