Skip to content

Commit 320b82a

Browse files
committed
Rwerite the "Testing for Elements" section
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.
1 parent 87f2365 commit 320b82a

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

doc/Language/list.pod6

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,12 +152,24 @@ Since what C<for> receives is a single argument, it will be treated as a list of
152152
153153
=head1 Testing for Elements
154154
155-
To test for elements convert the C<List> or C<Array> to a L<C<Set>|/type/Set>
156-
or use a Set L<operator|/language/setbagmix>.
155+
To test for elements in a C<List> or C<Array>, you can use the
156+
L<"is element of"|https://docs.perl6.org/language/setbagmix#infix_(elem)>
157+
L<C<Set>|/type/Set> operator.
157158
158159
my @a = <foo bar buzz>;
159-
say @a.Set<bar buzz>; # OUTPUT: «(True True)␤»
160-
say so 'bar' ∈ @a; # OUTPUT: «True␤»
160+
say 'bar' (elem) @a; # OUTPUT: «True␤»
161+
say 'bar' ∈ @a; # same, using unicode version of operator
162+
163+
This is the equivalent of:
164+
165+
'bar' (elem) @a.Set; # convert the array to a Set first
166+
167+
except that, if possible, it won't actually do the conversion.
168+
169+
It basically compares the value with each element in the array using the
170+
L<=:=|/routine/=:=> infix operator. If you want to use another way to
171+
compare values, you probably should use
172+
L<first|/routine/first#(List)_routine_first>.
161173
162174
=head2 Sequences
163175

0 commit comments

Comments
 (0)