Like all For statements, the really excellent For Each action's only limitation is that it's forgetful.
(The burden is on the user to set up variables and take care of capturing and storing any values created in each iteration)
A Map expression is a less forgetful older sister of a For statement. It automatically returns, for free, a collection (e.g. a series of lines) holding the whole series of values generated across all iterations.
Users could snap macros together a bit more rapidly, and be spared the work of:
- setting up variables,
- specifiying accumulation actions,
- inventing and debugging conditional defences against edge-case ambiguities
If the For Each action had an option to simply Map from an input collection to an output collection (from a set of Windows to a series of lines in a variable, for example) by applying some transformation/function to each successive value of the FOR EACH variable.
Example – saving and restoring a set of window tilings:
Pleased with a tiling arrangement for all my TaskPaper windows, I wanted to save the whole arrangement, for restoration tomorrow.
I needed to map (window -> line) over the collection of windows,
As a For Each statement this requires the introduction and management of an accumulator variable.
(Management ? Well, forgive my slowness, but in practice I had to experiment with when to introduce the separating newline, given that the first case is an assignment, but subsequent cases are appends. In my first attempt, I ended up with an unwanted blank line at the start of the result.
Trivial ? Perhaps – depends on how we restore the tilings later. In any case, it detained me ... whereas a map option would spare me from having to test and think )
For Each version:
As a map ?
- The user doesn't have to specify an accumulator value, or
- specify the mechanics of initialising, appending, or handling edge conditions at start and end.
- The whole derived collection (list of lines) is automatically returned (to a variable, clipboard, etc)
Map value returned:
Weekly.taskpaper • Home,501,22,427,1578 Waiting.taskpaper • Home,853,1074,853,526 Daily.taskpaper • Home,853,22,853,1052 Monthly.taskpaper • Home,1707,22,427,1578 Scratchpad.taskpaper • Home,2129,22,431,1578 Todo.taskpaper • Home,0,22,853,1578
For people like me, who still remember the 1950s, this is easier
( Pipe-lines can be snapped together rather simply when each component returns a value )
See, for example, the thoughtful notes and templates at:
Matthew McVickar - TaskPaper tiles