map, reduce, select, lookup, every, some

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, LCMark

Locked
DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

map, reduce, select, lookup, every, some

Post by DarScott » Tue Jul 16, 2013 5:28 am

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.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

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

Post by mwieder » Thu Jul 18, 2013 2:42 am

Hmmm... lookup seems error prone unless you can guarantee that there's only one item in the result set.

DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

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

Post by DarScott » Thu Jul 18, 2013 2:48 am

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.

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

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

Post by monte » Thu Jul 18, 2013 2:53 am

Arguably it's also redundant too because you could just get line one of the result of a selection/filter.
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

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

Post by mwieder » Thu Jul 18, 2013 3:09 am

"line one" is different from "only one line" in the result set...

monte
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1564
Joined: Fri Jan 13, 2012 1:47 am
Contact:

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

Post by monte » Thu Jul 18, 2013 3:29 am

right but if you are selecting something unique you get one line and if you aren't then who's fault is that?
LiveCode User Group on Facebook : http://FaceBook.com/groups/LiveCodeUsers/

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Location: Berkeley, CA, US
Contact:

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

Post by mwieder » Thu Jul 18, 2013 3:40 am

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.

DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

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

Post by DarScott » Thu Jul 18, 2013 4:19 am

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.

DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

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

Post by DarScott » Thu Jul 18, 2013 4:31 am

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.

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

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

Post by edgore » Thu Jul 18, 2013 2:57 pm

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.

DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

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

Post by DarScott » Thu Jul 18, 2013 6:14 pm

I agree with the redundancy of lookup.

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

edgore
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 197
Joined: Wed Jun 14, 2006 8:40 pm

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

Post by edgore » Thu Jul 18, 2013 6:39 pm

"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...

DarScott
Posts: 227
Joined: Fri Jul 28, 2006 12:23 am
Location: Albuquerque
Contact:

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

Post by DarScott » Thu Jul 18, 2013 6:52 pm

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

Vanceone
Posts: 37
Joined: Sat Dec 15, 2012 7:27 pm

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

Post by Vanceone » Mon Mar 02, 2015 10:02 pm

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?

Locked

Return to “Engine Contributors”