Max Alekseyev on Fri, 17 May 2024 19:00:41 +0200


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: apply / select are inconsistent on Maps


A couple more requests along the same lines.

Currently, parselect() / parapply() do not work on Lists (see example below) while regular select() / apply() do. Can it be fixed?

Also, is it possible to have a combined (par)selectapply function that will select elements based on one function and apply another function to those selected? Something like:

selectapply( selectfunction, applyfunction, container )

Thanks,
Max

===
? L = List(vector(10,i,i))
%1 = List([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])

? select(x->x%2,L)
%2 = List([1, 3, 5, 7, 9])

? parselect(x->x%2,L)
  ***   at top-level: parselect(x->x%2,L)
  ***                 ^-------------------
  *** parselect: incorrect type in parselect (t_LIST).
===


On Fri, May 10, 2024 at 3:11 PM Max Alekseyev <maxale@gmail.com> wrote:
PS. A typo correction - I meant to say:
However, there is an issue that t_MAP is an *unordered* object, while t_VEC is an ordered one, so perhaps it should return a set (a sorted t_VEC) to make the output well-defined.

On Fri, May 10, 2024 at 3:09 PM Max Alekseyev <maxale@gmail.com> wrote:
Hi Bill,

I trust your best judgement on fitting the requests into the current framework. Hence I sometimes formulate them sketchy.
Anyway, since you requested specifics - here are some thoughts:

I think the default behavior of select() should be returning the object of the same type as its second argument. In the case of t_MAP it should be t_MAP. And the same applies for apply().
A hypothetical example:

===
? M=Map(); for(i=1,5,mapput(M,i,vector(i))); M
Map([1, [0]; 2, [0, 0]; 3, [0, 0, 0]; 4, [0, 0, 0, 0]; 5, [0, 0, 0, 0, 0]])

? select((x,y)->(x%2==1)&&(#y>1),M)
Map([3, [0, 0, 0]; 5, [0, 0, 0, 0, 0]])

? apply((x,y)->[x+1,y],M)
Map([2, [0]; 3, [0, 0]; 4, [0, 0, 0]; 5, [0, 0, 0, 0]; 6, [0, 0, 0, 0, 0]])
===

Obtaining t_VEC output from t_MAP would be a nice optional addition for apply() function, say with flag=1. However, there is an issue that t_MAP is an ordered object, while t_VEC is an ordered one, so perhaps it should return a set (a sorted t_VEC) to make the output well-defined:

? apply((x,y)->-x,M,1)
[-5, -4, -3, -2, -1]

Regards,
Max

On Fri, May 10, 2024 at 11:19 AM Bill Allombert <Bill.Allombert@math.u-bordeaux.fr> wrote:
On Thu, May 09, 2024 at 01:10:57PM -0400, Max Alekseyev wrote:
> Support for binary function would be most general and helpful.

This is not a precise enough specification.
When suggesting an improvement, consider writing the documentation
and give a full example.

Otherwise we are required to guess what you need.

For example should select(f,t_MAP) return a t_MAP or a t_VEC ?

Cheers,
Bill.