holasoymas

holasoymas

Strange Output When Filtering List with `Enum.filter/2`

I’m encountering a strange issue when using Enum.filter/2 in my Elixir script. The problem occurs when I filter a list with a condition that elements should be greater than a certain value using pipe operator.

pipe_operator.exs

defmodule PipeOperator do
  def return_big(list) do
    list
    |> Enum.map(&(&1 * 2))
    |> Enum.filter(&(&1 > 40))
  end
end

big = PipeOperator.return_big([2, 5, 20, 30,40])
IO.inspect(big)

EXPECTED RESULT
I expect the output to be a list of elements greater than 40 after they have been doubled. For the input list [2,5,20,30,40] . The doubled list is [4,10,40,60,80] and the filtered list should be [60,80].

ACTUAL RESULT
When I filter with &(&1 > 40), instead of the expected list, I get this strange output in my terminal:

myname@Lenovo-V14-IGL:~/Desktop/learnelixir/modules$ elixir pipe_operator.exs 
~c"<P"
myname@Lenovo-V14-IGL:~/Desktop/learnelixir/modules$ 

and sometime ~c"<Px" , ~c"<\n(<Pdepending the value of list.

However, if I change the filter condition to &(&1 > 2), or 1 it works as expected, and I get the correct filtered list.

Elixir version : Elixir 1.17.0 (compiled with Erlang/OTP 27)
OS : Linux mint 21.2

Most Liked

rvnash

rvnash

IO.inspect is interpreting the list of integers as a Charlist, and rendering it with the ~c sigil. You can explicitly tell IO.inspect to render the list of integers as a list.

See: FAQ · elixir-lang/elixir Wiki · GitHub

iex(6)> IO.inspect([60,80])
~c"<P"
~c"<P"
iex(7)> IO.inspect([60,80], charlists: :as_lists)
[60, 80]
~c"<P"

Last Post!

dimitarvp

dimitarvp

They are exactly the same, the numbers can be interpreted as printable characters so they are shown as such. This setting can be disabled btw.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement