I want to convert Tuple to List

def list_contributor_by_attributed_tasks(attributor_id) do
    query =
      from t in Task,
      where: t.attributor_id == ^attributor_id

    result =
      Repo.all(query)
      |> Enum.map(fn t -> t.contributor_id end)
      |> List.to_tuple()

    Enum.map(result, fn res -> res end)
  end

Result:

iex(109)> a = Monitoring.list_contributor_by_attributed_tasks(53)
[debug] QUERY OK source="tasks" db=1.6ms idle=1658.4ms
SELECT t0."id", t0."date_end", t0."date_start", t0."deadline", t0."estimated_duration", t0."performed_duration", t0."progression", t0."title", t0."description", t0."achieved_at", t0."hidden", t0."without_control", t0."parent_id", t0."contributor_id", t0."priority_id", t0."attributor_id", t0."status_id", t0."project_id", t0."inserted_at", t0."updated_at" FROM "tasks" AS t0 WHERE (t0."attributor_id" = $1) [53]
{91, 53}
iex(110)> b = Tuple.to_list(a)
'[5'
iex(111)> i b
Term
  '[5'
Data type
  List
Description
  This is a list of integers that is printed as a sequence of characters
  delimited by single quotes because all the integers in it represent printable
  ASCII characters. Conventionally, a list of Unicode code points is known as a
  charlist and a list of ASCII characters is a subset of it.
Raw representation
  [91, 53]
Reference modules
  List
Implemented protocols
  Collectable, Enumerable, IEx.Info, Inspect, Jason.Encoder, List.Chars, Phoenix.HTML.Safe, Phoenix.Param, Plug.Exception, String.Chars, Swoosh.Email.Recipient
iex(114)> 


But i need it to return [91, 53]. How I can do it ?

This does return [91, 53] only printed as a charlist.

https://hexdocs.pm/elixir/List.html#module-charlists

But then I can’t convert it to a list so

It already is a list, there is no need to convert it in this instance. It is just (somewhat unhelpfully) being shown as a string in the shell.

The linked documentation above gives more detail as to why this is

Read the documentation I linked to. It should give you all the confidence in that this is indeed a list.

1 Like

But it’s possible to convert it in Raw representation or not ???

The representation in the shell is only a behavior of inspect, it is a list of integers through and through and the rest of your code will treat it that way. You can change your iex inspect settings to disable charlists but it won’t change the way your code actually works.

2 Likes

I know it’s confusing, but there is no need to convert it: [91, 53] is '[5'. You can try to type [92, 53] in the repl to see that. Those are just two alternative ways to print out the same data. The repl guesses that, since 91 and 53 are both printable characters, '[5' is what you want. In this case, the guess is wrong. But the underlying data is the same:

[91, 53] == '[5'
#=> true
4 Likes

Also, you can also fiddle with your Inspect.Opts .

iex> x = [91,53]
'[5'

Iex> IO.inspect x
'[5'
'[5'

iex> IO.inspect(x, [charlists: :as_lists])
[91, 53]
'[5'

2 Likes

Additionally, one can create a .iex.exs file at the root of their project and put something like this inside:

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

And have the less confusing inspect behaviour be the default in the current project.

@alain-nambi Sadly this is a common confusing point for Elixir newcomers. But everything is working as you already expect, the confusion is simply visual.

2 Likes