1Seq(3) OCaml library Seq(3)
2
3
4
6 Seq - Sequences (functional iterators).
7
9 Module Seq
10
12 Module Seq
13 : sig end
14
15
16 Sequences (functional iterators).
17
18 The type 'a Seq.t is a delayed list, i.e. a list where some evaluation
19 is needed to access the next element. This makes it possible to build
20 infinite sequences, to build sequences as we traverse them, and to
21 transform them in a lazy fashion rather than upfront.
22
23
24 Since 4.07
25
26
27
28
29
30 type 'a t = unit -> 'a node
31
32
33 The type of delayed lists containing elements of type 'a . Note that
34 the concrete list node 'a node is delayed under a closure, not a lazy
35 block, which means it might be recomputed every time we access it.
36
37
38 type 'a node =
39 | Nil
40 | Cons of 'a * 'a t
41
42
43 A fully-evaluated list node, either empty or containing an element and
44 a delayed tail.
45
46
47
48 val empty : 'a t
49
50 The empty sequence, containing no elements.
51
52
53
54 val return : 'a -> 'a t
55
56 The singleton sequence containing only the given element.
57
58
59
60 val cons : 'a -> 'a t -> 'a t
61
62
63 cons x xs is the sequence containing the element x followed by the se‐
64 quence xs
65
66
67
68 Since 4.11
69
70
71
72 val append : 'a t -> 'a t -> 'a t
73
74
75 append xs ys is the sequence xs followed by the sequence ys
76
77
78
79 Since 4.11
80
81
82
83 val map : ('a -> 'b) -> 'a t -> 'b t
84
85
86 map f seq returns a new sequence whose elements are the elements of seq
87 , transformed by f . This transformation is lazy, it only applies when
88 the result is traversed.
89
90 If seq = [1;2;3] , then map f seq = [f 1; f 2; f 3] .
91
92
93
94 val filter : ('a -> bool) -> 'a t -> 'a t
95
96 Remove from the sequence the elements that do not satisfy the given
97 predicate. This transformation is lazy, it only applies when the re‐
98 sult is traversed.
99
100
101
102 val filter_map : ('a -> 'b option) -> 'a t -> 'b t
103
104 Apply the function to every element; if f x = None then x is dropped;
105 if f x = Some y then y is returned. This transformation is lazy, it
106 only applies when the result is traversed.
107
108
109
110 val concat : 'a t t -> 'a t
111
112 concatenate a sequence of sequences.
113
114
115 Since 4.13
116
117
118
119 val flat_map : ('a -> 'b t) -> 'a t -> 'b t
120
121 Map each element to a subsequence, then return each element of this
122 sub-sequence in turn. This transformation is lazy, it only applies
123 when the result is traversed.
124
125
126
127 val concat_map : ('a -> 'b t) -> 'a t -> 'b t
128
129 Alias for Seq.flat_map .
130
131
132 Since 4.13
133
134
135
136 val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
137
138 Traverse the sequence from left to right, combining each element with
139 the accumulator using the given function. The traversal happens imme‐
140 diately and will not terminate on infinite sequences.
141
142 Also see List.fold_left
143
144
145
146
147 val iter : ('a -> unit) -> 'a t -> unit
148
149 Iterate on the sequence, calling the (imperative) function on every el‐
150 ement. The traversal happens immediately and will not terminate on in‐
151 finite sequences.
152
153
154
155 val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t
156
157 Build a sequence from a step function and an initial value. unfold f u
158 returns empty if f u returns None , or fun () -> Cons (x, unfold f y)
159 if f u returns Some (x, y) .
160
161 For example, unfold (function [] -> None | h::t -> Some (h,t)) l is
162 equivalent to List.to_seq l .
163
164
165 Since 4.11
166
167
168
169
170
171OCamldoc 2022-02-04 Seq(3)