heves
I’d like to generate a list from a variable length list of lists, choosing one element from each one. Like this:
list = [[:a, :b], [1, 2, 3], [{4, 5}, {5, 6}]]
magic_function(list)
Output:
[:a, 1, {4, 5}]
[:a, 1, {5, 6}]
[:a, 2, {4, 5}]
[:a, 2, {5, 6}]
[:a, 3, {4, 5}]
[:a, 3, {5, 6}]
[:b, 1, {4, 5}]
[:b, 1, {5, 6}]
[:b, 2, {4, 5}]
...
Is something like this possible in Elixir? I feel lost because of the variable length of the 2d list
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
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
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 12 to 3- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
gregvaughn
Oh, no! I suppose I’m building a reputation.
I was asked a variant of this at a job interview about 5 years ago and tried to make it work with a comprehension and failed. That was before the
reduceoption was added. I ultimately solved it with a recursive call to a comprehension. It needs a recursive/reduce style to handle the unknown count of lists.However, now that I think about it, I wonder if a macro could generate the right count of generators in the for comprehension? Nah, only if the input is known at compile time.
Eiji
Did you challenged a senior developer?
Pretty much the same as
Enum.reduce/3version. Simply pattern-matching here is not only in function clause, but also infor …, reduce: … clauseendnotation.However this looks like an
art for art's sake. Just look how simple is raw pattern-matching comparing to 2 other versions of my solution.Sebb
I wonder if there is any way to do this (passing Elji’s test cases) with a comprehension?
It’s easy when we know how many elements to combine.
But with an arbitrary list? @gregvaughn to the rescue! (because it’s your fault, that I’m always looking for the
forsolution)heves
Thank you all for your replies, this turned into a very educational discourse. Hopefully this topic will be as useful for others as it was for me.
I had this question originally pop up during a school assignment (of which this function would be only a small but important part), in which we had to consider fast runtime and optimal code, so I’m especially thankful for @Eiji
dtew
Thank you very much for this very educative discourse! I think this is an excellent way (for me at least) to learn. I will surely look through your previous posts for more Elixir fixes.
Eiji
Not yet, you do not have an optional code for handling
[]as element in root list. For now it correctly returns[]as output, but in my code you can optionally skip them.Try calling:
Smart, I forgot about such
List.wrap/1behaviour. However I think it’s not really clear for eveyone what happens here …Having in mind all cases and pattern-matching optimisations I would write it in this way:
Of course there is no need to uncomment every part for example empty list as input would work, but would behave slightly slower without pattern-matching.
What’s more interesting are 2 code fragments to skip and not empty list inside root list. This is useful when
Enum.filter/2or similar is used to generate input.trisolaran
Ah, good catch! Thanks.
I beg to differ. What about this one:
I think it covers all cases now. The initial accumulator of
[nil]for the reduce handles the case when there’s only one sublist: the sublist’s elements are prepended toList.wrap(nil), which is an empty list.So it appears recursion is not necessary after all
Eiji
Great you are improving answer, but no it does not works. If you want to enhance it then try all example cases I have shared in previous post.
Here is a result of one of them:
btw. I think it would be rather hard to write another solution which cover all cases, that I have shared, without recursion.
trisolaran
hands down
I updated mine, should cover all cases now
Eiji
I believe this code is the best:
This code should be fastest and work as long as the
inputis list contains 1 or more lists.The other solutions are limited to
3-elementlist and one of them have also other problem which was already mentioned by its author:In my case all of below calls work:
Note: If one of root list is empty, for example:
then then my example would properly return empty list. If you want to simply skit empty elements use this code:
right before “collecting data and recursion part” comment.
However the above does not work if empty list is a first element. To fix that you need an extra helper function to avoid conflicts in pattern-matching, for example: