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
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
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.
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










