9mm
Hello,
I have a collection like:
%{stats_a: [4, 5, 6], stats_b: [1, 2, 3]} # not actually integers (see next question)
Each of the 2 lists will be ~1 million items long.
I have a method which needs to push an item to beginning of each list. Ideally, it would in real-time chop the oldest/last items of the list so that it’s never longer than 1,000,000 items exactly
If theres no super fast way to do this however I will set a timer to do it every 1000ms… if there is an O(n) fast way to do it though I’ll do it in realtime. I need this timer anyway to aggregate stats so I’m not bothered if it needs to go there.
I will be pushing to it ~300-500 times/second.
Here’s what I had so far (without the limiting part yet…)
Is this crazy what I wrote?
def handle_cast({:push_stats, key, stats}, state) do
items = [stats | state[key]]
new_state = state |> Map.put(key, items)
{:noreply, new_state}
end
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
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
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
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
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 new to elixir and just tried to install the elixirLS extension for VScode(ium) and it is throwing some errors that I would like help ...
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
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 19 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
NobbZ
That attempt counts
nils as0instead of skipping them, at least it like this from a first glance, is this really what you want?peerreynders
Obviously I don’t care about line count - I like code that is easy to change:
9mm
This was my weak attempt, however it’s failing if there’s a single item which has a response_value of nil… which is showing me I have absolutely no idea how this works.. I thought value1 was the existing value from the accumulator and value2 was from the incoming map (from the list)
EDIT OK crap I see it, its because now it’s setting it for every value2…
NobbZ
I’m sure it does not work with your input directly, but if you want to retain the keys of your old data, this should work out:
Sorry for only having such a half snippet, but I’m currently on my mobile.
9mm
Does this apply to my input data in post #2 of this thread?
This is what I had before adding averaging which seems to be the simplest way “so far” (thanks to help of peerreynders). I also need to combine it in the final output as well
NobbZ
Wait? A simple average calculator with a reducer and you have 3 clauses? Then theres something wrong…
9mm
This is what I ended up doing… it’s so insanely verbose but I just couldnt’ get the {total, count} method to work
I’m hoping it’s not super simple and I just missed something (very likely).
I first calculate the totals in a list, then i calculate the actual list, and then I combine them
9mm
Ok so i ended up thinking this was easy but it turned out to be really complicated because the input value can now be nil, integer, or {total, count}, and now the reducer function can accept any of those as input
I had about 3 guard clauses and I just stopped because it’s too confusing to reason about a month from now
If you are thinking of an easier way would you mind exposing my mind to your greatness?
9mm
Dude thats a great idea… ok thank you!
peerreynders
One possible work around is to store
{total,count}instead of the average, so when you update you store{total+more,count+1}and when you need the average later you simply calculatetotal/count.