collection->foreach(func)

33 views
Skip to first unread message

Ernie Rael

unread,
Mar 13, 2023, 7:48:44 PM3/13/23
to vim...@googlegroups.com
I sometimes want "collection->foreach((_, v) => ...v...)", a simple
one liner, no return or return ignored. I know it's not a performance
winner, or I'd want it more.

After a quick look at the code, it looks like simply introducing
another filtermap_T, FILTERMAP_FOREACH, and there's not much coding
needed (I only looked at list). Probably more work doing tests/doc.

Any objections or other considerations?

-ernie

Bram Moolenaar

unread,
Mar 14, 2023, 1:24:01 PM3/14/23
to vim...@googlegroups.com, Ernie Rael
So, this would iterate over the items in a List or Dictionary and invoke
a function for each one. I assume the List or Dictionary is not
modified, otherwise you would use map(). Thus it's a short version of a
for loop. This seems useful.

I would suggest first writing the help and think of any error conditions
that need to be handled. Perhaps the collection should be locked
(frozen) to avoid trouble?

--
This sentence is not sure that it exists, but if it does, it will
certainly consider the possibility that other sentences exist.

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// \\\
\\\ sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Marvin Renich

unread,
Mar 14, 2023, 3:45:22 PM3/14/23
to vim...@googlegroups.com, Ernie Rael
* Bram Moolenaar <Br...@moolenaar.net> [230314 13:24]:
>
> Ernie Rael wrote:
>
> > I sometimes want "collection->foreach((_, v) => ...v...)", a simple
> > one liner, no return or return ignored. I know it's not a performance
> > winner, or I'd want it more.
> >
> > After a quick look at the code, it looks like simply introducing
> > another filtermap_T, FILTERMAP_FOREACH, and there's not much coding
> > needed (I only looked at list). Probably more work doing tests/doc.
> >
> > Any objections or other considerations?
>
> So, this would iterate over the items in a List or Dictionary and invoke
> a function for each one. I assume the List or Dictionary is not
> modified, otherwise you would use map(). Thus it's a short version of a
> for loop. This seems useful.
>
> I would suggest first writing the help and think of any error conditions
> that need to be handled. Perhaps the collection should be locked
> (frozen) to avoid trouble?

Would this have any functionality that is not provided by using mapnew
and ignoring or throwing away the result?

...Marvin

Ernie Rael

unread,
Mar 15, 2023, 1:15:34 AM3/15/23
to vim...@googlegroups.com
All the existing functions, map/mapnew/filter, require a return value;
that makes it impossible to have a one line lambda with a rhs that isn't
a value. And requiring a new list to be built is painful.

-ernie

> ...Marvin
>

Ernie Rael

unread,
Mar 15, 2023, 1:39:30 AM3/15/23
to vim...@googlegroups.com
On 23/03/14 10:23 AM, Bram Moolenaar wrote:
> Ernie Rael wrote:
>
>> I sometimes want "collection->foreach((_, v) => ...v...)", a simple
>> one liner, no return or return ignored. I know it's not a performance
>> winner, or I'd want it more.
>>
>> After a quick look at the code, it looks like simply introducing
>> another filtermap_T, FILTERMAP_FOREACH, and there's not much coding
>> needed (I only looked at list). Probably more work doing tests/doc.
>>
>> Any objections or other considerations?
> So, this would iterate over the items in a List or Dictionary and invoke
> a function for each one. I assume the List or Dictionary is not
> modified, otherwise you would use map().
Yes, yes. Not modified as though `lockvar 1`, a structured value, like
an inner list in a list of lists, could be modified.
> Thus it's a short version of a
> for loop. This seems useful.
>
> I would suggest first writing the help and think of any error conditions
> that need to be handled.
The first PR will be doc only.
> Perhaps the collection should be locked
> (frozen) to avoid trouble?

To protect against the script changing the list/dict top level structure
during iteration? Aren't map/filter/mapnew susceptible to the script
manipulating the source collection during iteration?

But it's always nice to get an exception if the script misbehaves. In
this case, there can be no structure modification, unlike map/filter. I
suppose with map/filter the list could be locked during the operation
except when the return from the function indicates a change needs to be
made (I've never looked at the lock code; have no idea of the overhead
of an internal `lockvar 1`)

-ernie

>

Bram Moolenaar

unread,
Mar 22, 2023, 12:41:56 PM3/22/23
to vim...@googlegroups.com, Marvin Renich, Ernie Rael

[resend - message was rejected somewhere]

> * Bram Moolenaar <Br...@moolenaar.net> [230314 13:24]:
> >
> > Ernie Rael wrote:
> >
> > > I sometimes want "collection->foreach((_, v) => ...v...)", a simple
> > > one liner, no return or return ignored. I know it's not a performance
> > > winner, or I'd want it more.
> > >
> > > After a quick look at the code, it looks like simply introducing
> > > another filtermap_T, FILTERMAP_FOREACH, and there's not much coding
> > > needed (I only looked at list). Probably more work doing tests/doc.
> > >
> > > Any objections or other considerations?
> >
> > So, this would iterate over the items in a List or Dictionary and invoke
> > a function for each one. I assume the List or Dictionary is not
> > modified, otherwise you would use map(). Thus it's a short version of a
> > for loop. This seems useful.
> >
> > I would suggest first writing the help and think of any error conditions
> > that need to be handled. Perhaps the collection should be locked
> > (frozen) to avoid trouble?
>
> Would this have any functionality that is not provided by using mapnew
> and ignoring or throwing away the result?

It is not adding functionality, it is restricting functionality.
Using mapnew() suggests that the list/dict is going to be modified, only
by taking care it would be avoided. And it adds overhead, a new
list/dict is created and items added to it.

--
FATHER: One day, lad, all this will be yours ...
PRINCE: What - the curtains?
"Monty Python and the Holy Grail" PYTHON (MONTY) PICTURES LTD
Reply all
Reply to author
Forward
0 new messages