1
+ //// Lists are an ordered sequence of elements and are one of the most common
2
+ //// data types in Gleam.
3
+ ////
4
+ //// New elements can be added and removed from the front of a list in
5
+ //// constant time, while adding and removing from the end requires traversing
6
+ //// the copying the whole list, so keep this in mind when designing your
7
+ //// programs.
8
+ ////
9
+ //// There is a dedicated syntax for prefixing to a list:
10
+ ////
11
+ //// ```
12
+ //// let new_list = [1, 2, ..existing_list]
13
+ //// ```
14
+ ////
15
+ //// And a matching syntax for getting the first elements of a list:
16
+ ////
17
+ //// ```
18
+ //// case list {
19
+ //// [first_element, ..rest] -> first_element
20
+ //// _ -> "this pattern matches when the list is empty"
21
+ //// }
22
+ //// ```
23
+
1
24
import gleam/int
2
25
import gleam/pair
3
26
import gleam/order . { Order }
@@ -6,36 +29,117 @@ import gleam/result.{Option}
6
29
pub type List ( elements) =
7
30
List ( elements)
8
31
32
+ /// An error value returned by the `strict_zip` function.
33
+ ///
9
34
pub type LengthMismatch {
10
35
LengthMismatch
11
36
}
12
37
13
- /// Using the Erlang C BIF implementation.
38
+ /// Count the number of elements in a given list.
39
+ ///
40
+ /// This function has to traverse the list to determine the number of elements,
41
+ /// so it runs in linear time.
42
+ ///
43
+ /// This function is natively implemented by the virtual machine and is highly
44
+ /// optimised.
45
+ ///
46
+ /// ## Examples
47
+ ///
48
+ /// ```
49
+ /// length([]) == 0
50
+ /// length([1]) == 1
51
+ /// length([1, 2]) == 2
52
+ /// ```
14
53
///
15
54
pub external fn length ( of : List ( a) ) -> Int = "erlang" "length"
16
55
17
- /// Using the Erlang C BIF implementation.
56
+ /// Create a new list from a given list containing the same elements but in the
57
+ /// opposite order.
58
+ ///
59
+ /// This function has to traverse the list to create the new reversed list, so
60
+ /// it runs in linear time.
61
+ ///
62
+ /// This function is natively implemented by the virtual machine and is highly
63
+ /// optimised.
64
+ ///
65
+ /// ## Examples
66
+ ///
67
+ /// ```
68
+ /// reverse([]) == []
69
+ /// reverse([1]) == [1]
70
+ /// reverse([1, 2]) == [2, 1]
71
+ /// ```
18
72
///
19
73
pub external fn reverse ( List ( a) ) -> List ( a) = "lists" "reverse"
20
74
75
+ /// Determine whether or not the list is empty.
76
+ ///
77
+ /// This function runs in constant time.
78
+ ///
79
+ /// ## Examples
80
+ ///
81
+ /// ```
82
+ /// is_empty([]) == True
83
+ /// is_empty([1]) == False
84
+ /// is_empty([1, 1]) == False
85
+ /// ```
86
+ ///
21
87
pub fn is_empty ( list : List ( a) ) -> Bool {
22
88
list == [ ]
23
89
}
24
90
91
+ /// Determine whether or not a given element exists within a given list.
92
+ ///
93
+ /// This function traverses the list to find the element, so it runs in linear
94
+ /// time.
95
+ ///
96
+ /// ## Examples
97
+ ///
98
+ /// ```
99
+ /// contains([], 0) == True
100
+ /// contains([0], 0) == True
101
+ /// contains([1], 0) == False
102
+ /// contains([1, 1], 0) == False
103
+ /// contains([1, 0], 0) == True
104
+ /// ```
105
+ ///
25
106
pub fn contains ( list : List ( a) , has elem : a) -> Bool {
26
107
case list {
27
108
[ ] -> False
28
109
[ head | rest] -> head == elem || contains(rest, elem)
29
110
}
30
111
}
31
112
113
+ /// Get the first element from the start of the list, if there is one.
114
+ ///
115
+ /// ## Examples
116
+ ///
117
+ /// ```
118
+ /// head([]) == Error(Nil)
119
+ /// head([0]) == Ok(0)
120
+ /// head([1, 2]) == Ok(1)
121
+ /// ```
122
+ ///
32
123
pub fn head(list: List(a)) -> Option(a) {
33
124
case list {
34
125
[] -> result.none()
35
126
[x | _] -> Ok(x)
36
127
}
37
128
}
38
129
130
+ /// Get the list minus the first element. If the list is empty `Error(Nil)` is
131
+ /// returned.
132
+ ///
133
+ /// This function runs in constant time and does not make a copy of the list.
134
+ ///
135
+ /// ## Examples
136
+ ///
137
+ /// ```
138
+ /// tail([]) == Error(Nil)
139
+ /// tail([0]) == Ok([])
140
+ /// tail([1, 2]) == Ok([2])
141
+ /// ```
142
+ ///
39
143
pub fn tail(list: List(a)) -> Option(List(a)) {
40
144
case list {
41
145
[] -> result.none()
0 commit comments