-
Notifications
You must be signed in to change notification settings - Fork 292
/
Bool.pod6
142 lines (91 loc) · 4.25 KB
/
Bool.pod6
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
=begin pod
=TITLE enum Bool
=SUBTITLE Logical boolean
enum Bool <False True>
X<|True>
X<|False>
X<|Boolean>
An enum for boolean true/false decisions.
=head1 Methods
=head2 method ACCEPTS
method ACCEPTS(Bool:D: --> Bool:D)
Used for smartmatch comparison. When the right side is C<True> returns always
C<True>, when the right side of the match is C<False> returns always C<False>.
In particular, C<ACCEPTS> returns always the instance on which it is invoked,
that is the right side of a smartmatch. As an example:
=begin code
my $b = Bool.new( True );
# when True on the right side returns
# always True
True ~~ $b; # True
False ~~ $b; # True
$b = Bool.new( False );
# when False on the right side
# returns always False
False ~~ $b; # False
True ~~ $b; # False
=end code
=head2 routine succ
method succ(--> Bool:D)
Returns C<True>.
say True.succ; # OUTPUT: «True»
say False.succ; # OUTPUT: «True»
C<succ> is short for "successor"; it returns the next enum value. Bool is a
special enum with only two values, C<False> and C<True>. When sorted, C<False>
comes first, so C<True> is its successor. And since C<True> is the "highest"
Bool enum value, its own successor is also C<True>.
=head2 routine pred
method pred(--> Bool:D)
Returns C<False>.
say True.pred; # OUTPUT: «False»
say False.pred; # OUTPUT: «False»
C<pred> is short for "predecessor"; it returns the previous enum value. Bool is
a special enum with only two values, C<False> and C<True>. When sorted, C<False>
comes first, so C<False> is the predecessor to C<True>. And since C<False> is
the "lowest" Bool enum value, its own predecessor is also C<False>.
=head2 routine enums
method enums(--> Hash:D)
Returns a L<Hash|/type/Hash> of enum-pairs. Works on both the C<Bool> type
and any key.
say Bool.enums; # OUTPUT: «{False => 0, True => 1}»
say False.enums; # OUTPUT: «{False => 0, True => 1}»
=head2 routine pick
multi method pick(Bool:U --> Bool:D)
multi method pick(Bool:U $count --> Seq:D)
Returns C<True> or C<False> if called without any argument. Otherwise returns
C<$count> elements chosen at random (without repetition) from the C<enum>. If
C<*> is passed as C<$count>, or C<$count> is greater than or equal to two, then
both elements are returned in random order.
say Bool.pick; # OUTPUT: «True»
say Bool.pick(1); # OUTPUT: «(False)»
say Bool.pick(*); # OUTPUT: «(False True)»
=head2 routine roll
multi method roll(Bool:U --> Bool:D)
multi method roll(Bool:U $count --> Seq:D)
Returns C<True> or C<False> if called without any argument. Otherwise returns
C<$count> elements chosen at random. Note that each random choice from the
C<enum> is made independently, like a separate coin toss where each side of the
coin represents one of the two values of the C<enum>. If C<*> is passed as
C<$count> an infinite L<Seq|/type/Seq> of C<Bool>s is returned.
say Bool.roll; # OUTPUT: «True»
say Bool.roll(3); # OUTPUT: «(True False False)»
say Bool.roll(*); # OUTPUT: «(...)»
=head2 routine Int
multi method Int(Bool:D --> Int:D)
Returns the value part of the C<enum> pair.
say False.Int; # OUTPUT: «0»
say True.Int; # OUTPUT: «1»
=head2 routine Numeric
multi method Numeric(Bool:D --> Int:D)
Returns the value part of the C<enum> pair.
say False.Numeric; # OUTPUT: «0»
say True.Numeric; # OUTPUT: «1»
=head1 Operators
=head2 prefix ?
multi sub prefix:<?>(Mu --> Bool:D)
Coerces its argument to C<Bool>.
=head2 prefix so
multi sub prefix:<so>(Mu --> Bool:D)
Coerces its argument to C<Bool>, has looser precedence than C<< prefix:<?> >>.
=end pod
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6