aboutsummaryrefslogtreecommitdiff
path: root/src/tuple.gleam
blob: 5da1810da96ccf5696d744a3da718fd0f2c6e28c (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
import expect
import result
import list

pub fn new(a, b) {
  {a, b}
}

test new {
  new(1, 2)
  |> expect:equal(_, {1, 2})

  new(2, "3")
  |> expect:equal(_, {2, "3"})
}

pub fn first(tup) {
  let {a, _} = tup
  a
}

test first {
  {1, 2}
  |> first
  |> expect:equal(_, 1)
}

pub fn second(tup) {
  let {_, a} = tup
  a
}

test second {
  {1, 2}
  |> second
  |> expect:equal(_, 2)
}

pub fn swap(tup) {
  let {a, b} = tup
  {b, a}
}

test swap {
  {1, "2"}
  |> swap
  |> expect:equal(_, {"2", 1})
}

pub fn fetch(haystack, needle) {
  list:find(haystack, fn(tuple) {
    case first(tuple) == needle {
    | True -> Ok(second(tuple))
    | False -> Error([])
    }
  })
}

test fetch {
  let proplist = [{0, "1"}, {1, "2"}]

  proplist
  |> fetch(_, 0)
  |> expect:equal(_, Ok("1"))

  proplist
  |> fetch(_, 1)
  |> expect:equal(_, Ok("2"))

  proplist
  |> fetch(_, 2)
  |> expect:is_error
}