minhajuddin
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 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
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
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
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
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
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
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
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
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
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
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
- #ecto-query
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #hex
- #security










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
andre1sk
minhajuddin
Thanks this is definitely clearer than the first implementation
This would be easy in
javascriptwith a simple generator function. I was hoping there would be some generator function which keeps giving new ids.andre1sk
Stream.iterate(offset, &(&1+1)) ?
minhajuddin
The js code persists the state, so it can give new ids, However this code gives a new set of ids starting from 0 every time.
JEG2
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.
minhajuddin
Thanks for sharing this solution.
rvirding
I don’t quite understand how you mean starts again at 1. Documentation for
:erlang.unique_interger, which I assumeSystem.unique_integercalls, says:“Generates and returns an integer unique on current runtime system instance. The integer is unique in the sense that this BIF, using the same set of modifiers, will not return the same integer more than once on the current runtime system instance.”
A simple test shows getting a different integer for each call.
minhajuddin
Thanks for dropping in
You are right about
System.unique_integer, It gives a stream of increasing numbers. However, I want my ids to reset for every function call. So, something like below: