1Seq(3) OCaml library Seq(3)
2
3
4
6 Seq - Functional iterators.
7
9 Module Seq
10
12 Module Seq
13 : sig end
14
15
16 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 flat_map : ('a -> 'b t) -> 'a t -> 'b t
111
112 Map each element to a subsequence, then return each element of this
113 sub-sequence in turn. This transformation is lazy, it only applies
114 when the result is traversed.
115
116
117
118 val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
119
120 Traverse the sequence from left to right, combining each element with
121 the accumulator using the given function. The traversal happens imme‐
122 diately and will not terminate on infinite sequences.
123
124 Also see List.fold_left
125
126
127
128
129 val iter : ('a -> unit) -> 'a t -> unit
130
131 Iterate on the sequence, calling the (imperative) function on every el‐
132 ement. The traversal happens immediately and will not terminate on in‐
133 finite sequences.
134
135
136
137 val unfold : ('b -> ('a * 'b) option) -> 'b -> 'a t
138
139 Build a sequence from a step function and an initial value. unfold f u
140 returns empty if f u returns None , or fun () -> Cons (x, unfold f y)
141 if f u returns Some (x, y) .
142
143 For example, unfold (function [] -> None | h::t -> Some (h,t)) l is
144 equivalent to List.to_seq l .
145
146
147 Since 4.11
148
149
150
151
152
153OCamldoc 2021-07-22 Seq(3)