aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/gleam_stdlib/src/gleam@bool.erl
blob: cd553586e4ec099a65e702d9609f745f8a24a4f7 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-module(gleam@bool).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function]).

-export(['and'/2, 'or'/2, negate/1, nor/2, nand/2, exclusive_or/2, exclusive_nor/2, compare/2, max/2, min/2, to_int/1, to_string/1, guard/3, lazy_guard/3]).

-spec 'and'(boolean(), boolean()) -> boolean().
'and'(A, B) ->
    A andalso B.

-spec 'or'(boolean(), boolean()) -> boolean().
'or'(A, B) ->
    A orelse B.

-spec negate(boolean()) -> boolean().
negate(Bool) ->
    case Bool of
        true ->
            false;

        false ->
            true
    end.

-spec nor(boolean(), boolean()) -> boolean().
nor(A, B) ->
    case {A, B} of
        {false, false} ->
            true;

        {false, true} ->
            false;

        {true, false} ->
            false;

        {true, true} ->
            false
    end.

-spec nand(boolean(), boolean()) -> boolean().
nand(A, B) ->
    case {A, B} of
        {false, false} ->
            true;

        {false, true} ->
            true;

        {true, false} ->
            true;

        {true, true} ->
            false
    end.

-spec exclusive_or(boolean(), boolean()) -> boolean().
exclusive_or(A, B) ->
    case {A, B} of
        {false, false} ->
            false;

        {false, true} ->
            true;

        {true, false} ->
            true;

        {true, true} ->
            false
    end.

-spec exclusive_nor(boolean(), boolean()) -> boolean().
exclusive_nor(A, B) ->
    case {A, B} of
        {false, false} ->
            true;

        {false, true} ->
            false;

        {true, false} ->
            false;

        {true, true} ->
            true
    end.

-spec compare(boolean(), boolean()) -> gleam@order:order().
compare(A, B) ->
    case {A, B} of
        {true, true} ->
            eq;

        {true, false} ->
            gt;

        {false, false} ->
            eq;

        {false, true} ->
            lt
    end.

-spec max(boolean(), boolean()) -> boolean().
max(A, B) ->
    case A of
        true ->
            true;

        false ->
            B
    end.

-spec min(boolean(), boolean()) -> boolean().
min(A, B) ->
    case A of
        false ->
            false;

        true ->
            B
    end.

-spec to_int(boolean()) -> integer().
to_int(Bool) ->
    case Bool of
        false ->
            0;

        true ->
            1
    end.

-spec to_string(boolean()) -> binary().
to_string(Bool) ->
    case Bool of
        false ->
            <<"False"/utf8>>;

        true ->
            <<"True"/utf8>>
    end.

-spec guard(boolean(), DDZ, fun(() -> DDZ)) -> DDZ.
guard(Requirement, Consequence, Alternative) ->
    case Requirement of
        true ->
            Consequence;

        false ->
            Alternative()
    end.

-spec lazy_guard(boolean(), fun(() -> DEA), fun(() -> DEA)) -> DEA.
lazy_guard(Requirement, Consequence, Alternative) ->
    case Requirement of
        true ->
            Consequence();

        false ->
            Alternative()
    end.