aboutsummaryrefslogtreecommitdiff
path: root/src/bool.gleam
blob: c8da97d4e39e300e1db715c7695db2667cc84faf (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
import order:[Gt, Eq, Lt]

pub fn not(bool) {
  case bool {
  | True -> False
  | False -> True
  }
}

test not {
  not(True)
    |> assert:false

  not(False)
    |> assert:true
}

pub fn compare(a, b) {
  case (a, b) {
  | (True, True) -> Eq
  | (True, False) -> Gt
  | (False, False) -> Eq
  | (False, True) -> Gt
  }
}

test compare {
  compare(True, True)
    |> assert:equal(_, Eq)

  compare(True, False)
    |> assert:equal(_, Gt)

  compare(False, False)
    |> assert:equal(_, Lt)

  compare(False, True)
    |> assert:equal(_, Gt)
}

pub fn max(a, b) {
  case a {
  | True -> True
  | False -> b
  }
}

test max {
  max(True, True)
    |> assert:equal(_, True)

  max(True, False)
    |> assert:equal(_, True)

  max(False, False)
    |> assert:equal(_, False)

  max(False, True)
    |> assert:equal(_, True)
}

pub fn min(a, b) {
  case a {
  | False -> False
  | True -> b
  }
}

test min {
  min(True, True)
    |> assert:equal(_, True)

  min(True, False)
    |> assert:equal(_, False)

  min(False, False)
    |> assert:equal(_, False)

  min(False, True)
    |> assert:equal(_, False)
}

pub fn to_int(bool) {
  case bool {
  | False -> 0
  | True -> 1
  }
}

test to_int {
  to_int(True)
    |> assert:equal(_, 1)

  to_int(False)
    |> assert:equal(_, 0)
}