diff --git a/.travis.yml b/.travis.yml index 1edda16..740eda7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,7 +8,7 @@ env: - PINS="iter:." - DISTRO="ubuntu-16.04" matrix: - - PACKAGE="iter" OCAML_VERSION="4.02.3" EXTRA_DEPS="base-bigarray" + - PACKAGE="iter" OCAML_VERSION="4.02.3" EXTRA_DEPS="base-bigarray" TESTS=false - PACKAGE="iter" OCAML_VERSION="4.03" EXTRA_DEPS="base-bigarray" - PACKAGE="iter" OCAML_VERSION="4.04" EXTRA_DEPS="base-bigarray" #- PACKAGE="iter" OCAML_VERSION="4.05" EXTRA_DEPS="base-bigarray" diff --git a/README.md b/README.md index a45da89..cfe2c86 100644 --- a/README.md +++ b/README.md @@ -57,17 +57,20 @@ With iter, if the source structure provides a `iter` function (or a `to_iter` wrapper), it becomes: ```ocaml -# let q = Queue.create();; +# #require "iter";; +# let q : int Queue.t = Queue.create();; +val q : int Queue.t = # Iter.( 1 -- 10 |> to_queue q);; - : unit = () # Iter.of_queue q |> Iter.to_list ;; - : int list = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10] -# let s = Stack.create();; +# let s : int Stack.t = Stack.create();; +val s : int Stack.t = # Iter.(of_queue q |> to_stack s);; - : unit = () # 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] ``` Note how the list of elements is reversed when we transfer them @@ -78,7 +81,8 @@ a hashtable (in an undefined order that depends on the underlying hash function): ```ocaml -# let h = Hashtbl.create 16;; +# let h: (int, string) Hashtbl.t = Hashtbl.create 16;; +val h : (int, string) Hashtbl.t = # for i = 0 to 10 do Hashtbl.add h i (string_of_int i) done;; @@ -87,9 +91,9 @@ underlying hash function): # Hashtbl.length h;; - : int = 11 -(* now to get the values *) -# Iter.of_hashtbl h |> Iter.map snd |> Iter.to_list;; -- : string list = ["6"; "2"; "8"; "7"; "3"; "5"; "4"; "9"; "0"; "10"; "1"] +# (* now to get the values *) + Iter.of_hashtbl h |> Iter.map snd |> Iter.to_list;; +- : string list = ["6"; "2"; "8"; "7"; "3"; "5"; "4"; "9"; "0"; "10"; "1"] ``` ### Replacing `for` loops @@ -124,6 +128,7 @@ A small λ-calculus AST, and some operations on it. | Var of string | App of term * term | Lambda of term ;; +type term = Var of string | App of term * term | Lambda of term # let rec subterms : term -> term Iter.t = fun t -> @@ -135,19 +140,20 @@ A small λ-calculus AST, and some operations on it. | App (a,b) -> Iter.append (subterms a) (subterms b)) ;; - -(* Now we can define many other functions easily! *) -# let vars t = +val subterms : term -> term Iter.t = + +# (* Now we can define many other functions easily! *) + let vars t = Iter.filter_map (function Var s -> Some s | _ -> None) (subterms t) ;; -val vars : term -> string sequence = +val vars : term -> string Iter.t = # let size t = Iter.length (subterms t) ;; -val size : term -> int = +val size : term -> int = # let vars_list l = Iter.(of_list l |> flat_map vars);; -val vars_list : term list -> string sequence = +val vars_list : term list -> string Iter.t = ``` ### Permutations @@ -161,16 +167,19 @@ enumerating the ways we can insert an element in a list. ```ocaml # open Iter.Infix;; # module S = Iter ;; +module S = Iter # let rec insert x l = match l with | [] -> S.return [x] | y :: tl -> S.append S.(insert x tl >|= fun tl' -> y :: tl') (S.return (x :: l)) ;; +val insert : 'a -> 'a list -> 'a list S.t = # let rec permute l = match l with | [] -> S.return [] | x :: tl -> permute tl >>= insert x ;; +val permute : 'a list -> 'a list S.t = # permute [1;2;3;4] |> S.take 2 |> S.to_list ;; - : int list list = [[4; 3; 2; 1]; [4; 3; 1; 2]] diff --git a/dune b/dune new file mode 100644 index 0000000..d5e5e21 --- /dev/null +++ b/dune @@ -0,0 +1,8 @@ + +(alias + (name runtest) + (deps README.md) + (action (progn + (run mdx test %{deps}) + (diff? %{deps} %{deps}.corrected)))) + diff --git a/iter.opam b/iter.opam index 2dd1912..a13886e 100644 --- a/iter.opam +++ b/iter.opam @@ -16,6 +16,7 @@ depends: [ "dune" {build} "qcheck" {with-test} "qtest" {with-test} + "mdx" {with-test} "odoc" {with-doc} ] tags: [ "sequence" "iterator" "iter" "fold" ]