| 1 | # | ||||||||||
| 2 | # Copyright (c) 2019-2021 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 3 | # | ||||||||||
| 4 | # This is free software, offered under either the same terms as perl 5 | ||||||||||
| 5 | # or the terms of the Artistic License version 2 or the terms of the | ||||||||||
| 6 | # MIT License (Expat version). See the file COPYING.md that came | ||||||||||
| 7 | # bundled with this file. | ||||||||||
| 8 | # | ||||||||||
| 9 | |||||||||||
| 10 | =head1 NAME | ||||||||||
| 11 | |||||||||||
| 12 | FP::autobox | ||||||||||
| 13 | |||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||
| 15 | |||||||||||
| 16 | 1 | ▶ | use FP::autobox; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 17 | |||||||||||
| 18 | is [12,4,2]->product, 96; | ||||||||||
| 19 | |||||||||||
| 20 | my $arr = [55,10]; | ||||||||||
| 21 | is ref($arr), "ARRAY"; | ||||||||||
| 22 | |||||||||||
| 23 | 1 | ▶ | use FP::Div ":all"; use FP::Ops ":all"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | is $arr->map(\&inc)->reduce(\&add), 67; | ||||||||||
| 25 | |||||||||||
| 26 | # Calling methods on arrays 'consumes' the arrays, i.e. makes them | ||||||||||
| 27 | # blessed and immutable (TODO: offer a FP::autobox::mutable or | ||||||||||
| 28 | # so?): | ||||||||||
| 29 | |||||||||||
| 30 | is ref($arr), 'FP::_::PureArray'; | ||||||||||
| 31 | |||||||||||
| 32 | is( (eval { $arr->[0]++; 1 } || regex_substitute(sub{s/\s*at .*//s}, $@)), | ||||||||||
| 33 | 'Modification of a read-only value attempted' ); | ||||||||||
| 34 | |||||||||||
| 35 | is $$arr[0], 55; | ||||||||||
| 36 | |||||||||||
| 37 | =head1 DESCRIPTION | ||||||||||
| 38 | |||||||||||
| 39 | Wouldn't it be cool if an ordinary Perl array could be used as an | ||||||||||
| 40 | L<FP::Abstract::Sequence>? Autobox delivers on that. | ||||||||||
| 41 | |||||||||||
| 42 | This uses L<autobox> underneath. This module does bless the arrays, so | ||||||||||
| 43 | that method calls other than in the lexical scope of the autobox | ||||||||||
| 44 | import work, too. | ||||||||||
| 45 | |||||||||||
| 46 | =head1 TODO | ||||||||||
| 47 | |||||||||||
| 48 | This could be extended to support methods on strings, once a | ||||||||||
| 49 | L<FP::String> module has been implemented, and on hashes for a to-be | ||||||||||
| 50 | implemented L<FP::Abstract::HashMap> protocol. | ||||||||||
| 51 | |||||||||||
| 52 | =head1 SEE ALSO | ||||||||||
| 53 | |||||||||||
| 54 | L<FP::PureArray>, which is transparently loaded to provide the | ||||||||||
| 55 | methods. | ||||||||||
| 56 | |||||||||||
| 57 | =head1 NOTE | ||||||||||
| 58 | |||||||||||
| 59 | This is alpha software! Read the status section in the package README | ||||||||||
| 60 | or on the L<website|http://functional-perl.org/>. | ||||||||||
| 61 | |||||||||||
| 62 | =cut | ||||||||||
| 63 | |||||||||||
| 64 | package FP::autobox; | ||||||||||
| 65 | |||||||||||
| 66 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 67 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 68 | 4 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 69 | |||||||||||
| 70 | 4 | require FP::PureArray; | |||||||||
| 71 | 4 | require autobox; | |||||||||
| 72 | |||||||||||
| 73 | 4 | our @ISA = "autobox"; | |||||||||
| 74 | |||||||||||
| 75 | 4 | ▶ | sub import { | ||||||||
|
Subroutine
import: called CC=4 SCAR=14.2
| |||||||||||
| 76 | 4 | my ($pack, @args) = @_; | |||||||||
| 77 | 4 | my %args = @args; | |||||||||
| 78 | 4 | ▶ | if (not $args{ARRAY}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 4 | push @_, ARRAY => 'FP::PureArray::autobox'; | |||||||||
| 80 | } | ||||||||||
| 81 | 4 | ▶ | if (not $args{HASH}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 4 | push @_, HASH => 'FP::PureHash::autobox'; | |||||||||
| 83 | } | ||||||||||
| 84 | 4 | ▶ | my $m = $pack->can("SUPER::import") or die "bug"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 85 | 4 | goto $m | |||||||||
| 86 | } | ||||||||||
| 87 | |||||||||||
| 88 | 4 | 1 | |||||||||