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 ?
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.
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:
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.