Proposal: Allow an optional final separator for (joined:)
A very common task is to render a list of items in a natural English idiom, e.g. "A, B, C and D", but no simple solution exists to do this. While (joined:)
can join an array, it only uses a single separator.
One possible solution is to remove the final item before joining, e.g.
(set: _meals to (a: "Breakfast", "Toast", "Dinner"))
(set: _rest to (subarray: _meals, 1, _meals's length - 1))
(print: (joined: ", ", ..._rest) + " and " + _meals's last)
However this is awkward to do frequently. Allowing (joined:)
an optional final separator would make this much easier. e.g.
(print: (joined: ", " " and ", ..._meals))
Here both separators are before the comma, to distinguish them from the items to be joined, an alternative might be to supply an array of separators for the first argument?
(print: (joined: (a:", "," and "), ..._meals))
An alternative would be to give the lambda in (folded:)
access to the index of the item in the array it is processing, or some other keyword indicating that it is at the last item, so that it could use (cond:)
, e.g. something like:
(folded: _meal making _mealList via _mealList + (cond: islast, _meal + ", ", " and " + _meal), "", ..._meals)
However this solution would require extra work to cope with 1 item lists, as opposed to the joined solution.