Today, if you want to pipe an append or a prepend, you must use then/2 to 
achieve this. This can be useful if the first elements of a list must be 
inserted in order through a pipe. Let me show an example:

dto.ledger.accounts
|> Enum.reject(fn %{account_number: acc_number} ->
acc_number in [debit_acc_number, credit_acc_number]
end)
|> then(fn accounts -> [get_ordered_debit_accounts() | accounts])
|> then(fn accounts -> [get_ordered_credit_accounts() | accounts] end)
|> then(fn accounts -> accounts ++ retrieve_unordered_accounts() end)
|> then(&Map.replace(dto.ledger, :accounts, &1))

Obviously, that one is too simple and can be done in one line without 
losing too much readability, like this:

dto.ledger.accounts
|> Enum.reject(fn %{account_number: acc_number} ->
acc_number in [debit_acc_number, credit_acc_number]
end)
|> then(fn accounts -> [get_ordered_debit_accounts(), 
get_ordered_credit_accounts() | accounts])
|> Kernel.++(retrieve_unordered_accounts())
|> then(&Map.replace(dto.ledger, :accounts, &1))

But it could be cleaner if it could just do this:

dto.ledger.accounts
|> Enum.reject(fn %{account_number: acc_number} ->
acc_number in [debit_acc_number, credit_acc_number]
end)
|> List.prepend(get_ordered_debit_accounts())
|> List.prepend(get_ordered_credit_accounts())
|> List.append(retrieve_unordered_accounts())
|> then(&Map.replace(dto.ledger, :accounts, &1))

So I think that the implementation of this can just perform a "remap" of 
the ++ operator and the [e | list] expression.

def append(list, element) when is_list(list), do: list ++ [element]
def prepend(list, element) when is_list(list), do: [element | list]


This feature is more of a syntactic sugar than something innovative; it 
would be a way to keep the code more vertical and easier to read for some.

*I will be glad to send a PR.*


-- 
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-core+unsubscr...@googlegroups.com.
To view this discussion visit 
https://groups.google.com/d/msgid/elixir-lang-core/ed3ccb00-5069-4b66-9d6f-132eadc7ea90n%40googlegroups.com.

Reply via email to