minhajuddin
Is there a simpler way to generate ids
I am using the code below to generate room ids:
reservations = [%{rooms: 1},
%{rooms: 2},
%{rooms: 3}]
{reservations_with_room_numbers, _} =
Enum.reduce(reservations, {[], 0}, fn reservation, {acc, offset} ->
upper_limit = offset+reservation.rooms - 1
room_numbers = Enum.to_list(offset..upper_limit)
reservation_with_room_numbers = Map.put(reservation, :room_numbers, room_numbers)
{[reservation_with_room_numbers|acc], upper_limit + 1}
end)
IO.inspect Enum.reverse(reservations_with_room_numbers)
# =>
# [
# %{room_numbers: [0], rooms: 1}
# %{room_numbers: [1, 2], rooms: 2},
# %{room_numbers: [3, 4, 5], rooms: 3},
# ]
The code above seems too complicated for such a simple task, Is there a simpler way to do the same?
System.unique_integer [:monotonic, :positive] gives out unique integers starting at 1, However there is no way to reset it after the generation is done, so that it starts from 1 for the next call.
Trending in Questions
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Using Phoenix.LiveView.TagEngine as an EEx.Engine is deprecated!
To compile HEEx, use Phoenix.LiveView.TagEngine.compile/2 instead.
Sta...
New
Hello !
We want new/edit form pages to POST/PUT to their own URL rather than the resources REST defaults (post /things, put /things/:id)...
New
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app?
Looking for hints regarding:
Addi...
New
Hi all, I wanted to ask how the community is dealing with post-release steps.
Today we have Ecto migrations, which make sure that the db...
New
I am using Oban and occasionally, shortly after a deployment, a handful of jobs can fail because of dependency on other parts of the syst...
New
Other Trending Topics
Patch Package: OTP 28.5.0.5
Git Tag: OTP-28.5.0.5
Date: 2026-08-04
Trouble Report Id: ...
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
I just stumbled on a newly redesigned elixir-lang.org. :tada: It looks like @Software_Mansion did the work, and I think it is generally a...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #performance
- #security










Marked As Solved
josevalim
I believe your initial solution looks good, I would just use
map_reduceinstead ofreduceto clean things up:Also Liked
JEG2
Or, with a generator:
benwilson512
One thing that isn’t clear is if we want a pure or impure solution. The original post had both. They’re wildly different solutions and people need to be aware of that.
michalmuskala
I believe this shows another use case for a function like
Stream.split/2- similar toEnum.split/2, but one that would leave the “rest” part as a stream. This would allow for the most natural way to solve this:Last Post!
sasajuric
It requires more code, but I tend to wrap such reductions in separate modules. Not sure about the wider context of your problem, so let’s say that multiple reservations make a singe “booking”. Then, I can have the booking module:
That’s more LOC, but on the upside, the client code can IMO become more readable:
If the module functionality is small, and it’s used in only one place, I just “inline” these funs (in this case
new/0,reserve/2,reservations/1), i.e. implement them as private in the same module where I use them.