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(<P
depending 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