--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/9d0f6096-43aa-441b-9060-1a4949ff8e67n%40googlegroups.com.
To give some more context to 'exploring a new protocol + API in the context in a library':
As Kurtis Rainbolt-Greene also mentioned: (A datatype supporting this) structure-preserving transformation operation is known as an `(Endo)functor` (and in some other places as a `Mappable`).
`Enumerable`, on the other hand, implements the transformation
operation known in other languages as `Foldable`
(or, in more maths-heavy contexts, a `Catamorphism`).
This transformation is inherently not
structure-preserving.
And while that means the structure is lost, it also allows it to
be implemented by a much wider range of commonly-used datatypes
(such as maps, sets, file handles, etc.) that cannot implement a
structure-preserving mapping.
It also means that `Enum`/`Stream` play more nicely with `for`
expressions, which are built around the same abstraction.
Not everything always turns the result into a list; that is what `Collectable` is for (which is known in some other languages as `Semigroup`/`Monoid`). Many `Enum`functions and also `for` allow you to specify a `Collectable` directly. But for those who do not, there is no issue with first turning the result into an intermediate list and turning that into a `Collectable` later at the end of a pipeline But it is also always fine to first create a list and only turn it into the desired `Collectable` later using `Enum.into`. This is always possible (because of what lists are, mathematically speaking,) and also usually reasonably efficient.
And for some (but not all) collections, structure is not really lost when converting to a list and back. (Such as maps (when viewed as key-value pairs) and sets).
Does that mean that `Functor`s are never used/never useful in
Elixir? No. There are definite use-cases, though not really in
Elixir's standard library.
Currently we see them used in two ways in the ecosystem:
1) Certain datastructures (ex: trees, graphs, tensors) really
desire a way to transform their elements while preserving their
structure. Elixir libraries implementing them often define a
specialized `map` or `transform` function.
2) Libraries exist which define a generalized `Functor` protocol.
The most well-known is probably Witchcraft. But
for many projects a dependency like this might be overkill.
~Marten/Qqwy
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/CAGnRm4JXT7V%2BMnk6i2WFzJZbNQ0G23x14WvhdUG_aXPbxohB5Q%40mail.gmail.com.