BorjaEst
Tree random element
Trees (specially gb_trees) are a very efficient/fast tool to store Key-Value. However, depending on the program it’s usage cat get a bit more complicated.
Here goes my challenge.
What would be the most efficient way to get a random element and it’s path from a gb_tree?
First Post!
al2o3cr
Last Post!
BorjaEst
So after thinking about it, my solution would be something like this:
iex(1)> tree = :gb_tree.example() #Any f that returns a tree
{3, {:a, 1, nil, {:b, 2, nil, {:c, 3, nil, nil}}}}
iex(2)> n = :rand.uniform(:gb_trees.size(tree))
2
iex(11)> iter = :gb_trees.iterator(tree)
[{:a, 1, nil, {:b, 2, nil, {:c, 3, nil, nil}}}]
iex(14)> {k,v,_}=Enum.reduce(1..n, {:nil,:nil,:gb_trees.iterator(tree)}, fn _, {_,_,i} -> :gb_trees.next(i) end)
{:b, 2, [{:c, 3, nil, nil}]}
However I am thinking, now gb_trees are not used any longer as we do have maps.
0
Popular in Challenges
Note by the Moderators: This topic is to talk about Day 5 of the Advent of Code.
For general discussion about the Advent of Code 2018 an...
New
Note: This topic is to talk about Day 4 of the Advent of Code 2019.
There is a private leaderboard for elixirforum members. You can join...
New
This topic is about Day 5 of the Advent of Code 2021.
We have a private leaderboard (shared with users of Erlang Forums ):
https://adve...
New
Probably not the most efficient implementation, because part 1 took >1 ms and part 2 >4ms, but the code was simple enough.
def p...
New
Here’s my day 3 code
https://github.com/code-shoily/advent_of_code/blob/master/lib/2024/day_03.ex
This was quite easy. I was afraid Par...
New
Today’s problem is really tense. I don’t think I can do it without libgraph.
New
I said I was on a break, but I took a sneak peak and it looked fun so…
Part 1 completes in half a millisecond with a single pass of the ...
New
Other popular topics
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set?
Thanks.
New
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
New
Hi everyone!
I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
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
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex









