elbasti

elbasti

Hi folks, I’ve been running into some very strange behavior that has me knocking my head against the wall.

If I run the following code:

["42", "12", "15"]
|> Enum.map(&(String.to_integer(&1)))

I get what you’d expect: the list [42, 12, 15]

But if I run this code:

["41", "92", "73", "84", "69"] 
|> Enum.map(&(String.to_integer(&1)))

I get this weird string: ~c")\\ITE"

In fact, if I run just this:

["41"] 
|> Enum.map(&(String.to_integer(&1)))

I get ~c")"

What on earth is going on?!

I figured “maybe there’s some unicode strageness going on with LiveBook” so I tried it in iex, with the same results:

Showing Posts 1 to 10

outlog

outlog

congrats on reaching this milestone - and completing a major step/rite of passage on your elixir journey - it’s a charlist Binaries, strings, and charlists — Elixir v1.21.0-dev

we have all been there - and gone through the same wtf etc.

17
Post #1
mindok

mindok

Try this to force lists of integers to display as lists of integers (reference: Inspect.Opts — Elixir v1.12.3):

["41", "92", "73", "84", "69"] 
|> Enum.map(&(String.to_integer(&1)))
|> IO.inspect(charlists: :as_lists)
stevensonmt

stevensonmt

That this is not the default is a little crazy to me.

kokolegorille

kokolegorille

It is a tribute to the Erlang community :slight_smile:

dimitarvp

dimitarvp

Yeah, you are basically seeing the runtime trying to be a bit too helpful – if a list of integers looks like a list of printable characters, then the REPL will instead show them as a printable string. The data is still there and is still the same.

If you want to get rid of that behavior in your project, just create an .iex.exs file at the root of your project and put this in there:

IEx.configure(inspect: [charlists: :as_lists])

Restart your iex session and, et voila:

iex(1)> ["41"] |> Enum.map(&String.to_integer/1)
[41]
stevensonmt

stevensonmt

The problem I run into is that it does not apply to any IO.inspect calls within the code. You have to remember to do IO.inspect(number_list_that_isnt_supposed_to_be_chars, charlists: :as_lists) every time you might be faced with this weirdness.

Aetherus

Aetherus

I think it’s time to make charlists: :as_lists to be a default option, and deprecate the charlists: :infer option.

LostKobrakai

LostKobrakai

I think that would just surface how often we actually run into charlists when interacting with erlang libraries. E.g. hardly any elixir app doesn‘t use gen_tcp somewhere.

From my personal experience I‘ve been hitting the case of needing to see the underlying list of integers mostly in tests or puzzles, where there is time and opportunity to deal with the printed formatting. Not optimal, but fine.

Interacting with actual charlists on the other hand comes up when you‘re debugging some production issue and trying to dig deeper into some failure, where you don‘t want to deal with first setting up useful formatting rules to be able to understand what the system is trying to provide in information.

fuelen

fuelen

Hehe
I was right here

Adzz

Adzz

At least now there is the chance that someone can look up the documentation for the ~c sigil. Just because it didn’t happen in this case it does not mean it’s not an improvement.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews