App-Cheats

 view release on metacpan or  search on metacpan

cheats.txt  view on Meta::CPAN

interp 12195122/s     --   -68%
concat 38461538/s   215%     --
#
# Inside a sentence.
perl -Me -e 'my $v = 123; n { interp => sub { my $c = "I got $v dollars"}, concat => sub { my $c = "I got " . $v . " dollars" } }, 10000000'
             Rate interp concat
interp 14285714/s     --    -4%
concat 14925373/s     4%     --

# Interpolation vs concat vs comma comparison:
perl -Me -e 'my $v = 123; n { interp => sub { "I got $v dollars"}, concat => sub { "I got " . $v . " dollars" }, comma => sub{ "I got ", $v, " dollars" } }, 10000000'
             Rate interp concat  comma
interp 20408163/s     --    -2%   -57%
concat 20833333/s     2%     --   -56%
comma  47619048/s   133%   129%     --

# Remove duplicate characters.
perl -E '$_ = "abbbc"; s/(.)\g1+/$1/; say'
abc
perl -E '$_ = "abbbc"; tr///cs; say'
abc

# Remove duplicate characters (benchmark).
perl -Me -e '
    $copy = "abbc";
    n {
        s => sub{
            local $_ = $copy;
            s/(.)\g1+/$1/;
            $_;
        },
        tr => sub{
            local $_ = $copy;
            tr///cs;
            $_;
        }
    }, 1000000
'
            (warning: too few iterations for a reliable count)
        Rate    s   tr
s  1408451/s   -- -70%
tr 4761905/s 238%   --

# Comparing different ways in perl to combine hashes.
#
# Each: 2.6s
while ( my ($key,$val) = each %users_one ) {
    $users{$key} = $val;
}
#
# Merge: 1.7s
%users = ( %users, %users_one);
#
# Slice: 700ms
@users{keys %users_one} = values %users_one;

# Benchmark glob() vs -e() functions
perl -Me -e '
    n {
        e_found                  => sub{ -e "recursive.pl" },
        e_not_found              => sub{ -e "recursive2.pl" },
        glob_found               => sub{ glob "recursive.pl" },
        glob_not_found           => sub{ glob "recursive2.pl" },
        glob_wild_flag_found     => sub{ glob "rec*.pl" },
        glob_wild_flag_not_found => sub{ glob "rec2*.pl" },
    }, 1_000_000
'
                              Rate glob_wild_flag_not_found glob_wild_flag_found glob_found glob_not_found e_found e_not_found
glob_wild_flag_not_found   79491/s                       --                 -45%       -94%           -95%    -96%        -98%
glob_wild_flag_found      143885/s                      81%                   --       -90%           -91%    -93%        -96%
glob_found               1408451/s                    1672%                 879%         --           -15%    -35%        -56%
glob_not_found           1666667/s                    1997%                1058%        18%             --    -23%        -48%
e_found                  2173913/s                    2635%                1411%        54%            30%      --        -33%
e_not_found              3225806/s                    3958%                2142%       129%            94%     48%          --


#############################################################
## Perl Binary
#############################################################

# Convert to binary using recursion (POC,perl).
sub binary{
    my($n) = @_;
    $n //= $_;
    return $n if $n == 0 or $n == 1;
    my $k = int($n/2);
    my $b = $n % 2;
    binary($k) . $b;
}
for(1..40){
    say "$_ ", binary;
}
__END__
1 1
2 10
3 11
4 100
5 101
6 110
7 111
8 1000
9 1001
10 1010
11 1011
12 1100
13 1101
14 1110
15 1111
16 10000
17 10001
18 10010
19 10011
20 10100
21 10101
22 10110
23 10111
24 11000
25 11001
26 11010
27 11011
28 11100
29 11101
30 11110
31 11111
32 100000
33 100001
34 100010
35 100011
36 100100
37 100101
38 100110
39 100111
40 101000

cheats.txt  view on Meta::CPAN

perl -lne 'INIT{$"="\n"} $n=/SECTION 1/.../SECTION 2/; push @a,$_ and next if $n and $n !~ /E0$/; /SECTION 3/ and print "@a" and undef @a; print' vim_comp

# Delete "SECTION 2" (end marker is "SECTION 3" exclusive (Vim)
:g/SECTION 2/,/SECTION 3/-1d
#
# Approach in perl
perl -lne '$a=/SECTION 2/.../SECTION 3/; print unless $a and $a !~ /E0$/' vim_comp


#############################################################
## Vim Markers
#############################################################

# Set marker c on this line (Vim)
mc

# Go to beginning of marker c line (Vim)
`c

# Go to first non-blank character of marker c line (Vim)
'c

# Same current deletion into buffer 'a' (Vim)
"add

# Paste current contents of buffer 'a' below (Vim)
"ap


#############################################################
## Vim Find/Search for strings
#############################################################

# Search forward for string (Vim)
/string

# Search back for string (Vim)
?string

# Search for next instance of string (Vim)
n

# Search for previous instance of string (Vim)
N

# Jump/Match between openning and closing parenthesis (Vim)
<Shift> + %

# Ignore case when searching (Vim)
:set ic

# Unignore case when searching (Vim)
:set noic


#############################################################
## Vim Find/Search and Replace for strings
#############################################################

# Make every after very magical (Vim,regex)
\v magical \V not_magical

# Replace pattern with string according to flags (Vim)
:s/pattern/string/flags

# Replace pattern with string ask for confirmation of each change (Vim)
:s/pattern/string/gc

# Replace pattern with string in entire file (Vim)
:%s/pattern/string/gc
:1,$s/pattern/string/gc

# Replace pattern with string in range (Vim)
:5,10s/pattern/string/gc

# Allow capturing groups (dollar variables) in substitutions (Vim, search and replace)
# \v for very magical mode
:%s/ \v([a-z]+)::/ <x>\1::/gc

# Positive lookahead (Vim)
# \v(pattern)@=
:%s/ \v([a-z_]+)\v(::)@=/ <x>\1/gc

# Format output from search and replace (Vim, substitution)
# as printf would do. Need to use submatch(1) instead of \1
s/\v(\S+)/submatch(1)/
:'<,'>s/\v(.* )\v(')@=/\=printf('%-60s',submatch(1))/c

# Word Boundary (Vim)
# \b \<
:%s/\<\v([cp]p[ab])@=/l/gc
:h \<

# Find how to use a specific variable (Vim)
:h \<

# Flag - Replace all occurences of pattern (Vim)
g

# Flag - Confirm replaces (Vim)
c

# Repeat last :s command (Vim)
&

# Non greedy search (Vim)
# Preceeding pattern range with "-"
:help non-greedy
{-}


#############################################################
## Vim Replace Placeholders (captures)
#############################################################

# Use capture in vim replace.
# s/\(\d\+\)/\1/
:'<,'>s/eval_result => \zs"\?\(\d\+[^"]\{0,\}\)"\?/qr{
:\1}/gc




( run in 1.758 second using v1.01-cache-2.11-cpan-5a3173703d6 )