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