Skip to content

Commit

Permalink
Rwerite the "Testing for Elements" section
Browse files Browse the repository at this point in the history
It assumed that you have to convert something to a Set before being able
to use a set operator on it.  This is not true: set operators will take
care of conversion when needed.  In the shown example, it was actually
detrimental to first convert to a Set, because the (elem) operator can
short-circuit whenever it finds a match, so it won't have to examine all
values in the conceptual Set.

Also, in the example "so" was used: this is unnecessary, since (elem)
returns a full-blown Bool.

Also, refer to "first" if you would like to use different ways of comparison.
  • Loading branch information
lizmat committed Jul 17, 2018
1 parent 87f2365 commit 320b82a
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions doc/Language/list.pod6
Expand Up @@ -152,12 +152,24 @@ Since what C<for> receives is a single argument, it will be treated as a list of
=head1 Testing for Elements
To test for elements convert the C<List> or C<Array> to a L<C<Set>|/type/Set>
or use a Set L<operator|/language/setbagmix>.
To test for elements in a C<List> or C<Array>, you can use the
L<"is element of"|https://docs.perl6.org/language/setbagmix#infix_(elem)>
L<C<Set>|/type/Set> operator.
my @a = <foo bar buzz>;
say @a.Set<bar buzz>; # OUTPUT: «(True True)␤»
say so 'bar' ∈ @a; # OUTPUT: «True␤»
say 'bar' (elem) @a; # OUTPUT: «True␤»
say 'bar' ∈ @a; # same, using unicode version of operator
This is the equivalent of:
'bar' (elem) @a.Set; # convert the array to a Set first
except that, if possible, it won't actually do the conversion.
It basically compares the value with each element in the array using the
L<=:=|/routine/=:=> infix operator. If you want to use another way to
compare values, you probably should use
L<first|/routine/first#(List)_routine_first>.
=head2 Sequences
Expand Down

0 comments on commit 320b82a

Please sign in to comment.