/
advent2013-day21.t
144 lines (121 loc) · 3.01 KB
/
advent2013-day21.t
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use Test;
plan 24;
# Positionals
{
sub greet($name) { "hello $name" }
is greet("joe"), "hello joe";
throws-like {EVAL 'greet()'}, X::TypeCheck::Argument;
}
our sub guess($who, $what?) {
# manually check to see if $what is defined
$what.defined
}
throws-like {EVAL 'guess()'}, X::TypeCheck::Argument;
is-deeply guess("World"), False, 'optional';
is-deeply guess("World",37), True, 'optional';
{
sub dance($who, $dance = "Salsa") {
$dance;
}
is dance("Rachael"), "Salsa", 'default';
is dance("Rachael", "Watusi"), "Watusi", 'default';
}
{
multi sub dance($who, $dance) {
"$who is doing the $dance";
}
multi sub dance($who) {
dance($who, "Salsa");
}
is dance("Rachael"), "Rachael is doing the Salsa", 'multi';
is dance("Rachael", "Watusi"), "Rachael is doing the Watusi", 'multi';
}
# Types
{
sub greet(Str $name) {"hello $name"}
lives-ok {EVAL 'greet("joe")'},'type check';
throws-like {EVAL 'greet(3)'}, X::TypeCheck::Argument;
}
{
multi odd-or-even(Int $i where * %% 2) { "even" };
multi odd-or-even(Int $i) { "odd"};
is odd-or-even(42), "even", 'where clause';
is odd-or-even(37), "odd", 'where clause';
}
{
multi fib(1) { 1 }
multi fib(2) { 1 }
multi fib(Int $i) { fib($i-1) + fib($i-2) }
is fib(10), 55, 'literal arguments'
}
# Named
{
sub doctor(:$number, :$prop) {
"Doctor # $number liked to play with his $prop";
}
is doctor(:prop("cricket bat"), :number<5>),
'Doctor # 5 liked to play with his cricket bat',
'named';
is doctor(:number<4>, :prop<scarf>),
'Doctor # 4 liked to play with his scarf',
'named';
my $prop = "fez";
my $number = 11;
is doctor(:$prop, :$number),
'Doctor # 11 liked to play with his fez',
'named';
}
{
sub doctor(:number($incarnation), :prop($accoutrement)) {
"Doctor # $incarnation liked to play with his $accoutrement";
}
my $number = 2;
my $prop = "recorder";
is doctor(:$number, :$prop),
'Doctor # 2 liked to play with his recorder',
'named (mapped)';
}
# Slurpy
{
sub Sprintf(Cool $format, *@args) {
return $format => @args
}
is-deeply Sprintf("%d plus %d is %d", 37, 5, 42), ("%d plus %d is %d" => [37, 5, 42]), 'sprintf example';
}
{
my &callwith := -> *@pos, *%named {
@pos => %named
};
is-deeply callwith(10, 20, :a(30), :b(40)),
[10,20] => {a => 30, b => 40},
'pointy block syntax';
}
# Methods
{
class Foo {
method explode($self: $method) {
[$self, $method];
}
}
my $obj = Foo.new;
my $r = $obj.explode(42);
is-deeply $r, [$obj, 42], 'method invocant';
}
# Parameter Traits
{
my $a = 35;
sub tst-ro($p is readonly) {$p = 42;}
throws-like {EVAL 'tst-ro($a)'}, Exception, 'readonly trait, does not have a type object yet';
}
{
my $a = 35;
sub tst-rw($p is rw) {$p = 42;}
tst-rw($a);
is $a, 42, 'rw trait';
}
{
my $a = 35;
sub tst-ro($p is copy) {$p = 42;}
is $a, 35, 'copy trait';
}
# vim: expandtab shiftwidth=4