| 1 | #!/usr/bin/env perl | ||||||||||
| 2 | |||||||||||
| 3 | # Copyright (c) 2019 Christian Jaeger, copying@christianjaeger.ch | ||||||||||
| 4 | # This is free software. See the file COPYING.md that came bundled | ||||||||||
| 5 | # with this file. | ||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | # find modules from functional-perl working directory (non-installed) | ||||||||||
| 12 | 1 | ▶ | use Cwd 'abs_path'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 1 | our ($mydir, $myname); | |||||||||
| 14 | |||||||||||
| 15 | 1 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=3 SCAR=11.7
| |||||||||||
| 16 | 1 | ▶ | my $location = (-l $0) ? abs_path($0) : $0; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 17 | 1 | ▶ | $location =~ /(.*?)([^\/]+?)_?\z/s or die "?"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 18 | 1 | ($mydir, $myname) = ($1, $2); | |||||||||
| 19 | } | ||||||||||
| 20 | 1 | ▶ | use lib "$mydir/../lib"; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | |||||||||||
| 22 | 1 | ▶ | use Test::Requires qw(Moo); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | # Comparison of imperative and functional approaches to some data | ||||||||||
| 25 | # container: | ||||||||||
| 26 | |||||||||||
| 27 | 1 | package ImperativePerson { | |||||||||
| 28 | 1 | ▶ | use Moo; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 29 | |||||||||||
| 30 | 1 | has name => is => 'ro'; | |||||||||
| 31 | 1 | has todo_items => is => 'ro'; # the array itself is the mutable part | |||||||||
| 32 | |||||||||||
| 33 | 2 | ▶ | sub add_todo { | ||||||||
|
Subroutine
add_todo: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 34 | 2 | my ($self, $item) = @_; | |||||||||
| 35 | 2 | push @{ $self->{todo_items} }, $item; | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | } | ||||||||||
| 39 | |||||||||||
| 40 | 1 | package FunctionalPerson { | |||||||||
| 41 | |||||||||||
| 42 | 1 | ▶ | use FP::List; # for `cons`, `cons_` | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 43 | |||||||||||
| 44 | 1 | ▶ | use FP::Struct ["name", "todo_items"]; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 45 | |||||||||||
| 46 | 2 | ▶ | sub add_todo { | ||||||||
|
Subroutine
add_todo: called CC=1 SCAR=0.0
| |||||||||||
| 47 | 2 | my ($self, $item) = @_; | |||||||||
| 48 | |||||||||||
| 49 | # The sub receives the old todo_items list, and returns a new | ||||||||||
| 50 | # list with $item prepended: | ||||||||||
| 51 | #$self->todo_items_update(sub { cons $item, $_[0] }) | ||||||||||
| 52 | # Or use `cons_` which returns a function that prepends | ||||||||||
| 53 | # cons_'s argument to its own argument: | ||||||||||
| 54 | 2 | $self->todo_items_update(cons_ $item) | |||||||||
| 55 | } | ||||||||||
| 56 | |||||||||||
| 57 | 1 | _END_ | |||||||||
| 58 | } | ||||||||||
| 59 | |||||||||||
| 60 | 1 | ▶ | use Test::More; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 61 | |||||||||||
| 62 | 1 | ▶ | sub testImperative { | ||||||||
|
Subroutine
testImperative: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 63 | 1 | my $p = ImperativePerson->new(name => "Chris", todo_items => []); | |||||||||
| 64 | 1 | $p->add_todo("buy bread"); | |||||||||
| 65 | 1 | $p->add_todo("walk dog"); | |||||||||
| 66 | 1 | is_deeply $p->todo_items, ['buy bread', 'walk dog']; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 1 | ▶ | use FP::Equal qw(is_equal); # using FP::Equal's `equal` but still | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 70 | # reporting via Test::More | ||||||||||
| 71 | 1 | ▶ | use FP::List; # for `list` | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 72 | |||||||||||
| 73 | 1 | ▶ | sub testFunctional { | ||||||||
|
Subroutine
testFunctional: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 74 | 1 | my $p = FunctionalPerson->new_(name => "Chris", todo_items => list()); | |||||||||
| 75 | |||||||||||
| 76 | # or, using positional arguments: | ||||||||||
| 77 | #my $p = FunctionalPerson->new("Chris", list()); | ||||||||||
| 78 | |||||||||||
| 79 | # Each add_todo call returns a new version of the object, any | ||||||||||
| 80 | # pointer to the old version still sees the original value: | ||||||||||
| 81 | 1 | my $p2 = $p->add_todo("buy bread"); | |||||||||
| 82 | 1 | my $p3 = $p2->add_todo("walk dog"); | |||||||||
| 83 | 1 | is_equal $p3->todo_items, list('walk dog', 'buy bread'); | |||||||||
| 84 | 1 | is_equal $p2->todo_items, list('buy bread'); | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | 1 | ▶ | if ($ENV{RUN_TESTS}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 1 | testImperative; | |||||||||
| 89 | 1 | testFunctional; | |||||||||
| 90 | 1 | done_testing; | |||||||||
| 91 | } else { | ||||||||||
| 92 | 0 | require FunctionalPerl; | |||||||||
| 93 | 0 | import FunctionalPerl ":repl"; | |||||||||
| 94 | 0 | repl(); | |||||||||
| 95 | } | ||||||||||