Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repeated macro #1000

Merged
merged 1 commit into from
Feb 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions macros/core/PGauxiliaryFunctions.pl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ =head1 DESCRIPTION
random_pairwise_coprime($ar1, $ar2, ... )
random_coprime($ar1, $ar2, ... )
random_subset($n, @set)
repeated(@list)
=cut

# ^uses loadMacros
Expand Down Expand Up @@ -555,5 +556,32 @@ sub random_subset {
return wantarray ? @out : \@out;
}

=head2 repeated function

Usage: C<repeated(@list)>

This function returns a list of every repeated elements in @list. Comparison is made
using ==, so two elements may be considered 'repeated' even when they are not literally
equal.

Note that the function will return () if there are no repeated elements, which
is false as a boolean. So !repeated(@list) is a way to check if the list has no repeated
values.

Also note that generally if two items are equivalent, both will be in the returned list.
However occasionally with MathObjects, x == y is true while y == x is false, and then
only x (the one that makes the relation true when it is on the left) will be included
in the returned list.

=cut

sub repeated {
my @return;
for my $x (@_) {
push(@return,$x) if (grep { $_ == $x } (@_)) > 1;
Comment on lines +580 to +581
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is now failing perltidy tests here, there is a misplaced space: push(@return, $x) if (grep { $_ == $x } (@_)) > 1;.

}
return @return;
}

# return 1 so that this file can be included with require
1