NAME

FP::PureArray

SYNOPSIS

use FP::PureArray;
use FP::Div 'inc';

my $a = purearray(1,4,5)->map(\&inc);
is $a->sum, 13;

# can't mutate it:
like( (eval { $a->[0]++; 1 } || $@),
      qr/^Modification of a read-only value attempted/);
like( (eval { push @$a, 123; 1 } || $@),
      qr/^Modification of a read-only value attempted/);

is $a->sum, 13;

my $b = $a->unsafe_mutable; # efficient but dangerous!
$$b[0]++;
is $a->sum, 14; # said it was dangerous!
is ref($a), 'FP::_::MutableArray';
# see FP::MutableArray

is( FP::_::PureArray->null == FP::_::PureArray->null, 1);

DESCRIPTION

Perl arrays blessed into the `FP::PureArray` package, inheriting from `FP::Abstract::Pure`, and coming with the functions from `FP::Array` as methods.

If you hand someone an FP::PureArray you guarantee that you won't mutate it. This might be enforced in the future by making them immutable (todo).

PURITY

`PureArray`s are created to be immutable by default, which enforces the functional purity of the API. This can be disabled by setting `$FP::PureArray::immutable` to false when creating them. Only ever use this during development, if at all. If you need to have efficient updates, use another data structure (FP::List suits many cases, or the to-be written FP::Vec, although at that point updates directly on PureArray may be implemented efficiently, too). Or if you're sure making a PureArray mutable again is safe, you can call the `unsafe_mutable` method. Should lexical analysis get implemented in Perl at some point, a method `mutable` could be offered that safely (by way of checking via the reference count that there are no other users) turns a PureArray back into a mutable array.

PureArray implements `FP::Abstract::Pure` (`is_pure` from `FP::Predicates` returns true even if instances were made mutable via setting `$FP::PureArray::immutable`). Values returned from `unsafe_mutable` are in a different class which does *not* implement `FP::Abstract::Pure`.

TODO

Create alternative implementation that is efficient for updates on big arrays (perhaps to be called FP::Vec, but to be interoperable).

SEE ALSO

Implements: FP::Abstract::Pure, FP::Abstract::Sequence, FP::Abstract::Equal, FP::Abstract::Show

FP::MutableArray

NOTE

This is alpha software! Read the status section in the package README or on the website.