Why is this happening??
iex(13)> maps123 = for _ <- 0…2, do: %{name: “g”, value: 100}
[%{name: “g”, value: 100}, %{name: “g”, value: 100}, %{name: “g”, value: 100}]
iex(14)> Enum.map maps123, fn x -> x.value end
‘ddd’
iex(15)>
Why is this happening??
iex(13)> maps123 = for _ <- 0…2, do: %{name: “g”, value: 100}
[%{name: “g”, value: 100}, %{name: “g”, value: 100}, %{name: “g”, value: 100}]
iex(14)> Enum.map maps123, fn x -> x.value end
‘ddd’
iex(15)>
I think you are asking why [100, 100, 100]
is inspected as 'ddd'
?
Well, thats because its interpreted as a charlist. Any list of integers that only consists of printable codepoints is inspected like this.
You can check using iex:
iex(1)> [100, 100, 100] === 'ddd'
true
iex(2)> i [100, 100, 100]
Term
'ddd'
Data type
List
Description
This is a list of integers that is printed as a sequence of characters
delimited by single quotes because all the integers in it represent printable
ASCII characters. Conventionally, a list of Unicode code points is known as a
charlist and a list of ASCII characters is a subset of it.
Raw representation
[100, 100, 100]
Reference modules
List
Implemented protocols
Collectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars
iex(3)> i 'ddd'
Term
'ddd'
Data type
List
Description
This is a list of integers that is printed as a sequence of characters
delimited by single quotes because all the integers in it represent printable
ASCII characters. Conventionally, a list of Unicode code points is known as a
charlist and a list of ASCII characters is a subset of it.
Raw representation
[100, 100, 100]
Reference modules
List
Implemented protocols
Collectable, Enumerable, IEx.Info, Inspect, List.Chars, String.Chars
As @NobbZ is saying, basically 'ddd' == [100, 100, 100]
due to how Elixir interprets anything that seems to be a list of characters.
You are still getting exactly what you expect.
You can print it as integer list with
'ddd' |> IO.inspect(charlists: false)
If all you care is inspecting the result