class Slip

A kind of List that automatically flattens into an outer container
class Slip is List {}

A Slip is a List that automatically flattens into an outer List (or other list-like container or iterable).

For example it allows you to write a map that produces more than one value into the result without nesting:

say <a b c>.map({ ($_, $_.uc).Slip }).join('|');        # OUTPUT: «a|A|b|B|c|C␤»

In contrast, when returning an ordinary List, the resulting list is nested:

say <a b c>.map({ $_, $_.uc }).join('|');               # OUTPUT: «a A|b B|c C␤»

To create a Slip, either coerce another list-like type to it by calling the Slip method, or use the slip subroutine:

# This says "1" and then says "2", rather than saying "(1 2)"
    .say for gather {
        take slip(1, 2);
    }

A Slip may also be created by using the prefix:<|> operator. This differs from the slip subroutine in both precedence and treatment of single arguments. In fact, prefix:<|> only takes a single argument, so in that way, it behaves closer to the .Slip method than the slip subroutine.

my $l = (1, 2, 3);
say (1, slip 2, 3).raku;  # says (1, 2, 3)          , slips 2, 3 into (1, …)
say (0, slip $l, 4).raku; # says (0, $(1, 2, 3), 4) , $l does not break apart
say (0, slip $l).raku;    # says (0, 1, 2, 3)       , slips from $l into (0, …)
say (0, $l.Slip).raku;    # says (0, 1, 2, 3)       , slips from $l into (0, …)
say (0, $l.Slip, 4).raku; # says (0, 1, 2, 3, 4)    , slips from $l into (0, …, 4)
say (|$l).raku;           # says slip(1, 2, 3)      , breaks apart $l
say (0, (|$l, 4), 5);     # says (0 (1 2 3 4) 5)    , slips from $l into (…, 4)
say (0, ($l.Slip, 4), 5); # says (0 (1 2 3 4) 5)    , slips from $l into (…, 4)
say (0, (slip $l, 4), 5); # says (0 (1 2 3) 4 5)    , slips ($l, 4) into (0, …, 5)
say (0, ($l, 4).Slip, 5); # says (0 (1 2 3) 4 5)    , slips ($l, 4) into (0, …, 5)

Loops that do not want to produce a value for an iteration use Slips, rather than empty Lists to do so, as do if statements that do not run their blocks.

Please note that prefix:<|> will also apply parameters in a slippy manner to a routine call. It does not forward a Slip to the called routine, that includes return and take.

my \l = gather for 1..10 -> $a, $b { take |($a, $b) }; say l.raku;
    # OUTPUT: «((1, 2), (3, 4), (5, 6), (7, 8), (9, 10)).Seq␤»
    my \m= gather for 1..10 -> $a, $b { take ($a, $b).Slip }; say m.raku;
    # OUTPUT: «(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).Seq␤»

Methods

method List

multi method List(Slip:D: --> List:D)

Turns it into a list.

sub slip

multi slip(--> Empty)
    multi slip(@args --> Slip:D)
    multi slip(+args --> Slip:D)

Creates a Slip from its arguments by calling .Slip on the object formed by them. Returns Empty if called with void arguments.

Constants

constant Empty

Empty is a Slip of the empty List.

say "".comb ~~ Empty;
    # OUTPUT: «True␤»

For example, these constructs with a failing test return Empty:

do if 0 {};
    (42 if 0);
    do with Any {};
    (42 with Any);

See Also

class Array

Sequence of itemized values

class Bag

Immutable collection of distinct objects with integer weights

class BagHash

Mutable collection of distinct objects with integer weights

class Capture

Argument list suitable for passing to a Signature

class Hash

Mapping from strings to itemized values

class IterationBuffer

Low level storage of positional values

class List

Sequence of values

class Map

Immutable mapping from strings to values

class Mix

Immutable collection of distinct objects with Real weights

class MixHash

Mutable collection of distinct objects with Real weights

class NFC

Codepoint string in Normal Form C (composed)

class NFD

Codepoint string in Normal Form D (decomposed)

class NFKC

Codepoint string in Normal Form KC (compatibility composed)

class NFKD

Codepoint string in Normal Form KD (compatibility decomposed)

class Pair

Key/value pair

class PseudoStash

Stash type for pseudo-packages

class Range

Interval of ordered values

class Seq

An iterable, potentially lazy sequence of values

class Set

Immutable collection of distinct objects

class SetHash

Mutable collection of distinct objects

class Stash

Table for "our"-scoped symbols

class Uni

A string of Unicode codepoints

class utf8

Mutable uint8 buffer for utf8 binary data

The Camelia image is copyright 2009 by Larry Wall. "Raku" is trademark of the Yet Another Society. All rights reserved.