rms.mrcs
Hi,
I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. I’ve managed to do this using the following code:
def indexed_map(list, index \\ 1, step \\ 1) do
to_indexed_map(list, index,step, %{})
end
defp to_indexed_map([h|t], i, s, acc),do: to_indexed_map(t, i + s, s, Map.put(acc, i, h))
defp to_indexed_map([], _, _, acc), do: acc
Basically if I apply this function to
[100, 200, 300]
it will return
%{1 => 100, 2 => 200, 3 => 300}
Is there any way to the same using Elixir’s standard library? I tried Enum.map, Enum.reduce and even Enum.map_reduce but I just couldn’t figure out how to it using them.
Thanks
Trending in Questions
Hey guys,
I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly
Do you guys have any suggestions what is the best prac...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Hello,
I’m developing a online persistent chat system (what’s app) like using elixir/dynamodb/aws for a mobile app(flutter).
The diffic...
New
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
Anyone here using Honeybadger?
My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of
Bandit.HTTPError...
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
Other Trending Topics
This release unifies configuration for queues, repos, and services, swaps opaque timing integers for readable durations, and backports pe...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
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
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
There are three potential reasons for members of this forum to have a look at https://vutuv.de
You are tired or annoyed of LinkedIn.
Yo...
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
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #blog-post
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 9- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Linuus
Something like this should work:
Or this
andre1sk
There is prob a better way then this:
rms.mrcs
Thanks.
I always used only this form:
How exactly the
{v, k}is filled by Elixir like in:?
EDIT: Nevermind, I figured it out in the docs (is the
Enum.with_indexfunction)rms.mrcs
Thank you
Linuus
The
{v, k}is just a pattern match on the values coming in..with_indexreturns tuples of the value and the index.Like this:
So for each value I just destruct the passed in tuple, like
{v, k} = {100, 1}.rms.mrcs
Got it
Thanks!
OvermindDL1
Or shorter (by actually creating the tuple in-order instead of needing to swap it and not getting the
lengthof a list, which can beO(n)instead ofO(1)):An aside, I wish
0..was a shortcut for(Stream.iterate(0, &(&1+1)), or maybe0...1means(Stream.iterate(0, &(&1+1))like0...-4means(Stream.iterate(0, &(&1-4))or so, ah well I wish, but it does not for now. Hmm, lot of ideas popping into mind for such a syntax, we need a Stream.seq/1.Qqwy
You might like the Sequences Hex package that I made a while back. It defines streams for quite a few common numeric sequences.
OvermindDL1
Huh, nice! Not ran across that yet.