lud
"Prepend" a transform to a collectable
Hello,
I would like to run the following code:
System.cmd("mix", ["deps.get"], cd: dir, stderr_to_stdout: true, into: IO.stream(:stdio, :line))
But I would like to indent all lines with an indent prefix, so basically with fn line -> [" ", line] end.
I am not sure whether there is an API for that. Stream.map or Enum.into need an initial enumerable value.
So I implemented my own stream (in a very naive way I guess):
defstruct [:target, :mapper]
def transform(target, mapper) do
%__MODULE__{target: target, mapper: mapper}
end
defimpl Collectable do
def into(%{target: target, mapper: mapper}) do
{acc, subfun} = Collectable.into(target)
fun = fn
acc, {:cont, elem} -> subfun.(acc, {:cont, mapper.(elem)})
acc, ctrl -> subfun.(acc, ctrl)
end
{acc, fun}
end
end
But am I missing something here? Is there a built-in way to declare transformations to a collectable?
Marked As Solved
josevalim
Creator of Elixir
You are right, there is no out-of-the-box mechanism to add such a transformation.
Popular in Questions
What is the proper way to load a module from a file in to IEX?
In the python world, doing something like this pretty standard:
from ....
New
can someone please explain to me how Enum.reduce works with maps
New
I have an umbrella app.
Some of the apps inside depend on other apps in the umbrella, unsurprisingly.
I'm writing a test for one of t...
New
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
Hi all,
I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage.
I'm trying to use Postg...
New
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
Other popular topics
In Ruby, I can go:
User.find_by(email: "foobar@email.com").update(email: "hello@email.com")
How can I do something similar in Elixir? ...
New
Okay, I'm having a heck of a time trying to figure out how to best handle the validation of belongs_to associations in Ecto. I'm sure I'...
New
Hi there,
I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
Good day to you all.
I have been struggling to get a query involving like and ilike to work.
Can anyone assist me on this, please?
pro...
New
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
The Elixir Typespec docs show the following syntax for keyword lists in typespecs:
# ...
| [key: type] # keyword lis...
New
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....
New
I have a super simple question about elixir - how would I take a file like this
foo bar baz
and output a new file that enumerates th...
New







