I have an array of objects and an array of valid return values ββfor a specific method. How to reduce an array of objects only to those whose method in question returns a value in my array of valid values?
Now I have this:
my @allowed = grep { my $object = $_; my $returned = $object->method; grep { my $value = $_; $value eq $returned; } @acceptableValues; } @objects;
The problem is that this is a complex cycle that I would like to avoid. This program is designed to scale to arbitrary sizes, and I want to minimize the number of iterations performed.
What is the best way to do this?
You can convert accepted return values ββto hash
my %values = map { $_ => 1 } @acceptedValues;
grep , grep:
grep
my @allowed = grep $values{ $_->method }, @objects;
, grep , , . , , . , , , . , , - .
, , . , , . 1, .
my %values; my @allowed; map {$values{$_}++} (@acceptableValues, @objects); for (keys %values) { push @allowed, $_ if $values{$_} > 1; }
Source: https://habr.com/ru/post/1016891/More articles:Ember fastboot works with http api host, but not with https one - javascriptDeploying matplotlib on heroku failed. How to do it right? - pythonUsing only part of a variable variable - openglReact + redux-form - redirect after submit - reactjsSimple SQL path query? - sqlHow to efficiently generate a list of primes in Perl 6? - perl6AWS load platform and socket.io - node.jsWhere is NuGet in VS2017 Community? - nugetHow to use template scope in vue jsx? - jsxhasRole always returns 403 - springAll Articles