| 1 | package Perl::Achievements::Achievement::SchwartzianTransform; | ||||||||||||||||||||
| 2 | 1 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | $Perl::Achievements::Achievement::SchwartzianTransform::AUTHORITY = 'cpan:YANICK'; | |||||||||||||||||||
| 4 | } | ||||||||||||||||||||
| 5 | { | ||||||||||||||||||||
| 6 | 1 | $Perl::Achievements::Achievement::SchwartzianTransform::VERSION = '0.4.0'; | |||||||||||||||||||
| 7 | } | ||||||||||||||||||||
| 8 | # ABSTRACT: code uses schwartzian transformations | ||||||||||||||||||||
| 9 | |||||||||||||||||||||
| 10 | 1 | ▶ | use 5.10.0; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 1 | ▶ | use Moose; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 16 | 1 | ▶ | use MooseX::SemiAffordanceAccessor; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 17 | |||||||||||||||||||||
| 18 | 1 | with 'Perl::Achievements::Achievement'; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 1 | has transformations => ( | |||||||||||||||||||
| 21 | traits => [qw/ Counter Perl::Achievements::Role::ConfigItem /], | ||||||||||||||||||||
| 22 | isa => 'Num', | ||||||||||||||||||||
| 23 | is => 'rw', | ||||||||||||||||||||
| 24 | default => 0, | ||||||||||||||||||||
| 25 | handles => { | ||||||||||||||||||||
| 26 | inc_transformations => 'inc', | ||||||||||||||||||||
| 27 | }, | ||||||||||||||||||||
| 28 | ); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | ▶ | sub scan { | ||||||||||||||||||
|
Subroutine
scan: not called CC=3 SCAR=22.0
| |||||||||||||||||||||
| 31 | 0 | my $self = shift; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 1 | ▶ | use List::MoreUtils 'uniq'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | # Living up to the name of... | ||||||||||||||||||||
| 36 | my $schwartzian_transformations = | ||||||||||||||||||||
| 37 | # Step 6) Superfluous but still nice to have a step 6 ;-) | ||||||||||||||||||||
| 38 | scalar | ||||||||||||||||||||
| 39 | # Step 5) DIRRRTY! (but matches more complex statements than just a simple map sort map) | ||||||||||||||||||||
| 40 | 0 | grep { /map.*?sort.*?map/g } | |||||||||||||||||||
| 41 | # Step 4) Prune the statements gathered from Step 3) | ||||||||||||||||||||
| 42 | map { | ||||||||||||||||||||
| 43 | 0 | ▶ | $_->prune(sub { # (that is, remove everything from that statement) | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 44 | !( # except | ||||||||||||||||||||
| 45 | 0 | ▶ | $_[1]->isa('PPI::Token::Word') # the tokens | ||||||||||||||||||
|
Condition0%0/3
$_[1]->isa('PPI::Token::Word') && $_[1]->literal ~~ ['map', 'grep', 'sort']
MC/DC0%0/2
$_[1]->isa('PPI::Token::Word') && $_[1]->literal ~~ ['map', 'grep', 'sort']
$_[1]->isa('PPI::Token::Word')
$_[1]->literal ~~ ['map', 'grep', 'sort']
Truth table0%0/3
A && B
A= $_[1]->isa('PPI::Token::Word')B= $_[1]->literal ~~ ['map', 'grep', 'sort']
| |||||||||||||||||||||
| 46 | and $_[1]->literal ~~ [ 'map', 'grep', 'sort' ] # 'map' and 'sort' ('grep' doesn't hurt here and you could | ||||||||||||||||||||
| 47 | ) # do some clever combination of Steps 1 + 4 + 5....) | ||||||||||||||||||||
| 48 | } # Finally, | ||||||||||||||||||||
| 49 | 0 | ) => $_->content # don't forget to return the content | |||||||||||||||||||
| 50 | } # (since prune only returns the number it has pruned) | ||||||||||||||||||||
| 51 | uniq # Step 3) uniq-ify them | ||||||||||||||||||||
| 52 | 0 | map { $_->statement } # Step 2) Get the statements in which these tokens occur | |||||||||||||||||||
| 53 | @{ # Step 1) Generate the list to work on... | ||||||||||||||||||||
| 54 | 0 | ▶ | $self->ppi->find( # all places where 'map' or 'sort' occurs | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 55 | 0 | ▶ | sub { # of course... | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 56 | 0 | ▶ | $_[1]->isa('PPI::Token::Word') # you could watch out for more | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | and $_[1]->literal ~~ [ 'map', 'sort' ] # 'grep', 'uniq', ... | ||||||||||||||||||||
| 58 | } | ||||||||||||||||||||
| 59 | ) | ||||||||||||||||||||
| 60 | 0 | } or return; | |||||||||||||||||||
| 61 | |||||||||||||||||||||
| 62 | 0 | $self->inc_transformations($schwartzian_transformations); | |||||||||||||||||||
| 63 | |||||||||||||||||||||
| 64 | 0 | ▶ | return unless $self->transformations >= 2**$self->level; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 65 | |||||||||||||||||||||
| 66 | 0 | $self->inc_level; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 0 | $self->unlock( | |||||||||||||||||||
| 69 | sprintf "used the schwartzian transformations %d times", | ||||||||||||||||||||
| 70 | $self->transformations | ||||||||||||||||||||
| 71 | ); | ||||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 1 | 1; | |||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | =pod | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | =head1 NAME | ||||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | Perl::Achievements::Achievement::SchwartzianTransform - code uses schwartzian transformations | ||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | =head1 VERSION | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | version 0.4.0 | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | =head1 AUTHORS | ||||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | =over | ||||||||||||||||||||
| 91 | |||||||||||||||||||||
| 92 | =item Daniel Bruder <dbr@cpan.org> | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | =item Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | =back | ||||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | =head1 SEE ALSO | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | Wikipedia entry on the Schwartzian Transform - | ||||||||||||||||||||
| 101 | L<http://en.wikipedia.org/wiki/Schwartzian_transform> | ||||||||||||||||||||
| 102 | |||||||||||||||||||||
| 103 | =head1 AUTHOR | ||||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | Yanick Champoux <yanick@cpan.org> | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | This software is copyright (c) 2012 by Yanick Champoux. | ||||||||||||||||||||
| 110 | |||||||||||||||||||||
| 111 | This is free software; you can redistribute it and/or modify it under | ||||||||||||||||||||
| 112 | the same terms as the Perl 5 programming language system itself. | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | |||||||||||||||||||||
| 117 | __END__ | ||||||||||||||||||||