View Source Colonel
Colonel is intended to provide functions that could be imagined in Kernel but aren't there.
Installation
If available in Hex, the package can be installed
by adding colonel
to your list of dependencies in mix.exs
:
def deps do
[
{:colonel, "~> 0.1.0"}
]
end
Documentation can be generated with ExDoc and published on HexDocs. Once published, the docs can be found at https://hexdocs.pm/colonel.
Ideas
Formatted Strings
Can we do anything to make :io_lib.format/2 more Elixir-friendly?
Then/Tap With/If
Frequently I find myself wanting to modify a value only if a condition is met. Often this means rebinding variables to conditional expressions.
"with" expressions are convenient for modifying values only if they meet a certain condition. "if" expressions require
an "else" to return values when the condition is not met. But neither is great for modifying values via piping with |>
.
Not without combining with then/2.
I like to imagine a construct that makes it easy to make conditional changes to a value in a pipeline.
Function Wrapper
I sometimes find myself wanting to build anonymous functions that return already-known values. Often these functions are 0-arity, but occasionally they take arguments.
I'm imagining a function that does something like this:
fn_wrap({:ok, value})
#=> fn -> {:ok, value} end
fn_wrap([a, b, c], 2)
#=> fn _, _ -> [a, b, c] end
Non-Integer Ranges
Ranges are great for testing inclusion and generating series, but the built-in ones only support integers. It would be cool to have ranges that could support any orderable and/or incrementable values, such as floats or dates.
Local Accumulators
This one would be more an exercise to see if I could do it than something I personally want, but could the proposed local accumulator functionality be provided by a library instead of being built into the language?
Pipe-Friendly Flat Map Reduce
I see Enum.flat_map_reduce/3 as one of the most powerful constructs for working through enumerables, but if your acc is very complicated it can get unwieldy.
Would there be some structure into which we could reduce to provide a similar functionality? I'm picturing something like this:
acc =
for value <- my_enumerable, reduce: Acc.new(a: %{}, b: 0) do
acc ->
acc
|> Acc.yield(value * 2)
|> Acc.update(:a, &Map.put(&1, value, acc[:b]))
|> Acc.update(:b, & &1 + value)
end
# get list
Acc.yielded(acc)
# get accumulated value
acc[:a]