Perl 6 is a multi-paradigm programming language that offers several features that make writing in a functional style simple, most of the time. Perl 6 is not a pure functional programming language and shouldn't be treated as such. This repository was created to document various FP solutions and approaches using Perl 6.
Note: This is not to be considered the one_true_way_to_write_perl_6 in any paradigm.
-
See the following articles regarding Immutability in Perl 6:
-
The following built-in types are considered immutable:
Type | Description |
---|---|
Str | Perl string (finite sequence of Unicode characters) |
Int | Perl integer (allows Inf/NaN, arbitrary precision, etc.) |
Num | Perl number (approximate Real, generally via floating point) |
Rat | Perl rational (exact Real, limited denominator) |
FatRat | Perl rational (unlimited precision in both parts) |
Complex | Perl complex number |
Bool | Perl boolean |
Exception | Perl exception |
Block | Executable objects that have lexical scopes |
Seq | A list of values (can be generated lazily) |
Range | A pair of Ordered endpoints |
Set | Unordered collection of values that allows no duplicates |
Bag | Unordered collection of values that allows duplicates |
Enum | An immutable Pair |
Map | A mapping of Enums with no duplicate keys |
Signature | Function parameters (left-hand side of a binding) |
Capture | Function call arguments (right-hand side of a binding) |
Blob | An undifferentiated mass of ints, an immutable Buf |
Instant | A point on the continuous atomic timeline |
Duration | The difference between two Instants |
-
Use the bind
:=
infix for scalars, which defaults to 'ro':my $name := "Camelia"; $name = "Elaine"; Cannot assign to an immutable value in block <unit> at <unknown file> line 1
Keep in mind, you can still re-bind using
:=
:my $name := "Camelia"; $name := "Elaine"; Elaine
-
Lists without containers are
ro
immutable, use Lists instead of Arrays (@) for listy things:my $list = (1, 2, 3); push $list, 4; Cannot call 'push' on an immutable 'List' in block <unit> at <unknown file> line 1
A list is immutable, which means you cannot change the number of elements in a list. But if one of the elements happens to be a scalar container, you can still assign to it. [1]
In this example
$list[0]
is arw
container,$x
, with a value of42
. This will allow replacing the value inside the$x
scalar container:my $x = 42; my $list = ($x, 1, 2); $list[0] = 23; say $x; # 23
Instead, bind the
Int
value to$x
which which creates aro
container:my $x := 42; my $list = ($x, 1, 2); $list[0] = 23; # Error Cannot modify an immutable Int in block <unit> at <unknown file> line 1
This example tries replacing the value of
$list[1]
, which is an immutableInt
:my $x = 42; my $list = ($x, 1, 2); $list[1] = 23; # Error Cannot modify an immutable Int in block <unit> at <unknown file> line 1
Use Array (@) in subroutine signatures for FP-style list processing of slurpy subparams. Lists will not work here. The new
@
Array isro
immutable within the scope of the subroutine:multi sub do-sum([$head, *@tail] ) { do-sum( $head, @tail ); }; multi sub do-sum($total, [$head, *@tail] ) { do-sum( $total + $head, @tail ); }; multi sub do-sum($total, [] ) { $total; }; my List $list = (1,2,3,4); do-sum($list); # Attempting to modify an subparameter Array inside its subrouting sub edit-param(($head, *@tail)) { @tail[2] = 30; # Error return ($head, @tail).flat; }; say edit-param((1,2,3,4)); Cannot modify an immutable Int
Use Map for associative Arrays / Hash like behavior:
my Map $list = (name => "Camelia", email => "camelia@perl6.tld").Map Map.new((:email("camelia\@perl6.tld"),:name("Camelia"))) say $list<name>; Camelia # Try modifying $list<name> $list<name> = "Steve"; Cannot modify an immutable Str in block <unit> at <unknown file> line 1 # Try adding a new key-value pair $list<email> = "camelia@perl6.tld"; Cannot modify an immutable Mu in block <unit> at <unknown file> line 1
When you submit a PR and it gets merged, you will be automatically added as a collaborator, but if you wouldn't like to be added, please mention it in your submission.
The larger Perl 6 community is very approachable and does a great job of answering questions and helping those new to Perl 6. If you're looking for other projects to contribute to please see the Perl 6 Most Wanted Modules.
This work is licensed under a
Creative Commons Attribution 3.0 Unported License
https://github.com/scmorrison/perl6-fp-notes/graphs/contributors