Raku / problem-solving

🦋 Problem Solving, a repo for handling problems that require review, deliberation and possibly debate
Artistic License 2.0
70 stars 16 forks source link

Add :k option to min and max #367

Closed 0rir closed 1 year ago

0rir commented 1 year ago

This is feature request for the addition of a :k option for appropriate types; map being the model.

lizmat commented 1 year ago

If I understand it correctly, you want it to produce the ordinal numbers of any matches?

Could you give some code examples and expected result?

jnthn commented 1 year ago

I figure it's about the list case of min, so [4,1,2,-6,10].min(:k) would evaluate to 3, the index of the minimum element. While it's not mentioned, I imagine :kv and :p are implied by this request too.

0rir commented 1 year ago

@lizmat, @jnthn, you both are entirely correct in what you say. I would have done better to reference @a.first( @a.min) as :end is also applicable.

lizmat commented 1 year ago

And I guess if there are multiple minimum / maximum values, it would produce multiple indices.

@0rir I'm not sure how :end comes into play here?

jnthn commented 1 year ago

And I guess if there are multiple minimum / maximum values, it would produce multiple indices.

Only if min/max have an option to do that already; otherwise, I'd keep it consistent with the value result.

0rir commented 1 year ago

My thinking was to keep the return to the value element, the index or both in a Pair.

0racle commented 1 year ago

Putting my hand up as someone who has wanted min/max with :k.

The adverbs named args should be similar to first, ie. :v just returns the value (default), :k returns the index, :kv returns a Seq of the index and value, and :p returns a Pair of the index and value.

lizmat commented 1 year ago

So, what's wrong with minpairs / maxpairs ?

lizmat commented 1 year ago

Implemented .min / .max with :k, :v, :kv and :p adverbs with https://github.com/rakudo/rakudo/commit/ec0007729d

2colours commented 1 year ago

So, what's wrong with minpairs / maxpairs ?

So that these arguments brought up on IRC are preserved.

The "wrong" with them is that

  1. they are yet another ad-hoc thing to remember/look up
  2. they do two things at once that could make sense on their own as well: retrieve all the extreme values, PLUS retrieve their position/key

Please keep in mind that since a custom predicate could be used for determining extreme values, there could be different values that are all the minimums/maximums of a certain list of values and hence retrieving not just one but all of them at once would surely make sense on its own as well.

At which point I would ask: actually, why not have a flag for min and max for retrieving all minimum/maximum values? In which case minpairs and maxpairs would immediately stand out as the ad-hoc redundant alternative.

lizmat commented 1 year ago

Yesterday I've implemented :k, :v, :kv and :p on .min and .max.

:v is effectively: give me all minimum / maximum values.