Question

I am learning Elixir and one of the examples I got from the PragProg book "Programming Elixir" is this:

iex(25)> (1..10) |> Enum.map(&(&1*&1)) |> Enum.filter(&(&1 < 20))
[1, 4, 9, 16]

After typing the code, I tried to play with it a bit and I wrote:

iex(26)> (1..10) |> Enum.map(&(&1*&1)) |> Enum.filter(&(&1 > 20))   
[25, 36, 49, 64, 81, 100]

Great, this was the expected outcome. Now when I write it like this:

iex(27)> (1..10) |> Enum.map(&(&1*&1)) |> Enum.filter(&(&1 > 40))   
'1@Qd'

Why do I get this weird '1@Qd' string?

Was it helpful?

Solution

It not bug in elixir, because method [] should return new list as in erlang.

But for symbols with unicode number betwee 32 and 255 it return their representation in Unicode (default setting).

iex(25)> [31]
[31]
iex(26)> [32]
' '
iex(27)> [255]
'ÿ'
iex(28)> [256]
[256]

But when you start erlang with +pc unicode it decode more symbols:

 erl +pc unicode
 Erlang R16B (erts-5.10.1) [source] [async-threads:0] [hipe] [kernel-poll:false]

 Eshell V5.10.1  (abort with ^G)  
 1> [1024].
 "Ѐ"
 2> [1070,1085,1080,1082,1086,1076].
 "Юникод" 

OTHER TIPS

"2.3 Strings (binaries) and char lists (lists)" in the getting started guide may be related on this topic.

http://elixir-lang.org/getting_started/2.html

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top