pertsevds
When i use List.flatten() it would flatten a deep list with every nested .
iex> list = [[],["ant",["hello","hi",[[[]]]], "bat"], ["cat", "dog"]]
[[], ["ant", ["hello", "hi", [[[]]]], "bat"], ["cat", "dog"]]
iex> List.flatten(list)
["ant", "hello", "hi", "bat", "cat", "dog"]
As expected.
But when i do Stream.flat_map()
iex(6)> list = [[],["ant",["hello","hi",[[[]]]], "bat"], ["cat", "dog"]]
[[], ["ant", ["hello", "hi", [[[]]]], "bat"], ["cat", "dog"]]
iex(7)> list
[[], ["ant", ["hello", "hi", [[[]]]], "bat"], ["cat", "dog"]]
iex(8)> |> Stream.flat_map(& &1)
#Function<60.124013645/2 in Stream.transform/3>
iex(9)> |> Enum.to_list()
["ant", ["hello", "hi", [[[]]]], "bat", "cat", "dog"]
It flattens only the first level of the list. It’s a flatten() with depth == 1.
And that was not what i expected.
I needed a deep_flatten() for Stream, so i’ve made it like this:
defmodule ExTelnet.StreamDeepFlatten do
def deep_flatten(enumerables) do
deep_flat_map(enumerables, & &1)
end
def deep_flatten(first, second) do
deep_flat_map([first, second], & &1)
end
def deep_flat_map(enum, mapper) when is_function(mapper, 1) do
Stream.transform(enum, nil, fn val, nil ->
case val do
val when is_list(val) -> {deep_flat_map(val, mapper), nil}
val -> {[mapper.(val)], nil}
end
end)
end
end
Works as expected for me:
iex(2)> list = [[],["ant",["hello","hi",[[[]]]], "bat"], ["cat", "dog"]]
iex(3)> list
iex(4)> |> Stream.map(&IO.inspect(&1))
iex(5)> |> ExTelnet.StreamDeepFlatten.deep_flatten()
iex(6)> |> Stream.map(&IO.inspect(&1))
iex(7)> |> Stream.map(&("seen " <> &1))
iex(8)> |> Stream.map(&IO.inspect(&1))
iex(9)> |> Enum.to_list()
[]
["ant", ["hello", "hi", [[[]]]], "bat"]
"ant"
"seen ant"
"hello"
"seen hello"
"hi"
"seen hi"
"bat"
"seen bat"
["cat", "dog"]
"cat"
"seen cat"
"dog"
"seen dog"
["seen ant", "seen hello", "seen hi", "seen bat", "seen cat", "seen dog"]
So what i’m questioning myself now is: “Am I reinvening the wheel? Maybe there is some better simpler method and I just don’t see it?”
Trending in Discussions
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
New
The obligatory hello world thread!
Who are you and where are you from? :stuck_out_tongue:
New
@chrismccord : I just saw the Extract AGENTS.md from Phoenix.new into phx.new generator commit to the phoenix project.
My initial shotgu...
New
I was working on an Ecto migration and I needed a timestamp. So, for the nth time, I looked up the different data types for timestamps, a...
New
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog
It says that Fly is going all-in on sprites, which is a worry ...
New
We’re evaluating API mocking tools for OpenAPI-based projects and would love to hear what other teams are using.
We’re particularly inte...
New
Is there a word for the ~> symbol used in Version strings?
Do you also just call it a Squiggle Arrow™ ?!
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
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
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New
Chat & Discussions>Discussions
Latest on Elixir Forum
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
- #blog-post
- #phoenix_html
- #iex
- #graphql
- #ai
- #genstage
- #elixirconf-us
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #metaprogramming
- #security
- #hex










First 10 of 11 Posts
LostKobrakai
There might be simpler way to do this, but I’m wondering what the background to this is. Can you show a practical usecase for this, where the inputs are not lists, but actual (nested) enumerables/streams?
pertsevds
Sometimes you have functions that parse some portion of text and returns . If they are included in one another we can have something like [[]] at the output.
Sebb
behaves like
Enum.flat_map/2not likeList.flatten/1pertsevds
Well, i know. But why?
lucaong
One reason:
flat_mapflattening a single level, andflattenflattening multiple levels, produces a more versatile behavior: if one needs to flatten only a single level (maybe the nested items are collections themselves and should be treated as individual items) one can useflat_map. If multiple levels should be flattened, one can callflatteninside the function called byflat_map.pertsevds
I don’t see it as that.
flattenfor lists - flattens multiple levels. So what is the word “flat” inflat_map? It’sflatten. What doesflattendo here? It’s flattening a single level. Why? It’s inconsistent withflattenfor lists.I think it should be named
concat_mapbecause of what it does. Not flattening, concatenating. And by the way, in the docs we have exactly that:Link: Enum — Elixir v1.14.2
lucaong
Another aspect is that
List.flattenis more specialized, it only flattens lists, while it leaves other collections unchanged:This makes it clear what to flatten and what not. Instead,
flat_mapflattens every enumerable:The fact that
flat_mapflattens all enumerable would make it more confusing if it were to flatten all levels: if each element is a list of maps, should the map be flattened too, or not? Flattening only one level leaves the choice to the developer.lucaong
Finally, and possibly more importantly, flattening a single level on
flat_mapis a common choice on many programming languages, making it the expected behavior for many developers.Examples are JavaScript:
Ruby:
Sebb
that’s why its called differently.
flat_mapis very handy when you map over something and you get results that you just want to omit.josevalim
Hi everyone, I believe @davaeron understands the differences between them. The question is about naming.
Correct. This is common nomenclature in all functional languages. Also add Scala and Erlang to your list.
You should read it as a “flat map operation”, i.e. as a map operation that joins its consecutive results, not as a “map plus flatten”.