/
Any.pod6
934 lines (573 loc) · 24.2 KB
/
Any.pod6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
=begin pod
=TITLE class Any
=SUBTITLE Thing/object
class Any is Mu {}
While L<Mu|/type/Mu> is the root of the Perl 6 class hierarchy, C<Any> is the class
that serves as a default base class for new classes, and as the base class for
most built-in classes.
Since Perl 6 intentionally confuses items and single-element lists, most
methods in C<Any> are also present on class L<List|/type/List>, and coerce to
List or a list-like type.
=head1 Methods
=head2 method ACCEPTS
Defined as:
multi method ACCEPTS(Any:D: Mu $other)
Usage:
=begin code :lang<pseudo>
EXPR.ACCEPTS(EXPR);
=end code
Returns C<True> if C<$other === self> (i.e. it checks object identity).
Many built-in types override this for more specific comparisons
=head2 method any
Defined as:
method any(--> Junction:D)
Interprets the invocant as a list and creates an
C<any>-L<Junction|/type/Junction> from it.
say so 2 == <1 2 3>.any; # OUTPUT: «True»
say so 5 == <1 2 3>.any; # OUTPUT: «False»
=head2 method all
Defined as:
method all(--> Junction:D)
Interprets the invocant as a list and creates an
C<all>-L<Junction|/type/Junction> from it.
say so 1 < <2 3 4>.all; # OUTPUT: «True»
say so 3 < <2 3 4>.all; # OUTPUT: «False»
=head2 method one
Defined as:
method one(--> Junction:D)
Interprets the invocant as a list and creates a
C<one>-L<Junction|/type/Junction> from it.
say so 1 == (1, 2, 3).one; # OUTPUT: «True»
say so 1 == (1, 2, 1).one; # OUTPUT: «False»
=head2 method none
Defined as:
method none(--> Junction:D)
Interprets the invocant as a list and creates a
C<none>-L<Junction|/type/Junction> from it.
say so 1 == (1, 2, 3).none; # OUTPUT: «False»
say so 4 == (1, 2, 3).none; # OUTPUT: «True»
=head2 method list
Defined as:
method list(--> List:D)
Interprets the invocant as a list, and returns that L<List|/type/List>.
say 42.list.^name; # OUTPUT: «List»
say 42.list.elems; # OUTPUT: «1»
=head2 method push
Defined as:
method push(|values --> Positional:D)
The method push is defined for undefined invocants and allows for
autovivifying undefined to an empty C<Array>, unless the undefined value
implements C<Positional> already. The argument provided will then be pushed
into the newly created Array.
my %h;
say %h<a>; # OUTPUT: «(Any)» <-- Undefined
%h<a>.push(1); # .push on Any
say %h; # OUTPUT: «{a => [1]}» <-- Note the Array
=head2 routine reverse
Defined as:
multi sub reverse(*@list --> Seq:D)
multi method reverse(List:D: --> Seq:D)
Returns a L«C<Seq>|/type/Seq» with the same elements in reverse order.
Note that C<reverse> always refers to reversing elements of a list;
to reverse the characters in a string, use L<flip>.
Examples:
say <hello world!>.reverse; # OUTPUT: «(world! hello)»
say reverse ^10; # OUTPUT: «(9 8 7 6 5 4 3 2 1 0)»
=head2 method sort
Defined as:
multi method sort()
multi method sort(&custom-routine-to-use)
Sorts iterables with C<infix:<cmp>> or given code object and returns a new C<List>.
Optionally, takes a L<Callable> as a positional parameter, specifying how to
sort.
Examples:
say <b c a>.sort; # OUTPUT: «(a b c)»
say 'bca'.comb.sort.join; # OUTPUT: «abc»
say 'bca'.comb.sort({$^b cmp $^a}).join; # OUTPUT: «cba»
say '231'.comb.sort(&infix:«<=>»).join; # OUTPUT: «123»
=head2 method map
Defined as:
multi method map(\SELF: █; :$label, :$item)
C<map> will iterate over the invocant and apply the number of positional
parameters of the code object from the invocant per call. The returned values
of the code object will become elements of the returned C<Seq>.
The C<:$label> and C<:$item> are useful only internally, since C<for> loops
get converted to C<map>s. The C<:$label> takes an existing C<Label> to label
the C<.map>'s loop with and C<:$item> controls whether the iteration will
occur over C<(SELF,)> (if C<:$item> is set) or C<SELF>.
=head2 method deepmap
Defined as:
method deepmap(&block --> List) is nodal
C<deepmap> will apply C<&block> to each element and return a new C<List> with
the return values of C<&block>, unless the element does the C<Iterable> role.
For those elements C<deepmap> will descend recursively into the sublist.
say [[1,2,3],[[4,5],6,7]].deepmap(* + 1);
# OUTPUT: «[[2 3 4] [[5 6] 7 8]]»
=head2 method duckmap
Defined as:
method duckmap(&block) is rw is nodal
C<duckmap> will apply C<&block> on each element and return a new list with
defined return values of the block. For undefined return values, C<duckmap>
will try to descend into the element if that element implements C<Iterable>.
<a b c d e f g>.duckmap(-> $_ where <c d e>.any { .uc }).say;
# OUTPUT: «(a b C D E f g)»
(('d', 'e'), 'f').duckmap(-> $_ where <e f>.any { .uc }).say;
# OUTPUT: «((d E) F)»
=head2 method nodemap
Defined as:
method nodemap(&block --> List) is nodal
C<nodemap> will apply C<&block> to each element and return a new L<List> with
the return values of C<&block>. In contrast to L<deepmap> it will B<not> descend
recursively into sublists if it finds elements which L<does> the L<Iterable> role.
say [[1,2,3], [[4,5],6,7], 7].nodemap(*+1);
# OUTPUT: «(4, 4, 8)»
say [[2, 3], [4, [5, 6]]]».nodemap(*+1)
# OUTPUT: «((3 4) (5 3))»
The examples above would have produced the exact same results if we had used
L<map> instead of C<nodemap>. The difference between the two lies in the
fact that L<map> flattens out L<slips|/type/Slip> while C<nodemap> doesn't.
say [[2,3], [[4,5],6,7], 7].nodemap({.elems == 1 ?? $_ !! slip});
# OUTPUT: «(() () 7)»
say [[2,3], [[4,5],6,7], 7].map({.elems == 1 ?? $_ !! slip});
# OUTPUT: «(7)»
=head2 method flat
Defined as:
method flat(--> Seq:D) is nodal
Interprets the invocant as a list, flattens
L<non-containerized|/language/containers> L<Iterables|/type/Iterable>
into a flat list, and returns that list. Keep in mind L<Map> and
L<Hash> types are L<Iterable> and so will be flattened into lists
of pairs.
say ((1, 2), (3), %(:42a)); # OUTPUT: «((1 2) 3 {a => 42})»
say ((1, 2), (3), %(:42a)).flat; # OUTPUT: «(1 2 3 a => 42)»
Note that L<Arrays|/type/Array> containerize their elements by default, and so
C<flat> will not flatten them. You can use
L<hyper method call|/language/operators#index-entry-postfix_».> to call
L«C<.List>|/routine/List» method on all the inner L<Iterables|/type/Iterable>
and so de-containerize them, so that C<flat> can flatten them:
say [[1, 2, 3], [(4, 5), 6, 7]] .flat; # OUTPUT: «([1 2 3] [(4 5) 6 7])»
say [[1, 2, 3], [(4, 5), 6, 7]]».List.flat; # OUTPUT: «(1 2 3 4 5 6 7)»
For more fine-tuned options, see L«C<deepmap>|/routine/deepmap»,
L«C<duckmap>|/routine/duckmap», and
L<signature destructuring|/type/Signature#Destructuring_Parameters>
=head2 method eager
Defined as:
method eager(--> Seq:D) is nodal
Interprets the invocant as a C<List>, evaluates it eagerly, and returns that
C<List>.
my $range = 1..5;
say $range; # OUTPUT: «1..5»
say $range.eager; # OUTPUT: «(1 2 3 4 5)»
=head2 method elems
Defined as:
method elems(--> Int:D) is nodal
Interprets the invocant as a list, and returns the number of elements in the
list.
say 42.elems; # OUTPUT: «1»
say <a b c>.elems; # OUTPUT: «3»
=head2 method end
method end(--> Any:D) is nodal
Interprets the invocant as a list, and returns the last index of that list.
say 6.end; # OUTPUT: «0»
say <a b c>.end; # OUTPUT: «2»
=head2 method pairup
Defined as:
method pairup(--> Seq:D) is nodal
Interprets the invocant as a list, and constructs a list of
L<pairs|/type/Pair> from it, in the same way that assignment to a
L<Hash|/type/Hash> does. That is, it takes two consecutive elements and
constructs a pair from them, unless the item in the key position already is a
pair (in which case the pair is passed is passed through, and the next
list item, if any, is considered to be a key again).
say (a => 1, 'b', 'c').pairup.perl; # OUTPUT: «(:a(1), :b("c")).Seq»
=head2 sub exit
Defined as:
sub exit(Int() $status = 0)
Exits the current process with return code C<$status> or zero if no value has been specified.
The exit value (C<$status>), when different from zero, has to be opportunely evaluated from the
process that catches it (e.g., a shell).
C<exit> does prevent the L<LEAVE|/phasers#LEAVE> phaser to be executed.
C<exit> should be used as last resort only to signal the parent process about an exit code different from zero,
and should not be used to terminate exceptionally a method or a sub: use L<exceptions|/language/exceptions> instead.
=comment TODO maybe find a better place to document &exit
=head2 sub item
X<|$ (item contextualizer)>
Defined as:
proto sub item(|) is pure
multi item(\x)
multi item(|c)
multi item(Mu $a)
Forces given object to be evaluated in item context and returns the value of it.
say item([1,2,3]).perl; # OUTPUT: «$[1, 2, 3]»
say item( %( apple => 10 ) ).perl; # OUTPUT: «${:apple(10)}»
say item("abc").perl; # OUTPUT: «"abc"»
You can also use C<$> as item contextualizer.
say $[1,2,3].perl; # OUTPUT: «$[1, 2, 3]»
say $("abc").perl; # OUTPUT: «"abc"»
=head2 method Array
Defined as:
method Array(--> Array:D) is nodal
Coerce the invocant to L<Array|/type/Array>.
=head2 method List
Defined as:
method List(--> List:D) is nodal
Coerce the invocant to L<List|/type/List>.
=head2 method Hash
Defined as:
method Hash(--> Hash:D) is nodal
Coerce the invocant to L<Hash|/type/Hash>.
=head2 method hash
Defined as:
method hash(--> Hash:D) is nodal
Coerce the invocant to L<Hash|/type/Hash>.
=head2 method Slip
Defined as:
method Slip(--> Slip:D) is nodal
Coerce the invocant to L<Slip|/type/Slip>.
=head2 method Map
Defined as:
method Map(--> Map:D) is nodal
Coerce the invocant to L<Map|/type/Map>.
=head2 method Bag
Defined as:
method Bag(--> Bag:D) is nodal
Coerce the invocant to L<Bag|/type/Bag>, whereby C<Positionals> are treated as
lists of values.
=head2 method BagHash
Defined as:
method BagHash(--> BagHash:D) is nodal
Coerce the invocant to L<BagHash|/type/BagHash>, whereby C<Positionals> are
treated as lists of values.
=head2 method Set
Defined as:
method Set(--> Set:D) is nodal
Coerce the invocant to L<Set|/type/Set>, whereby C<Positionals> are treated as
lists of values.
=head2 method SetHash
Defined as:
method SetHash(--> SetHash:D) is nodal
Coerce the invocant to L<SetHash|/type/SetHash>, whereby C<Positionals> are
treated as lists of values.
=head2 method Mix
Defined as:
method Mix(--> Mix:D) is nodal
Coerce the invocant to L<Mix|/type/Mix>, whereby C<Positionals> are treated as
lists of values.
=head2 method MixHash
Defined as:
method MixHash(--> MixHash:D) is nodal
Coerce the invocant to L<MixHash|/type/MixHash>, whereby C<Positionals> are
treated as lists of values.
=head2 method Supply
Defined as:
method Supply(--> Supply:D) is nodal
Coerce the invocant first to a C<List> and then to a L<Supply|/type/Supply>.
=head2 method min
Defined As:
multi method min(--> Any:D)
multi method min(&filter --> Any:D)
Coerces to Iterable and returns the numerically smallest element.
If a C<Callable> positional argument is provided, each value is passed
into the filter, and its return value is compared instead of the
original value. The original value is still the one returned from C<min>.
say (1,7,3).min(); # OUTPUT:«1»
say (1,7,3).min({1/$_}); # OUTPUT:«7»
=head2 method max
Defined As:
multi method max(--> Any:D)
multi method max(&filter --> Any:D)
Coerces to Iterable and returns the numerically largest element.
If a C<Callable> positional argument is provided, each value is passed
into the filter, and its return value is compared instead of the
original value. The original value is still the one returned from C<max>.
say (1,7,3).max(); # OUTPUT:«7»
say (1,7,3).max({1/$_}); # OUTPUT:«1»
=head2 method minmax
Defined As:
multi method minmax(--> Range:D)
multi method minmax(&filter --> Range:D)
Returns a Range from the smallest to the largest element.
If a C<Callable> positional argument is provided, each value is passed
into the filter, and its return value is compared instead of the
original value. The original values are still used in the returned
Range.
say (1,7,3).minmax(); # OUTPUT:«1..7»
say (1,7,3).minmax({-$_}); # OUTPUT:«7..1»
=head2 method minpairs
Defined As:
multi method minpairs(Any:D: --> Seq:D)
Calls L«C<.pairs>|/routine/pairs» and returns a L«C<Seq>|/type/Seq» with
all of the Pairs with minimum values, as judged by the
L«C<cmp> operator|/routine/cmp»:
<a b c a b c>.minpairs.perl.put; # OUTPUT: «(0 => "a", 3 => "a").Seq»
%(:42a, :75b).minpairs.perl.put; # OUTPUT: «(:a(42),).Seq»
=head2 method maxpairs
Defined As:
multi method maxpairs(Any:D: --> Seq:D)
Calls L«C<.pairs>|/routine/pairs» and returns a L«C<Seq>|/type/Seq» with
all of the Pairs with maximum values, as judged by the
L«C<cmp> operator|/routine/cmp»:
<a b c a b c>.maxpairs.perl.put; # OUTPUT: «(2 => "c", 5 => "c").Seq»
%(:42a, :75b).maxpairs.perl.put; # OUTPUT: «(:b(75),).Seq»
=head2 method keys
Defined As:
multi method keys(Any:U: --> List)
multi method keys(Any:D: --> List)
For defined Any returns its keys, otherwise returns an empty list.
say Any.keys; # OUTPUT: «()»
=head2 method flatmap
Defined As:
method flatmap(Any:U: &code --> Seq)
Coerces the C<Any> to a C<list> by applying the C<.list> method and uses
L«C<List.flatmap>|/type/List#method_flatmap» on it.
say Any.flatmap({.reverse}); # OUTPUT: «((Any))»
In the case of C<Any>, C<Any.list> returns a 1-item list, as is shown.
=head2 method roll
Defined As:
multi method roll(--> Any)
multi method roll($n --> Seq)
Coerces the C<Any> to a C<list> by applying the C<.list> method and uses
L«C<List.roll>|/type/List#routine_roll» on it.
say Any.roll; # OUTPUT: «(Any)»
say Any.roll(5); # OUTPUT: «((Any) (Any) (Any) (Any) (Any))»
=head2 method pick
Defined As:
multi method pick(--> Any)
multi method pick($n --> Seq)
Coerces the C<Any> to a C<list> by applying the C<.list> method and uses
L«C<List.pick>|/type/List#routine_pick» on it.
say Any.pick; # OUTPUT: «(Any)»
say Any.pick(5); # OUTPUT: «((Any))»
=head2 method skip
Defined As:
multi method skip(--> Seq)
multi method skip($n --> Seq)
Creates a Seq from 1-item list's iterator and uses
L«C<Seq.skip>|/type/List#method_skip» on it.
say Any.skip; # OUTPUT: «()»
say Any.skip(5); # OUTPUT: «()»
say Any.skip(-1); # OUTPUT: «((Any))»
say Any.skip(*-1); # OUTPUT: «((Any))»
=head2 method prepend
Defined As:
multi method prepend(--> Array)
multi method prepend(@values --> Array)
Initializes Any variable as empty Array and calls
L«C<Array.prepend>|/type/Array#method_prepend» on it.
my $a;
say $a.prepend; # OUTPUT: «[]»
say $a; # OUTPUT: «[]»
my $b;
say $b.prepend(1,2,3); # OUTPUT: «[1 2 3]»
=head2 method unshift
Defined As:
multi method unshift(--> Array)
multi method unshift(@values --> Array)
Initializes Any variable as empty Array and calls
L«C<Array.unshift>|/type/Array#routine_unshift» on it.
my $a;
say $a.unshift; # OUTPUT: «[]»
say $a; # OUTPUT: «[]»
my $b;
say $b.unshift([1,2,3]); # OUTPUT: «[[1 2 3]]»
=head2 method first
Defined As:
method first(Mu $matcher?, :$k, :$kv, :$p, :$end)
Treats the C<Any> as a 1-item list and uses
L«C<List.first>|/type/List#routine_first» on it.
say Any.first; # OUTPUT: «(Any)»
=head2 method unique
Defined As:
method unique(:&as, :&with --> Seq:D)
Treats the C<Any> as a 1-item list and uses
L«C<List.unique>|/type/List#routine_unique» on it.
say Any.unique; # OUTPUT: «((Any))»
=head2 method repeated
Defined As:
method repeated(:&as, :&with --> Seq)
Treats the C<Any> as a 1-item list and uses
L«C<List.repeated>|/type/List#routine_repeated» on it.
say Any.repeated; # OUTPUT: «()»
=head2 method squish
Defined As:
method squish(:&as, :&with --> Seq)
Treats the C<Any> as a 1-item list and uses
L«C<List.squish>|/type/List#routine_squish» on it.
say Any.squish; # OUTPUT: «((Any))»
=head2 method reduce
Defined As:
method reduce(&with --> Nil)
TODO
=head2 method permutations
Defined As:
method permutations(--> Seq)
Treats the C<Any> as a 1-item list and uses
L«C<List.permutations>|/type/List#routine_permutations» on it.
say Any.permutations; # OUTPUT: «(((Any)))»
=head2 method categorize
Defined As:
method categorize(&mapper --> Hash:D)
Treats the C<Any> as a 1-item list and uses
L«C<List.categorize>|/type/List#routine_categorize» on it.
say Any.categorize({ $_ }); # OUTPUT: «{(Any) => [(Any)]}»
=head2 method classify
Defined As:
method classify(&mapper -->Hash:D)
Treats the C<Any> as a 1-item list and uses
L«C<List.classify>|/type/List#routine_classify» on it.
say Any.classify({ $_ }); # OUTPUT: «{(Any) => [(Any)]}»
=head2 method produce
Defined As:
method produce(--> Nil)
TODO
=head2 method pairs
Defined As:
method pairs(--> List)
Returns an empty List.
say Any.pairs; # OUTPUT: «()»
=head2 method antipairs
Defined As:
method antipairs(--> List)
Returns an empty List.
say Any.antipairs; # OUTPUT: «()»
=head2 method kv
Defined As:
method kv(--> List)
Returns an empty List.
say Any.kv; # OUTPUT: «()»
=head2 method toggle
Defined as:
method toggle(Any:D: *@conditions where .all ~~ Callable:D, Bool :$off --> Seq:D)
L<Iterates|/routine/iterator> over the invocant, producing a L<Seq>, toggling
whether the received values are propagated to the result on and off, depending
on the results of calling L<Callables|/type/Callable> in C<@conditions>:
say ^10 .toggle: * < 4, * %% 2, &is-prime; # OUTPUT: «(0 1 2 3 6 7)»
say ^10 .toggle: :off, * > 4; # OUTPUT: «(5 6 7 8 9)»
Imagine a switch that's either on or off (C<True> or C<False>), and values are
produced if it's on. By default, the initial state of that switch is in "on"
position, unless C<:$off> is set to a true value, in which case the initial
state will be "off".
A L<Callable> from the L<head> of C<@conditions> is taken (if any are available)
and it becomes the current tester. Each value from the original sequence is
tested by calling the tester L<Callable> with that value. The state of our
imaginary switch is set to the return value from the tester: if it's truthy,
set switch to "on", otherwise set it to "off".
Whenever the switch is I<toggled> (i.e. switched from "off" to "on" or
from "on" to "off"), the current tester L<Callable> is replaced by the next
L<Callable> in C<@conditions>, if available, which will be used to test any
further values. If no more tester Callables are available, the switch will
remain in its current state until the end of iteration.
=begin code
# our original sequence of elements:
say list ^10; # OUTPUT: «(0 1 2 3 4 5 6 7 8 9)»
# toggled result:
say ^10 .toggle: * < 4, * %% 2, &is-prime; # OUTPUT: «(0 1 2 3 6 7)»
# First tester Callable is `* < 4` and initial state of switch is "on".
# As we iterate over our original sequence:
# 0 => 0 < 4 === True switch is on, value gets into result, switch is
# toggled, so we keep using the same Callable:
# 1 => 1 < 4 === True same
# 2 => 2 < 4 === True same
# 3 => 3 < 4 === True same
# 4 => 4 < 4 === False switch is now off, "4" does not make it into the
# result. In addition, our switch got toggled, so
# we're switching to the next tester Callable
# 5 => 5 %% 2 === False switch is still off, keep trying to find a value
# 6 => 6 %% 2 === True switch is now on, take "6" into result. The switch
# toggled, so we'll use the next tester Callable
# 7 => is-prime(7) === True switch is still on, take value and keep going
# 8 => is-prime(8) === False switch is now off, "8" does not make it into
# the result. The switch got toggled, but we
# don't have any more tester Callables, so it
# will remain off for the rest of the sequence.
=end code
Since the toggle of the switch's state loads the next tester L<Callable>,
setting C<:$off> to a C<True> value affects when first tester is discarded:
=begin code
# our original sequence of elements:
say <0 1 2>; # OUTPUT: «(0 1 2)»
# toggled result:
say <0 1 2>.toggle: * > 1; # OUTPUT: «()»
# First tester Callable is `* > 1` and initial state of switch is "on".
# As we iterate over our original sequence:
# 0 => 0 > 1 === False switch is off, "0" does not make it into result.
# In addition, switch got toggled, so we change the
# tester Callable, and since we don't have any more
# of them, the switch will remain "off" until the end
=end code
=begin code
# our original sequence of elements:
say <0 1 2>; # OUTPUT: «(0 1 2)»
# toggled result:
say <0 1 2>.toggle: :off, * > 1; # OUTPUT: «(2)»
# First tester Callable is `* > 1` and initial state of switch is "off".
# As we iterate over our original sequence:
# 0 => 0 > 1 === False switch is off, "0" does not make it into result.
# The switch did NOT get toggled this time, so we
# keep using our current tester Callable
# 1 => 1 > 1 === False same
# 2 => 2 > 1 === True switch is on, "2" makes it into the result
=end code
=head2 method tree
Defined As:
method tree(--> Any)
Returns Any.
say Any.tree; # OUTPUT: «Any»
=head2 method nl-out
Defined As:
method nl-out(--> Str)
Returns Str with the value of "\n". See
L«C<IO::Handle.nl-out>|/type/IO::Handle#method_nl-out» for the
details.
say Any.nl-out; # OUTPUT: «»
=head2 method invert
Defined As:
method invert(--> List)
Returns an empty List.
say Any.invert; # OUTPUT: «()»
=head2 method combinations
Defined As:
method combinations(--> Seq)
Treats the C<Any> as a 1-item list and uses
L«C<List.combinations>|/type/List#routine_combinations» on it.
say Any.combinations; # OUTPUT: «(() ((Any)))»
=head2 method iterator
Defined As:
method iterator(--> Iterator)
Coerces the C<Any> to a C<list> by applying the C<.list> method and uses
L«C<iterator>|/type/Iterable#method_iterator» on it.
my $it = Any.iterator;
say $it.pull-one; # OUTPUT: «(Any)»
say $it.pull-one; # OUTPUT: «IterationEnd»
=head2 method grep
Defined As:
method grep(Mu $matcher, :$k, :$kv, :$p, :$v --> Seq)
Coerces the C<Any> to a C<list> by applying the C<.list> method and uses
L«C<List.grep>|/type/List#routine_grep» on it.
Based on C<$matcher> value can be either C<((Any))> or empty List.
my $a;
say $a.grep({ True }); # OUTPUT: «((Any))»
say $a.grep({ $_ }); # OUTPUT: «()»
=head2 method append
Defined As:
method append(@values --> Array)
Initializes Any variable as empty Array and calls
L«C<Array.append>|/type/Array#method_append» on it.
my $a;
say $a.append; # OUTPUT: «[]»
my $b;
say $b.append((1,2,3)); # OUTPUT: «[1 2 3]»
=head2 method values
Defined As:
method values(--> List)
Returns an empty List.
=head2 method collate
Defined As:
method collate(--> Seq)
TODO
=head2 method cache
Defined As:
method cache(--> List)
TODO
=end pod
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6