sagnik2911
Iterating over a lists of list of tuple
Here nums is a list of [list of {tuples}].
nums = [[{5,2}], [{10,2},{4,5}], [{6,5},{3,10},{2,15}]]
I want to print output like this:
10 5 2
20 10 2 4 5
30 6 5 3 10 2 15
Here, the first number is desired to be the product the tuple, followed by the tuples (factors).
The code I used,
Enum.each(nums, fn n ->
case n do
[{x,y}] ->IO.puts "#{x*y} #{x} #{y}"
end
end)
But my code only handles the case where “nums” at any index has only 1 tuple.
What if it has n tuples and how can I handle the same?
Most Liked
Ted
Oh, oops; I could have been clearer. ![]()
Regarding the assumption of pre-grouped tuples, I was talking about continuing to accept the original list-of-lists, perhaps due to a limitation of the source, e.g.:
[[{5,2}], [{10,2}, {4,5}], [{6,5}, {3,10}, {2,15}]]
|> List.flatten()
|> Enum.group_by(fn {a, b} -> a * b end)
|> ...
But I agree that a single list of tuples would be a preferable input format, if the source can provide it.
sagnik2911
Thanks @OvermindDL1 and @Ted for your help.
The filtering at the source using List.flatten looks much better. Applied that. And there goes my first project in Elixir. Thank you for your help. Elixir!
kalpak92
@OvermindDL1 and @Ted
Thank you guys for this . Saved me big time.. You guys are rockstars.. !!
Popular in Questions
Other popular topics
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
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance









