Padmark.Parser.Enum.Ext (padmark v1.4.49)
View SourceSome extensions of Enum functions
Summary
Functions
reduce_with_end is like Enum.reduce for lists, but the reducer function is called for
each element of the list with the tuple {:element, element} and the accumulator and once
more at the end with :end and the accumulator
Functions
reduce_with_end is like Enum.reduce for lists, but the reducer function is called for
each element of the list with the tuple {:element, element} and the accumulator and once
more at the end with :end and the accumulator
iex(1)> reducer =
...(1)> fn {:element, nil}, {partial, result} -> {[], [Enum.sum(partial)|result]}
...(1)> {:element, val}, {partial, result} -> {[val|partial], result}
...(1)> :end, {partial, result} -> [Enum.sum(partial)|result] |> Enum.reverse
...(1)> end
...(1)> [1, 2, nil, 4, 1, 0, nil, 3, 2, 2]
...(1)> |> reduce_with_end({[], []}, reducer)
[3, 5, 7]N.B. that in the treatment of :end we can change the shape of the accumulator w/o any
penalty concerning the complexity of the reducer function