chore: rename to iter

This commit is contained in:
Simon Cruanes 2018-12-13 18:31:41 -06:00
parent 20e32f8509
commit 97d4ac78a5
16 changed files with 133 additions and 125 deletions

View file

@ -1,36 +1,36 @@
= Sequence = Iter
:toc: macro :toc: macro
:source-highlighter: pygments :source-highlighter: pygments
Simple sequence abstract datatype, intended to iterate efficiently Simple abstraction over `iter` functions, intended to iterate efficiently
on collections while performing some transformations. on collections while performing some transformations. Used to be called `Sequence`.
Common operations supported by Sequence include Common operations supported by `Iter` include
`filter`, `map`, `take`, `drop`, `append`, `flat_map`, etc. `filter`, `map`, `take`, `drop`, `append`, `flat_map`, etc.
Sequence is not designed to be as general-purpose or flexible as, say, `Iter` is not designed to be as general-purpose or flexible as, say,
Batteries' `'a Enum.t`. Rather, it aims at providing a very simple and efficient Batteries' `'a Enum.t`. Rather, it aims at providing a very simple and efficient
way of iterating on a finite number of values, only allocating (most of the time) way of iterating on a finite number of values, only allocating (most of the time)
one intermediate closure to do so. For instance, iterating on keys, or values, one intermediate closure to do so. For instance, iterating on keys, or values,
of a `Hashtbl.t`, without creating a list. of a `Hashtbl.t`, without creating a list.
image::https://travis-ci.org/c-cube/sequence.svg?branch=master[alt="Build Status", link="https://travis-ci.org/c-cube/sequence"] image::https://travis-ci.org/c-cube/iter.svg?branch=master[alt="Build Status", link="https://travis-ci.org/c-cube/iter"]
toc::[] toc::[]
== Documentation == Documentation
There is only one important type, `'a Sequence.t`, and lots of functions built There is only one important type, `'a Iter.t`, and lots of functions built
around this type. around this type.
To get an overview of sequence, its origins and why it was created, To get an overview of iter (originally "sequence"), its origins and why it was created,
you can start with http://cedeela.fr/~simon/talks/sequence.pdf[the slides of a talk] you can start with http://simon.cedeela.fr/assets/talks/sequence.pdf[the slides of a talk]
I (@c-cube) made at some OCaml meeting. I (@c-cube) made at some OCaml meeting.
See https://c-cube.github.io/sequence/[the online API] See https://c-cube.github.io/iter/[the online API]
for more details on the set of available functions. for more details on the set of available functions.
== Build == Build
1. via opam `opam install sequence` 1. via opam `opam install iter`
2. manually (need OCaml >= 4.02.0): `make all install` 2. manually (need OCaml >= 4.02.0): `make all install`
If you have https://github.com/vincent-hugot/iTeML[qtest] installed, If you have https://github.com/vincent-hugot/iTeML[qtest] installed,
@ -51,7 +51,7 @@ $ ./benchs.native
To see how to use the library, check the `examples` directory. To see how to use the library, check the `examples` directory.
`tests.ml` has a few examples of how to convert basic data structures into `tests.ml` has a few examples of how to convert basic data structures into
sequences, and conversely. iterators, and conversely.
== Short Tutorial == Short Tutorial
@ -60,21 +60,21 @@ sequences, and conversely.
Conversion between n container types Conversion between n container types
would take n² functions. In practice, for a given collection would take n² functions. In practice, for a given collection
we can at best hope for `to_list` and `of_list`. we can at best hope for `to_list` and `of_list`.
With sequence, if the source structure provides a With iter, if the source structure provides a
`iter` function (or a `to_seq` wrapper), it becomes: `iter` function (or a `to_iter` wrapper), it becomes:
[source,OCaml] [source,OCaml]
---- ----
# let q = Queue.create();; # let q = Queue.create();;
# Sequence.( 1 -- 10 |> to_queue q);; # Iter.( 1 -- 10 |> to_queue q);;
- : unit = () - : unit = ()
# Sequence.of_queue q |> Sequence.to_list ;; # Iter.of_queue q |> Iter.to_list ;;
- : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] - : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10]
# let s = Stack.create();; # let s = Stack.create();;
# Sequence.(of_queue q |> to_stack s);; # Iter.(of_queue q |> to_stack s);;
- : unit = () - : unit = ()
# Sequence.of_stack s |> Sequence.to_list ;; # Iter.of_stack s |> Iter.to_list ;;
- : int list = [10; 9; 8; 7; 6; 5; 4; 3; 2; 1] - : int list = [10; 9; 8; 7; 6; 5; 4; 3; 2; 1]
---- ----
@ -97,7 +97,7 @@ underlying hash function):
- : int = 11 - : int = 11
(* now to get the values *) (* now to get the values *)
# Sequence.of_hashtbl h |> Sequence.map snd |> Sequence.to_list;; # Iter.of_hashtbl h |> Iter.map snd |> Iter.to_list;;
- : string list = ["6"; "2"; "8"; "7"; "3"; "5"; "4"; "9"; "0"; "10"; "1"] - : string list = ["6"; "2"; "8"; "7"; "3"; "5"; "4"; "9"; "0"; "10"; "1"]
---- ----
@ -105,15 +105,15 @@ underlying hash function):
The `for` loop is a bit limited, and lacks compositionality. The `for` loop is a bit limited, and lacks compositionality.
Instead, it can be more convenient and readable to Instead, it can be more convenient and readable to
use `Sequence.(--) : int -> int -> int Sequence.t`. use `Iter.(--) : int -> int -> int Iter.t`.
[source,OCaml] [source,OCaml]
---- ----
# Sequence.(1 -- 10_000_000 |> fold (+) 0);; # Iter.(1 -- 10_000_000 |> fold (+) 0);;
- : int = 50000005000000 - : int = 50000005000000
# let p x = x mod 5 = 0 in # let p x = x mod 5 = 0 in
Sequence.(1 -- 5_000 Iter.(1 -- 5_000
|> filter p |> filter p
|> map (fun x -> x * x) |> map (fun x -> x * x)
|> fold (+) 0 |> fold (+) 0
@ -136,28 +136,28 @@ A small λ-calculus AST, and some operations on it.
| App of term * term | App of term * term
| Lambda of term ;; | Lambda of term ;;
# let rec subterms : term -> term Sequence.t = # let rec subterms : term -> term Iter.t =
fun t -> fun t ->
let open Sequence.Infix in let open Iter.Infix in
Sequence.cons t Iter.cons t
(match t with (match t with
| Var _ -> Sequence.empty | Var _ -> Iter.empty
| Lambda u -> subterms u | Lambda u -> subterms u
| App (a,b) -> | App (a,b) ->
Sequence.append (subterms a) (subterms b)) Iter.append (subterms a) (subterms b))
;; ;;
(* Now we can define many other functions easily! *) (* Now we can define many other functions easily! *)
# let vars t = # let vars t =
Sequence.filter_map Iter.filter_map
(function Var s -> Some s | _ -> None) (function Var s -> Some s | _ -> None)
(subterms t) ;; (subterms t) ;;
val vars : term -> string sequence = <fun > val vars : term -> string sequence = <fun >
# let size t = Sequence.length (subterms t) ;; # let size t = Iter.length (subterms t) ;;
val size : term -> int = <fun > val size : term -> int = <fun >
# let vars_list l = Sequence.(of_list l |> flat_map vars);; # let vars_list l = Iter.(of_list l |> flat_map vars);;
val vars_list : term list -> string sequence = <fun > val vars_list : term list -> string sequence = <fun >
---- ----
@ -165,14 +165,14 @@ val vars_list : term list -> string sequence = <fun >
Makes it easy to write backtracking code (a non-deterministic Makes it easy to write backtracking code (a non-deterministic
function returning several `'a` function returning several `'a`
will just return a `'a Sequence.t`). will just return a `'a Iter.t`).
Here, we generate all permutations of a list by Here, we generate all permutations of a list by
enumerating the ways we can insert an element in a list. enumerating the ways we can insert an element in a list.
[source,OCaml] [source,OCaml]
---- ----
# open Sequence.Infix;; # open Iter.Infix;;
# module S = Sequence ;; # module S = Iter ;;
# let rec insert x l = match l with # let rec insert x l = match l with
| [] -> S.return [x] | [] -> S.return [x]
| y :: tl -> | y :: tl ->
@ -218,25 +218,25 @@ let zip (g1: 'a Gen.t) (g2:'b Gen.t) : ('a * 'b) Gen.t =
Some (x,y) Some (x,y)
---- ----
- `Sequence` is an *internal* iterator. When one wishes to iterate over - `Iter` is an *internal* iterator. When one wishes to iterate over
an `'a Sequence.t`, one has to give a callback `f : 'a -> unit` an `'a Iter.t`, one has to give a callback `f : 'a -> unit`
that is called in succession over every element of the sequence. that is called in succession over every element of the sequence.
Control is not handed back to the caller before the whole iteration is over. Control is not handed back to the caller before the whole iteration is over.
This makes `zip` impossible to implement. However, the type `'a Sequence.t` This makes `zip` impossible to implement. However, the type `'a Iter.t`
is general enough that it can be extracted from any classic `iter` function, is general enough that it can be extracted from any classic `iter` function,
including from data structures such as `Map.S.t` or `Set.S.t` or `Hashtbl.t`; including from data structures such as `Map.S.t` or `Set.S.t` or `Hashtbl.t`;
one cannot obtain a `'a Gen.t` from these without having access to the internal one cannot obtain a `'a Gen.t` from these without having access to the internal
data structure. data structure.
In short, `'a Gen.t` is more expressive than `'a Sequence.t`, but it also In short, `'a Gen.t` is more expressive than `'a Iter.t`, but it also
requires more knowledge of the underlying source of items. requires more knowledge of the underlying source of items.
For some operations such as `map` or `flat_map`, Sequence is also extremely For some operations such as `map` or `flat_map`, Iter is also extremely
efficient and will, if flambda permits, be totally removed at efficient and will, if flambda permits, be totally removed at
compile time (e.g. `Sequence.(--)` becomes a for loop, and `Sequence.filter` compile time (e.g. `Iter.(--)` becomes a for loop, and `Iter.filter`
becomes a if test). becomes a if test).
For more details, you can read http://gallium.inria.fr/blog/generators-iterators-control-and-continuations/ . For more details, you can read http://gallium.inria.fr/blog/generators-iterators-control-and-continuations/ .
== License == License
Sequence is available under the BSD license. Iter is available under the BSD license.

View file

@ -1,13 +1,13 @@
QTEST_PREAMBLE='' QTEST_PREAMBLE=''
DONTTEST=../src/sequenceLabels.ml ../src/mkflags.ml DONTTEST=../src/iterLabels.ml ../src/mkflags.ml
QTESTABLE=$(filter-out $(DONTTEST), \ QTESTABLE=$(filter-out $(DONTTEST), \
$(wildcard ../src/*.ml) \ $(wildcard ../src/*.ml) \
$(wildcard ../src/*.mli) \ $(wildcard ../src/*.mli) \
) )
qtest-gen: qtest-gen:
@rm run_qtest.ml || true @rm run_qtest.ml 2>/dev/null || true
@if which qtest > /dev/null ; then \ @if which qtest > /dev/null ; then \
qtest extract --preamble $(QTEST_PREAMBLE) \ qtest extract --preamble $(QTEST_PREAMBLE) \
-o run_qtest.ml \ -o run_qtest.ml \

View file

@ -11,7 +11,7 @@
(executable (executable
(name run_qtest) (name run_qtest)
(flags :standard -warn-error -a+8 -safe-string -w -33) (flags :standard -warn-error -a+8 -safe-string -w -33)
(libraries sequence qcheck) (libraries iter qcheck)
) )
(alias (alias

View file

@ -4,26 +4,18 @@ version: "1.1"
author: "Simon Cruanes" author: "Simon Cruanes"
maintainer: "simon.cruanes.2007@m4x.org" maintainer: "simon.cruanes.2007@m4x.org"
license: "BSD-2-clauses" license: "BSD-2-clauses"
synopsis: "Simple sequence abstract datatype, intended to iterate efficiently synopsis: "compatibility package for `iter`"
on collections while performing some transformations"
build: [ build: [
["dune" "build" "@install" "-p" name "-j" jobs] ["dune" "build" "@install" "-p" name "-j" jobs]
["dune" "build" "@doc" "-p" name] {with-doc}
["dune" "runtest" "-p" name] {with-test}
] ]
depends: [ depends: [
"base-bytes" "iter"
"result"
"dune" {build}
"qcheck" {with-test}
"qtest" {with-test}
"odoc" {with-doc}
] ]
tags: [ "sequence" "iterator" "iter" "fold" ] tags: [ "sequence" "iterator" "iter" "fold" ]
homepage: "https://github.com/c-cube/sequence/" homepage: "https://github.com/c-cube/iter/"
depopts: [ depopts: [
"base-bigarray" "base-bigarray"
] ]
doc: "https://c-cube.github.io/sequence/" doc: "https://c-cube.github.io/iter/"
bug-reports: "https://github.com/c-cube/sequence/issues" bug-reports: "https://github.com/c-cube/iter/issues"
dev-repo: "git+https://github.com/c-cube/sequence.git" dev-repo: "git+https://github.com/c-cube/iter.git"

View file

@ -1,12 +1,12 @@
(* This file is free software, part of sequence. See file "license" for more details. *) (* This file is free software, part of iter. See file "license" for more details. *)
(** {1 Simple and Efficient Iterators} *) (** {1 Simple and Efficient Iterators} *)
(** Sequence abstract iterator type *) (** Iter abstract iterator type *)
type 'a t = ('a -> unit) -> unit type 'a t = ('a -> unit) -> unit
type 'a sequence = 'a t type 'a iter = 'a t
(*$inject (*$inject
let pp_ilist = Q.Print.(list int) let pp_ilist = Q.Print.(list int)
@ -15,7 +15,7 @@ type 'a sequence = 'a t
type 'a equal = 'a -> 'a -> bool type 'a equal = 'a -> 'a -> bool
type 'a hash = 'a -> int type 'a hash = 'a -> int
(** Build a sequence from a iter function *) (** Build an iterator from a iter function *)
let from_iter f = f let from_iter f = f
let rec from_fun f k = match f () with let rec from_fun f k = match f () with
@ -803,7 +803,7 @@ let head seq =
let head_exn seq = let head_exn seq =
match head seq with match head seq with
| None -> invalid_arg "Sequence.head_exn" | None -> invalid_arg "Iter.head_exn"
| Some x -> x | Some x -> x
exception ExitTake exception ExitTake
@ -967,7 +967,7 @@ let is_empty seq =
try seq (fun _ -> raise_notrace ExitIsEmpty); true try seq (fun _ -> raise_notrace ExitIsEmpty); true
with ExitIsEmpty -> false with ExitIsEmpty -> false
(** {2 Transform a sequence} *) (** {2 Transform an iterator} *)
let zip_i seq k = let zip_i seq k =
let r = ref 0 in let r = ref 0 in
@ -1181,13 +1181,13 @@ let to_klist seq =
let l = MList.of_seq seq in let l = MList.of_seq seq in
MList.to_klist l MList.to_klist l
(** {2 Functorial conversions between sets and sequences} *) (** {2 Functorial conversions between sets and iterators} *)
module Set = struct module Set = struct
module type S = sig module type S = sig
include Set.S include Set.S
val of_seq : elt sequence -> t val of_seq : elt iter -> t
val to_seq : t -> elt sequence val to_seq : t -> elt iter
val to_list : t -> elt list val to_list : t -> elt list
val of_list : elt list -> t val of_list : elt list -> t
end end
@ -1212,20 +1212,20 @@ module Set = struct
end end
end end
(** {2 Conversion between maps and sequences.} *) (** {2 Conversion between maps and iterators.} *)
module Map = struct module Map = struct
module type S = sig module type S = sig
include Map.S include Map.S
val to_seq : 'a t -> (key * 'a) sequence val to_seq : 'a t -> (key * 'a) iter
val of_seq : (key * 'a) sequence -> 'a t val of_seq : (key * 'a) iter -> 'a t
val keys : 'a t -> key sequence val keys : 'a t -> key iter
val values : 'a t -> 'a sequence val values : 'a t -> 'a iter
val to_list : 'a t -> (key * 'a) list val to_list : 'a t -> (key * 'a) list
val of_list : (key * 'a) list -> 'a t val of_list : (key * 'a) list -> 'a t
end end
(** Adapt a pre-existing Map module to make it sequence-aware *) (** Adapt a pre-existing Map module to make it iterator-aware *)
module Adapt(M : Map.S) = struct module Adapt(M : Map.S) = struct
let to_seq_ m = from_iter (fun k -> M.iter (fun x y -> k (x,y)) m) let to_seq_ m = from_iter (fun k -> M.iter (fun x y -> k (x,y)) m)
@ -1244,14 +1244,14 @@ module Map = struct
let of_seq = of_seq_ let of_seq = of_seq_
end end
(** Create an enriched Map module, with sequence-aware functions *) (** Create an enriched Map module, with iterator-aware functions *)
module Make(V : Map.OrderedType) : S with type key = V.t = struct module Make(V : Map.OrderedType) : S with type key = V.t = struct
module M = Map.Make(V) module M = Map.Make(V)
include Adapt(M) include Adapt(M)
end end
end end
(** {2 Infinite sequences of random values} *) (** {2 Infinite iterators of random values} *)
let random_int bound = forever (fun () -> Random.int bound) let random_int bound = forever (fun () -> Random.int bound)
@ -1359,9 +1359,9 @@ end
include Infix include Infix
(** {2 Pretty printing of sequences} *) (** {2 Pretty printing of iterators} *)
(** Pretty print a sequence of ['a], using the given pretty printer (** Pretty print an ['a iter], using the given pretty printer
to print each elements. An optional separator string can be provided. *) to print each elements. An optional separator string can be provided. *)
let pp_seq ?(sep=", ") pp_elt formatter seq = let pp_seq ?(sep=", ") pp_elt formatter seq =
let first = ref true in let first = ref true in

View file

@ -1,5 +1,5 @@
(* This file is free software, part of sequence. See file "license" for more details. *) (* This file is free software, part of iter. See file "license" for more details. *)
(** {1 Simple and Efficient Iterators} *) (** {1 Simple and Efficient Iterators} *)
@ -34,7 +34,7 @@ type +'a t = ('a -> unit) -> unit
(** A sequence of values of type ['a]. If you give it a function ['a -> unit] (** A sequence of values of type ['a]. If you give it a function ['a -> unit]
it will be applied to every element of the sequence successively. *) it will be applied to every element of the sequence successively. *)
type +'a sequence = 'a t type +'a iter = 'a t
(** {b NOTE} Type [('a, 'b) t2 = ('a -> 'b -> unit) -> unit] (** {b NOTE} Type [('a, 'b) t2 = ('a -> 'b -> unit) -> unit]
has been removed and subsumed by [('a * 'b) t] @since 1.0 *) has been removed and subsumed by [('a * 'b) t] @since 1.0 *)
@ -58,7 +58,7 @@ val singleton : 'a -> 'a t
(** Singleton sequence, with exactly one element. *) (** Singleton sequence, with exactly one element. *)
val doubleton : 'a -> 'a -> 'a t val doubleton : 'a -> 'a -> 'a t
(** Sequence with exactly two elements *) (** Iterator with exactly two elements *)
val init : (int -> 'a) -> 'a t val init : (int -> 'a) -> 'a t
(** [init f] is the infinite sequence [f 0; f 1; f 2; …]. (** [init f] is the infinite sequence [f 0; f 1; f 2; …].
@ -85,7 +85,7 @@ val iterate : ('a -> 'a) -> 'a -> 'a t
(** [iterate f x] is the infinite sequence [x, f(x), f(f(x)), ...] *) (** [iterate f x] is the infinite sequence [x, f(x), f(f(x)), ...] *)
val forever : (unit -> 'b) -> 'b t val forever : (unit -> 'b) -> 'b t
(** Sequence that calls the given function to produce elements. (** Iterator that calls the given function to produce elements.
The sequence may be transient (depending on the function), and definitely The sequence may be transient (depending on the function), and definitely
is infinite. You may want to use {!take} and {!persistent}. *) is infinite. You may want to use {!take} and {!persistent}. *)
@ -420,7 +420,7 @@ val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a t
and unfoldr recurses with [b']. *) and unfoldr recurses with [b']. *)
val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t
(** Sequence of intermediate results *) (** Iterator of intermediate results *)
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a option val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a option
(** Max element of the sequence, using the given comparison function. (** Max element of the sequence, using the given comparison function.
@ -531,7 +531,7 @@ val of_array_i : 'a array -> (int * 'a) t
(** Elements of the array, with their index *) (** Elements of the array, with their index *)
val array_slice : 'a array -> int -> int -> 'a t val array_slice : 'a array -> int -> int -> 'a t
(** [array_slice a i j] Sequence of elements whose indexes range (** [array_slice a i j] Iterator of elements whose indexes range
from [i] to [j] *) from [i] to [j] *)
val of_opt : 'a option -> 'a t val of_opt : 'a option -> 'a t
@ -539,7 +539,7 @@ val of_opt : 'a option -> 'a t
@since 0.5.1 *) @since 0.5.1 *)
val of_stream : 'a Stream.t -> 'a t val of_stream : 'a Stream.t -> 'a t
(** Sequence of elements of a stream (usable only once) *) (** Iterator of elements of a stream (usable only once) *)
val to_stream : 'a t -> 'a Stream.t val to_stream : 'a t -> 'a Stream.t
(** Convert to a stream. linear in memory and time (a copy is made in memory) *) (** Convert to a stream. linear in memory and time (a copy is made in memory) *)
@ -548,13 +548,13 @@ val to_stack : 'a Stack.t -> 'a t -> unit
(** Push elements of the sequence on the stack *) (** Push elements of the sequence on the stack *)
val of_stack : 'a Stack.t -> 'a t val of_stack : 'a Stack.t -> 'a t
(** Sequence of elements of the stack (same order as [Stack.iter]) *) (** Iterator of elements of the stack (same order as [Stack.iter]) *)
val to_queue : 'a Queue.t -> 'a t -> unit val to_queue : 'a Queue.t -> 'a t -> unit
(** Push elements of the sequence into the queue *) (** Push elements of the sequence into the queue *)
val of_queue : 'a Queue.t -> 'a t val of_queue : 'a Queue.t -> 'a t
(** Sequence of elements contained in the queue, FIFO order *) (** Iterator of elements contained in the queue, FIFO order *)
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit
(** Add elements of the sequence to the hashtable, with (** Add elements of the sequence to the hashtable, with
@ -568,7 +568,7 @@ val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.t
(** Build a hashtable from a sequence of key/value pairs *) (** Build a hashtable from a sequence of key/value pairs *)
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) t val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) t
(** Sequence of key/value pairs from the hashtable *) (** Iterator of key/value pairs from the hashtable *)
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a t val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a t
val hashtbl_values : ('a, 'b) Hashtbl.t -> 'b t val hashtbl_values : ('a, 'b) Hashtbl.t -> 'b t
@ -638,8 +638,8 @@ val to_klist : 'a t -> 'a klist
module Set : sig module Set : sig
module type S = sig module type S = sig
include Set.S include Set.S
val of_seq : elt sequence -> t val of_seq : elt iter -> t
val to_seq : t -> elt sequence val to_seq : t -> elt iter
val to_list : t -> elt list val to_list : t -> elt list
val of_list : elt list -> t val of_list : elt list -> t
end end
@ -656,10 +656,10 @@ end
module Map : sig module Map : sig
module type S = sig module type S = sig
include Map.S include Map.S
val to_seq : 'a t -> (key * 'a) sequence val to_seq : 'a t -> (key * 'a) iter
val of_seq : (key * 'a) sequence -> 'a t val of_seq : (key * 'a) iter -> 'a t
val keys : 'a t -> key sequence val keys : 'a t -> key iter
val values : 'a t -> 'a sequence val values : 'a t -> 'a iter
val to_list : 'a t -> (key * 'a) list val to_list : 'a t -> (key * 'a) list
val of_list : (key * 'a) list -> 'a t val of_list : (key * 'a) list -> 'a t
end end
@ -683,7 +683,7 @@ val random_bool : bool t
val random_float : float -> float t val random_float : float -> float t
val random_array : 'a array -> 'a t val random_array : 'a array -> 'a t
(** Sequence of choices of an element in the array *) (** Iterator of choices of an element in the array *)
val random_list : 'a list -> 'a t val random_list : 'a list -> 'a t
(** Infinite sequence of random elements of the list. Basically the (** Infinite sequence of random elements of the list. Basically the
@ -765,19 +765,19 @@ val to_string : ?sep:string -> ('a -> string) -> 'a t -> string
Example: copy a file ["a"] into file ["b"], removing blank lines: Example: copy a file ["a"] into file ["b"], removing blank lines:
{[ {[
Sequence.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");; Iterator.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;
]} ]}
By chunks of [4096] bytes: By chunks of [4096] bytes:
{[ {[
Sequence.IO.(chunks_of ~size:4096 "a" |> write_to "b");; Iterator.IO.(chunks_of ~size:4096 "a" |> write_to "b");;
]} ]}
Read the lines of a file into a list: Read the lines of a file into a list:
{[ {[
Sequence.IO.lines "a" |> Sequence.to_list Iterator.IO.lines "a" |> Iterator.to_list
]} ]}
@since 0.5.1 *) @since 0.5.1 *)

1
src/IterLabels.ml Normal file
View file

@ -0,0 +1 @@
include Iter

View file

@ -4,7 +4,7 @@
(** {1 Simple and Efficient Iterators} (** {1 Simple and Efficient Iterators}
Version of {!Sequence} with labels Version of {!Iterator} with labels
@since 0.5.5 *) @since 0.5.5 *)
@ -12,7 +12,7 @@ type +'a t = ('a -> unit) -> unit
(** A sequence of values of type ['a]. If you give it a function ['a -> unit] (** A sequence of values of type ['a]. If you give it a function ['a -> unit]
it will be applied to every element of the sequence successively. *) it will be applied to every element of the sequence successively. *)
type +'a sequence = 'a t type +'a iter = 'a t
(** {b NOTE} Type [('a, 'b) t2 = ('a -> 'b -> unit) -> unit] (** {b NOTE} Type [('a, 'b) t2 = ('a -> 'b -> unit) -> unit]
has been removed and subsumed by [('a * 'b) t] @since 1.0 *) has been removed and subsumed by [('a * 'b) t] @since 1.0 *)
@ -36,7 +36,7 @@ val singleton : 'a -> 'a t
(** Singleton sequence, with exactly one element. *) (** Singleton sequence, with exactly one element. *)
val doubleton : 'a -> 'a -> 'a t val doubleton : 'a -> 'a -> 'a t
(** Sequence with exactly two elements *) (** Iterator with exactly two elements *)
val init : f:(int -> 'a) -> 'a t val init : f:(int -> 'a) -> 'a t
(** [init f] is the infinite sequence [f 0; f 1; f 2; …]. (** [init f] is the infinite sequence [f 0; f 1; f 2; …].
@ -63,7 +63,7 @@ val iterate : ('a -> 'a) -> 'a -> 'a t
(** [iterate f x] is the infinite sequence [x, f(x), f(f(x)), ...] *) (** [iterate f x] is the infinite sequence [x, f(x), f(f(x)), ...] *)
val forever : (unit -> 'b) -> 'b t val forever : (unit -> 'b) -> 'b t
(** Sequence that calls the given function to produce elements. (** Iterator that calls the given function to produce elements.
The sequence may be transient (depending on the function), and definitely The sequence may be transient (depending on the function), and definitely
is infinite. You may want to use {!take} and {!persistent}. *) is infinite. You may want to use {!take} and {!persistent}. *)
@ -392,7 +392,7 @@ val unfoldr : ('b -> ('a * 'b) option) -> 'b -> 'a t
and unfoldr recurses with [b']. *) and unfoldr recurses with [b']. *)
val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t val scan : ('b -> 'a -> 'b) -> 'b -> 'a t -> 'b t
(** Sequence of intermediate results *) (** Iterator of intermediate results *)
val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a option val max : ?lt:('a -> 'a -> bool) -> 'a t -> 'a option
(** Max element of the sequence, using the given comparison function. (** Max element of the sequence, using the given comparison function.
@ -503,7 +503,7 @@ val of_array_i : 'a array -> (int * 'a) t
(** Elements of the array, with their index *) (** Elements of the array, with their index *)
val array_slice : 'a array -> int -> int -> 'a t val array_slice : 'a array -> int -> int -> 'a t
(** [array_slice a i j] Sequence of elements whose indexes range (** [array_slice a i j] Iterator of elements whose indexes range
from [i] to [j] *) from [i] to [j] *)
val of_opt : 'a option -> 'a t val of_opt : 'a option -> 'a t
@ -511,7 +511,7 @@ val of_opt : 'a option -> 'a t
@since 0.5.1 *) @since 0.5.1 *)
val of_stream : 'a Stream.t -> 'a t val of_stream : 'a Stream.t -> 'a t
(** Sequence of elements of a stream (usable only once) *) (** Iterator of elements of a stream (usable only once) *)
val to_stream : 'a t -> 'a Stream.t val to_stream : 'a t -> 'a Stream.t
(** Convert to a stream. linear in memory and time (a copy is made in memory) *) (** Convert to a stream. linear in memory and time (a copy is made in memory) *)
@ -520,13 +520,13 @@ val to_stack : 'a Stack.t -> 'a t -> unit
(** Push elements of the sequence on the stack *) (** Push elements of the sequence on the stack *)
val of_stack : 'a Stack.t -> 'a t val of_stack : 'a Stack.t -> 'a t
(** Sequence of elements of the stack (same order as [Stack.iter]) *) (** Iterator of elements of the stack (same order as [Stack.iter]) *)
val to_queue : 'a Queue.t -> 'a t -> unit val to_queue : 'a Queue.t -> 'a t -> unit
(** Push elements of the sequence into the queue *) (** Push elements of the sequence into the queue *)
val of_queue : 'a Queue.t -> 'a t val of_queue : 'a Queue.t -> 'a t
(** Sequence of elements contained in the queue, FIFO order *) (** Iterator of elements contained in the queue, FIFO order *)
val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit val hashtbl_add : ('a, 'b) Hashtbl.t -> ('a * 'b) t -> unit
(** Add elements of the sequence to the hashtable, with (** Add elements of the sequence to the hashtable, with
@ -540,7 +540,7 @@ val to_hashtbl : ('a * 'b) t -> ('a, 'b) Hashtbl.t
(** Build a hashtable from a sequence of key/value pairs *) (** Build a hashtable from a sequence of key/value pairs *)
val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) t val of_hashtbl : ('a, 'b) Hashtbl.t -> ('a * 'b) t
(** Sequence of key/value pairs from the hashtable *) (** Iterator of key/value pairs from the hashtable *)
val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a t val hashtbl_keys : ('a, 'b) Hashtbl.t -> 'a t
val hashtbl_values : ('a, 'b) Hashtbl.t -> 'b t val hashtbl_values : ('a, 'b) Hashtbl.t -> 'b t
@ -611,8 +611,8 @@ val to_klist : 'a t -> 'a klist
module Set : sig module Set : sig
module type S = sig module type S = sig
include Set.S include Set.S
val of_seq : elt sequence -> t val of_seq : elt iter -> t
val to_seq : t -> elt sequence val to_seq : t -> elt iter
val to_list : t -> elt list val to_list : t -> elt list
val of_list : elt list -> t val of_list : elt list -> t
end end
@ -629,10 +629,10 @@ end
module Map : sig module Map : sig
module type S = sig module type S = sig
include Map.S include Map.S
val to_seq : 'a t -> (key * 'a) sequence val to_seq : 'a t -> (key * 'a) iter
val of_seq : (key * 'a) sequence -> 'a t val of_seq : (key * 'a) iter -> 'a t
val keys : 'a t -> key sequence val keys : 'a t -> key iter
val values : 'a t -> 'a sequence val values : 'a t -> 'a iter
val to_list : 'a t -> (key * 'a) list val to_list : 'a t -> (key * 'a) list
val of_list : (key * 'a) list -> 'a t val of_list : (key * 'a) list -> 'a t
end end
@ -656,7 +656,7 @@ val random_bool : bool t
val random_float : float -> float t val random_float : float -> float t
val random_array : 'a array -> 'a t val random_array : 'a array -> 'a t
(** Sequence of choices of an element in the array *) (** Iterator of choices of an element in the array *)
val random_list : 'a list -> 'a t val random_list : 'a list -> 'a t
(** Infinite sequence of random elements of the list. Basically the (** Infinite sequence of random elements of the list. Basically the
@ -739,19 +739,19 @@ val to_string : ?sep:string -> ('a -> string) -> 'a t -> string
Example: copy a file ["a"] into file ["b"], removing blank lines: Example: copy a file ["a"] into file ["b"], removing blank lines:
{[ {[
Sequence.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");; Iterator.(IO.lines_of "a" |> filter (fun l-> l<> "") |> IO.write_lines "b");;
]} ]}
By chunks of [4096] bytes: By chunks of [4096] bytes:
{[ {[
Sequence.IO.(chunks_of ~size:4096 "a" |> write_to "b");; Iterator.IO.(chunks_of ~size:4096 "a" |> write_to "b");;
]} ]}
Read the lines of a file into a list: Read the lines of a file into a list:
{[ {[
Sequence.IO.lines "a" |> Sequence.to_list Iterator.IO.lines "a" |> Iterator.to_list
]} ]}
@since 0.5.1 *) @since 0.5.1 *)

View file

@ -1 +0,0 @@
include Sequence

View file

@ -5,8 +5,8 @@
@since 0.5.4 *) @since 0.5.4 *)
val of_bigarray : ('a, _, _) Bigarray.Array1.t -> 'a Sequence.t val of_bigarray : ('a, _, _) Bigarray.Array1.t -> 'a Iter.t
(** Iterate on the elements of a 1-D array *) (** Iterate on the elements of a 1-D array *)
val mmap : string -> char Sequence.t val mmap : string -> char Iter.t
(** Map the file into memory, and read the characters. *) (** Map the file into memory, and read the characters. *)

View file

@ -1,8 +1,8 @@
(library (library
(name sequence_bigarray) (name iter_bigarray)
(public_name sequence.bigarray) (public_name iter.bigarray)
(libraries sequence bigarray) (libraries iter bigarray)
(wrapped false) (wrapped false)
(optional) (optional)
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string) (flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string)

View file

@ -7,10 +7,10 @@
) )
(library (library
(name sequence) (name iter)
(public_name sequence) (public_name iter)
(wrapped false) (wrapped false)
(modules Sequence SequenceLabels) (modules Iter IterLabels)
(flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string -nolabels) (flags :standard -w +a-4-42-44-48-50-58-32-60@8 -safe-string -nolabels)
(ocamlopt_flags :standard (:include flambda.flags)) (ocamlopt_flags :standard (:include flambda.flags))
(libraries bytes result) (libraries bytes result)

6
src/sequence/Sequence.ml Normal file
View file

@ -0,0 +1,6 @@
[@@@ocaml.deprecated "the package is now `Iter`"]
include Iter
type 'a sequence = 'a iter

View file

@ -0,0 +1,4 @@
[@@@ocaml.deprecated "the package is now `Iter`"]
include IterLabels

6
src/sequence/dune Normal file
View file

@ -0,0 +1,6 @@
(library
(name sequence)
(public_name sequence)
(wrapped false)
(libraries iter))