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"

Where Next?

Popular in Questions Top

gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
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
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New

Other popular topics Top

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
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New

We're in Beta

About us Mission Statement