Page 1 of 1

map, reduce, select, lookup, every, some

Posted: Tue Jul 16, 2013 5:28 am
by DarScott
The discussion related to filter moved to a logical variation of the command and digressed into the idea of selection (filtering) as part of a chunk expression.

Maybe that can be expanded to these:
  • Mapping (applying a unary function to each chunk)
    Reduction ("summing" the chunks using some arbitrary expression)
    Selection (as was discussed for filter)
    Lookup (as selection but only one is needed and it is returned)
    Every & sSome (special syntax for logical reduction for and and or)
These might apply to expressions, and once in a while I have give that thought.

Or it might be more xtalky for these to have their own commands.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 2:42 am
by mwieder
Hmmm... lookup seems error prone unless you can guarantee that there's only one item in the result set.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 2:48 am
by DarScott
Maybe we can use words such as "undefined" instead of "error". But maybe it can be defined as "first". And if this applies to elements, order can be defined.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 2:53 am
by monte
Arguably it's also redundant too because you could just get line one of the result of a selection/filter.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 3:09 am
by mwieder
"line one" is different from "only one line" in the result set...

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 3:29 am
by monte
right but if you are selecting something unique you get one line and if you aren't then who's fault is that?

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 3:40 am
by mwieder
That's exactly the question I'm raising about this. The answer is probably "it depends". If you specify the filter incorrectly and you still only get one line back from a lookup call, is the call doing the right thing? How are you going to know that there were actually more possibilities?

It's like asking "what's the square root of four?" and expecting only one answer.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 4:19 am
by DarScott
the square root of four
a square root of four
the square roots of four
...

I guess a possible difference between lookup and select (filter) might be that select might return a list, but lookup never does.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 4:31 am
by DarScott
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.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 2:57 pm
by edgore
Lookup does seem a little redundant for a core thing. You can still filter down to however many matching entries there are in a list using Select, then if you only want a single entry returned write your own code based on the criteria for you have for returning a single entry (put line 1, put line random(the number of lines), whatever) that seems like it would meet every need I could think of, without adding syntax.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 6:14 pm
by DarScott
I agree with the redundancy of lookup.

As for a random line or item, we have 'any' which I think will work.

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 6:39 pm
by edgore
"As for a random line or item, we have 'any' which I think will work."

Huh, how did I miss the existence of "any" all these years...

Re: map, reduce, select, lookup, every, some

Posted: Thu Jul 18, 2013 6:52 pm
by DarScott
It is because of the established meaning of 'any' that I have been using 'some' and other words when thinking about logical reduction.

Code: Select all

if every line of x is greater than some item of y then

Re: map, reduce, select, lookup, every, some

Posted: Mon Mar 02, 2015 10:02 pm
by Vanceone
Did any of these fantastic ideas get added? I'd love to have a map, filter, and reduce functions. By that I mean where we can take a list or array and apply an arbitrary function to them. Currently we have filter, which while nice isn't filtering an array according to the results of an arbitrary function. Nor do we have map or reduce.

Could we get them added somehow?