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
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
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










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.