File: bonus.t

package info (click to toggle)
libfunction-parameters-perl 2.002005-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 624 kB
  • sloc: perl: 3,945; makefile: 3
file content (64 lines) | stat: -rw-r--r-- 1,256 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#!perl

use Test::More tests => 13;

use warnings FATAL => 'all';
use strict;

use Function::Parameters {
    fun => {
        defaults => 'function_strict',
    },
};

fun filter($f = fun ($x) { 1 }, @xs) {
    !@xs
        ? ()
        : (($f->($xs[0]) ? $xs[0] : ()), filter $f, @xs[1 .. $#xs])
}

is_deeply [filter], [];
is_deeply [filter fun (@) { 1 }, 2 .. 3], [2 .. 3];
is_deeply [filter fun ($x) { $x % 2 }, 1 .. 10], [1, 3, 5, 7, 9];

fun fact($k, $n) :prototype(&$) {
    $n < 2
        ? $k->(1)
        : fact { $k->($n * $_[0]) } $n - 1
}

is +(fact { "~@_~" } 5), "~120~";
is +(fact { $_[0] / 2 } 6), 360;

fun write_to($ref) :prototype(\$) :lvalue { $$ref }

{
    my $x = 2;
    is $x, 2;
    write_to($x) = "hi";
    is $x, "hi";
    write_to($x)++;
    is $x, "hj";
}

{
    my $c = 0;
    fun horf_dorf($ref, $val = $c++) :prototype(\@;$) :lvalue {
        push @$ref, $val;
        $ref->[-1]
    }
}

{
    my @asdf = "A";
    is_deeply \@asdf, ["A"];
    horf_dorf(@asdf) = "b";
    is_deeply \@asdf, ["A", "b"];
    ++horf_dorf @asdf;
    is_deeply \@asdf, ["A", "b", 2];
    horf_dorf @asdf, 100;
    is_deeply \@asdf, ["A", "b", 2, 100];
    splice @asdf, 1, 1;
    horf_dorf(@asdf) *= 3;
    is_deeply \@asdf, ["A", 2, 100, 6];
}