Skip Menu |
 

This queue is for tickets about the List-MoreUtils CPAN distribution.

Report information
The Basics
Id: 122878
Status: rejected
Priority: 0/
Queue: List-MoreUtils

People
Owner: Nobody in particular
Requestors: EDAVIS [...] cpan.org
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



Subject: wishlist: is_uniq, assert_uniq
Download (untitled) / with headers
text/plain 842b
Thanks for adding minmaxstr. Could I log another feature request? The uniq() function is handy but I often find myself using it as if (scalar(uniq(@a)) != scalar(@a)) { die "duplicates found in: @a"; } This is usually for 'cannot happen' cases in the code, where I am sure that @a will have unique values, but I add a check out of paranoia and to document my assumption. Using uniq for this is a lot more concise than building a %seen hash. However, it could be made more concise (and faster) if you could just test whether a list has only unique elements: die 'duplicates' unless is_uniq(@a); Finally, for the kind of assertion above it would be even nicer to have the library routine do the checking and throw an error itself: assert_uniq(@a); # 'values not unique: value XXX appears more than once'
Download (untitled) / with headers
text/plain 230b
In the same spirit is_all_same(@a) checks whether every element in @a is equal, which is equivalent to scalar(uniq(@a)) <= 1 but can be implemented more efficiently. P.S. assert_uniq(@a) could return the unique value found.
Download (untitled) / with headers
text/plain 1.1k
On Wed Aug 23 06:10:16 2017, EDAVIS wrote: Show quoted text
> Thanks for adding minmaxstr. Could I log another feature request? > > The uniq() function is handy but I often find myself using it as > > if (scalar(uniq(@a)) != scalar(@a)) { > die "duplicates found in: @a"; > } > > This is usually for 'cannot happen' cases in the code, where I am sure > that @a will have unique values, but I add a check out of paranoia and > to document my assumption. Using uniq for this is a lot more concise > than building a %seen hash. > > However, it could be made more concise (and faster) if you could just > test whether a list has only unique elements: > > die 'duplicates' unless is_uniq(@a);
How about die 'duplicates' if(duplicates(@a)); ? Show quoted text
> Finally, for the kind of assertion above it would be even nicer to > have the library routine do the checking and throw an error itself: > > assert_uniq(@a); > # 'values not unique: value XXX appears more than once'
This will consequently come with a assert_singleton, assert_duplicates, assert_... and is not clear in first place. Naming them "assert_all_uniq" or "assert_all_distinct", "assert_all_singleton", etc. I think you get that point ;) Best regards, Jens
Subject: RE: [rt.cpan.org #122878] wishlist: is_uniq, assert_uniq
Date: Wed, 23 Aug 2017 11:33:59 +0000
To: "bug-List-MoreUtils [...] rt.cpan.org" <bug-List-MoreUtils [...] rt.cpan.org>, "EDAVIS [...] cpan.org" <EDAVIS [...] cpan.org>
From: Ed Avis <eda [...] waniasset.com>
Download (untitled) / with headers
text/plain 1.3k
(replying by mail since CPAN website appears down; please ignore disclaimer at the end) I'm not worried about the name. Probably you are right that it's best not to call it 'uniq' because that already has too many overloaded meanings. (In C# UniqOrException() checks that there is just one single distinct value in the sequence. And uniq(1) depends on sorting first.) Perhaps to be as explicit as possible all_are_distinct(@a) # scalar(@a) == scalar(uniq(@a)) assert_all_are_distinct(@a) single_distinct_value_or_die(@a) but really I leave the names up to you. -- Ed Avis <eda@waniasset.com> This email is intended only for the person to whom it is addressed and may contain confidential information. Any retransmission, copying, disclosure or other use of, this information by persons other than the intended recipient is prohibited. If you received this email in error, please contact the sender and delete the material. This email is for information only and is not intended as an offer or solicitation for the purchase or sale of any financial instrument. Wadhwani Asset Management LLP is a Limited Liability Partnership registered in England (OC303168) with registered office at 9th Floor Orion House, 5 Upper St Martin’s Lane, London, WC2H 9EA. It is authorised and regulated by the Financial Conduct Authority.
Download (untitled) / with headers
text/plain 1.1k
On Wed Aug 23 07:51:39 2017, eda@waniasset.com wrote: Show quoted text
> (replying by mail since CPAN website appears down; please ignore > disclaimer at the end)
Aye! Show quoted text
> I'm not worried about the name. Probably you are right that it's best > not to call it 'uniq' because that already has too many overloaded > meanings. > (In C# UniqOrException() checks that there is just one single distinct > value in the sequence. And uniq(1) depends on sorting first.) > > Perhaps to be as explicit as possible > > all_are_distinct(@a) # scalar(@a) == scalar(uniq(@a))
As said, this is equivalent to scalar(duplicates @a) == 0. And it has a much smaller stack usage for returning nothing :D Adding a function (with a new stack frame, preserving locals and context) for just one compare (read it from my lips: one operation) is probably slightly overkill. Maybe Sub::Quote can be the knight in shining armour for this particular situation? Show quoted text
> assert_all_are_distinct(@a) > single_distinct_value_or_die(@a) > > but really I leave the names up to you.
I don't think assertions shall belong to List::MoreUtils. I will support you creating a List::Assertions with all those handy functions. Best regards, Jens


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

Please report any issues with rt.cpan.org to rt-cpan-admin@bestpractical.com.