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