Hi
While doing advent of code I ended up with some functions I used often. Maybe some of these could be candiates for the standard library. Let me know if you like any and I can make PRs, thanks.
list.index_fold
Like fold, but also passing the index
list.try_fold
Like fold, but takes a function that returns Result(acc, acc)
.
If the result is Ok, it keeps folding, If Err it returns the acc immediately.
Useful when you want to stop folding early.
list.map2
Like map2 here https://package.elm-lang.org/packages/elm/core/latest/List#map2
list.rotate
Moves elements from one side of the list to the other.
E.g.
rotate([1,2,3,4,5], 2) == [3,4,5,1,2]
list.windows
Returns a list with tuples of contiguous elements.
Useful for doing operations that need to know the previous element.
E.g.
`windows([1,2,3,4,5]) == [(1,2), (2,3), (3,4), (4,5)]
int.to_binary
Converts an Int to a binary representation. I'm using Bool.
e.g. to_binary(12) == [True,True,False,False]
Would be better to have some binary literal however e.g. 0b1100
int.from_binary
The opposite of to_binary
result.replace_error
Many functions in the standard library return Result(a, Nil)
. But most of the time I want Result(a, error)
. So I ended up with a replace_error
function. map_error
is too verbose in this case.
e.g.
input
|> list.map(parse_line)
|> result.all
|> replace_error("Could not parse lines")
Thanks!