Skip to content

Commit 66eeb70

Browse files
committed
List documentation
1 parent 306ceb6 commit 66eeb70

File tree

2 files changed

+109
-4
lines changed

2 files changed

+109
-4
lines changed

src/gleam/function.gleam

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1-
/// A set of utility higher-order functions for working with functions.
2-
31
/// Takes two functions and chains them together to form one function that takes
42
/// the input from the first and returns the output of the second.
3+
///
54
pub fn compose(fun1: fn(a) -> b, fun2: fn(b) -> c) -> fn(a) -> c {
65
fn(a) { fun1(a) |> fun2 }
76
}
87

98
/// Takes a function that takes two arguments and returns a new function that
109
/// takes the same two arguments, but in reverse order.
10+
///
1111
pub fn flip(fun: fn(a, b) -> c) -> fn(b, a) -> c {
1212
fn(b, a) { fun(a, b) }
1313
}
1414

1515
/// A function that always returns its input value.
16+
///
1617
pub fn identity(x: a) -> a {
1718
x
1819
}

src/gleam/list.gleam

Lines changed: 106 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,26 @@
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+
124
import gleam/int
225
import gleam/pair
326
import gleam/order.{Order}
@@ -6,36 +29,117 @@ import gleam/result.{Option}
629
pub type List(elements) =
730
List(elements)
831

32+
/// An error value returned by the `strict_zip` function.
33+
///
934
pub type LengthMismatch {
1035
LengthMismatch
1136
}
1237

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+
/// ```
1453
///
1554
pub external fn length(of: List(a)) -> Int = "erlang" "length"
1655

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+
/// ```
1872
///
1973
pub external fn reverse(List(a)) -> List(a) = "lists" "reverse"
2074

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+
///
2187
pub fn is_empty(list: List(a)) -> Bool {
2288
list == []
2389
}
2490

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+
///
25106
pub fn contains(list: List(a), has elem: a) -> Bool {
26107
case list {
27108
[] -> False
28109
[head | rest] -> head == elem || contains(rest, elem)
29110
}
30111
}
31112

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+
///
32123
pub fn head(list: List(a)) -> Option(a) {
33124
case list {
34125
[] -> result.none()
35126
[x | _] -> Ok(x)
36127
}
37128
}
38129

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+
///
39143
pub fn tail(list: List(a)) -> Option(List(a)) {
40144
case list {
41145
[] -> result.none()

0 commit comments

Comments
 (0)