As much as I would love to use expressions, commands are important to the style of LiveCode programmers. And we have commands
sort and
filter. So a command approach can be explored.
The first,
sort, already uses an "each expression" and perhaps soon
filter will, too. We can also add a choice between lines and items as sort has.
The simplified syntax might be like this:
Code: Select all
sort [{lines | items} of] <container> … [by <eachExpression>]
filter [{lines | items} of] <container> where <eachExpression>
filter [{lines | items} of] <container> {with | without} <regEx>
We can expand that to
map:
Code: Select all
map [{lines | items} of] <container> with <eachExpression>
That would change the list to be that made of the eachExpression evaluated for each chunk of the original list. Or we can move parameters around and call it
apply.
Reduction is harder. We need two parameters in the each expression, not just
each. We can call them left and right. There is a potential that they can be confused with other lefts and rights, so that needs to be reviewed. Maybe something like this:
Code: Select all
reduce [{lines | items} of] <container> … with <leftRightExpression
An example might be this:
Code: Select all
put "1,2,4,8" into x
reduce items of x with left + right
We expect this to add them up and leave 15 in x. The keywords left and right can be interpreted as parameter variables that are applied to elements.
Looking closer we need to specify the identity and the direction. The identity for addition is 0 and in LiveCode we can use empty. The direction does not matter for addition. So the … in the syntax above can specify those two.
And for
lookup
Code: Select all
lookup [{lines | items} of] <container> using <eachExpression>
That reads funny.
Maybe that should be required to return only one chunk. It might be first, undefined or any (random). Or maybe 'first' or 'last' could be part of the command.
It is possible to have a logical reduction command, but that seems goofy and this email is long.