"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?

You are right, there is no out-of-the-box mechanism to add such a transformation.

Alright, thank you!