Closed ThomasWeiser closed 6 years ago
Hey @ThomasWeiser !
I think this is great. My only piece of feedback is that maybe there could be more examples. The functions are kind of tricky and its hard to figure out what they do by reading the one example in each description.
@Chadtech Thanks for the feedback!
I've added a diagram visualizing the operation and a second example to each of the two new functions.
Adding two functions
mapAccuml
andmapAccumr
that behave like a combination of map and foldl: Pass an accumulator through the list and map the elements using the accumulator value at that point.See this blog post for nice diagrams comparing
mapAccuml
withmap
,foldl
andscanl
.I took the names from the Haskell library, but with a lowercase
l
/r
at the end, following the Elm library folklore.One use case is using a running total of some sort when mapping the elements of a list.
A specific application of that use case that I stumbled upon are view functions, that use CSS grid-layout with items spanning across multiple cells. Say you have a model of three items, each with a specific column-span count:
The CSS grid spec demands that each item specifies the column number where it should start. If we adorn the list with that information it may look like this:
In other words, we need a running total for counting the columns already used. That's a perfect job for
mapAccuml
: