Codball
I have a function
def list_store_params do
#query = Ecto.Query.from(s in Store, select: [s.state, s.city, s.name], distinct: true)
IO.inspect(Repo.all(query()))
query()
|> Repo.all()
|> Enum.group_by(&Enum.take(&1, 2))
|> Enum.map(fn {key, value} ->
key ++ Enum.flat_map(value, &Enum.drop(&1, 2))
end)
|> Enum.chunk_every(2)
|> IO.inspect
end
def query,
do:
from(s in Store,
order_by: [asc: s.state, asc: s.city, asc: s.name],
select: %{
a: s.state,
b: s.city,
c: s.name
}
)
that converts
[
%{a: "Idaho", b: "Boise", c: "Test"},
%{a: "Colorado", b: "Denver", c: "Eagle"},
%{a: "Colorado", b: "Denver", c: "Jelly Beans"},
%{a: "Colorado", b: "Denver", c: "Name"}
]
into
[
[
[a: "Idaho", b: "Boise", c: "Test"],
[a: "Colorado", b: "Denver", c: "Eagle", c: "Jelly Beans", c: "Name"]
]
]
which I need to convert to a keyword map {a: “Idaho”, b: “Boise”, c: “Test”}. I’m having trouble understanding Enum.into and Map.new, does anyone have any direction/sources here?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
peerreynders
You shouldn’t be using
Enum.take/2orEnum.drop/2on aMapbecause the key order is not guaranteed - it’s considered an implementation detail. You should choose a more reliable means.Map.to_list/1Keyword.take/2Note also that
Enum.group_by/3also accepts avalue_fun/1as the third parameter, so you could extract the:cpair immediately. Then you can useList.flatten/1in the next step.It also isn’t clear from your description what exactly you want to accomplish. Given:
what exactly, in detail is
outputfromoutput = convert(input)going to look like?kokolegorille
I grouped your list by :a and :b keys, then I merged all the :c keys, mapped into a keyword list.
I am not sure if this is what You expect, but it seems to output the right result (minus the fact You wrap all in a double list …).
It’s kind of ugly code
I would prefer something like this as I don’t like to have duplicate keys.
Codball
I think I should probably be accomplishing what I need through the query in the first place rather than doing math on the whole database client side. The idea is to group c in b and b in a.
I want to show a distinct list of states, with a distinct list of cities with a list of all entries of c.
it should look like diagram
Eiji
Yeah, I also think that’s best solution.
I do not have repository to test this code, but it should look like:
Codball
returns
# Tuples can only be used in comparisons with literal tuples of the same sizeEiji
right, it should be
List- notTupleCodball
That returns:
is there a way to make a: distinct like
I’m trying to accomplish diagram
Eiji
sure, just change a bit query:
Codball
As I have more than one entry under a city, this doesn’t accomplish what I was hoping either.
I want to display it in a template like
etc to solve an accordion like flow as in diagram
Thanks for your help.
peerreynders
The old school approach is to have the RDBMS order the result to keep the necessary information clustered together and then aggregate the result rows:
Using Ecto associations is the easier option.
Given the following schemas