=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 :skip-test
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() returns 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;        # True
    say so 5 == <1 2 3>.any;        # False

=head2 method all

Defined as:

    method all() returns 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;         # True
    say so 3 < <2 3 4>.all;         # False

=head2 method one

Defined as:

    method one() returns 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;      # True
    say so 1 == (1, 2, 1).one;      # False

=head2 method none

Defined as:

    method none() returns 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;     # False
    say so 4 == (1, 2, 3).none;     # True

=head2 method list

Interprets the invocant as a list, and returns that L<List|/type/List>.

    say 42.list.^name;           # List
    say 42.list.elems;           # 1

=head2 method push

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;
    dd %h<a>; # Any (and therefor undefined)
    %h<a>.push(1); # .push on Any
    dd %h; # «Hash %h = {:a($[1])}␤» # please note the Array

=head2 routine reverse

Defined as:

    multi sub    reverse(*@list ) returns List:D
    multi method reverse(List:D:) returns List:D

Returns a list 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;     # (world! hello)
    say reverse ^10;                # (9 8 7 6 5 4 3 2 1 0)

=head2 method sort

Sorts iterables with C<infix:<cmp>> or given code object and returns a new C<List>.

Examples:

    say <b c a>.sort;                           # (a b c)
    say 'bca'.comb.sort.join;                   # abc
    say 'bca'.comb.sort({$^b cmp $^a}).join;    # cba
    say '231'.comb.sort(&infix:«<=>»).join;     # 123

=head2 method map

Defined as:

=begin code :skip-test
proto method map(|) is nodal { * }
=end code
    multi method map(\SELF: &block;; :$label, :$item)
    multi method map(HyperIterable:D: &block;; :$label)

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>.

=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.

    dd [[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>.

    my @a = [1,[2,3],4];
    dd @a.duckmap({ $_ ~~ Int ?? $_++ !! Any });
    # OUTPUT«(1, (2, 3), 4)␤»

=head2 method flat

Interprets the invocant as a list, flattens it, and returns that list.
Please note that C<.flat> will not solve the
L<halting problem|https://en.wikipedia.org/wiki/Halting_problem> for you.
If you flat an infinite list C<.flat> may return that infinite list, eating
all your RAM in the process.

    say ((1, 2), (3)).elems;        # 2
    say ((1, 2), (3)).flat.elems;   # 3

Please not that C<flat> is not recursing into sub lists. You have to recurse by
hand or reconsider your data structures. A single level of nesting can often be
handled with L<destructuring|/type/Signature#Destructuring_Parameters> in
signatures. For deeper structures you may consider
L<gather/take|/syntax/gather take> to create a lazy list.

    my @a = [[1,2,3],[[4,5],6,7]];
    sub deepflat(@a){
        gather for @a {
            take ($_ ~~ Iterable ?? deepflat($_).Slip !! $_)
        }
    };
    dd deepflat(@a);
    # OUTPUT«(1, 2, 3, 4, 5, 6, 7).Seq␤»

=head2 method eager

Interprets the invocant as a list, evaluates it eagerly, and returns that
list.

    say (1..10).eager;              # (1 2 3 4 5 6 7 8 9 10)

=head2 method elems

Interprets the invocant as a list, and returns the number of elements in the
list.

    say 42.elems;                   # 1
    say <a b c>.elems;              # 3

=head2 method end

Interprets the invocant as a list, and returns the last index of that list.

    say 6.end;                      # 0
    say <a b c>.end;                # 2

=head2 method pairup

    method pairup() returns List

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;     # (:a(1), :b("c")).Seq

=head2 sub exit

    sub exit(Int() $status = 0)

Exits the current process with return code C<$status>.

=comment TODO maybe find a better place to document &exit

=end pod

# vim: expandtab shiftwidth=4 ft=perl6
