test: use mdx to test the readme

This commit is contained in:
Simon Cruanes 2019-02-14 12:40:50 -06:00
parent b325eae2d8
commit f447b39e4e
4 changed files with 32 additions and 14 deletions

View file

@ -8,7 +8,7 @@ env:
- PINS="iter:." - PINS="iter:."
- DISTRO="ubuntu-16.04" - DISTRO="ubuntu-16.04"
matrix: 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.03" EXTRA_DEPS="base-bigarray"
- PACKAGE="iter" OCAML_VERSION="4.04" EXTRA_DEPS="base-bigarray" - PACKAGE="iter" OCAML_VERSION="4.04" EXTRA_DEPS="base-bigarray"
#- PACKAGE="iter" OCAML_VERSION="4.05" EXTRA_DEPS="base-bigarray" #- PACKAGE="iter" OCAML_VERSION="4.05" EXTRA_DEPS="base-bigarray"

View file

@ -57,13 +57,16 @@ With iter, if the source structure provides a
`iter` function (or a `to_iter` wrapper), it becomes: `iter` function (or a `to_iter` wrapper), it becomes:
```ocaml ```ocaml
# let q = Queue.create();; # #require "iter";;
# let q : int Queue.t = Queue.create();;
val q : int Queue.t = <abstr>
# Iter.( 1 -- 10 |> to_queue q);; # Iter.( 1 -- 10 |> to_queue q);;
- : unit = () - : unit = ()
# Iter.of_queue q |> Iter.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 : int Stack.t = Stack.create();;
val s : int Stack.t = <abstr>
# Iter.(of_queue q |> to_stack s);; # Iter.(of_queue q |> to_stack s);;
- : unit = () - : unit = ()
# Iter.of_stack s |> Iter.to_list ;; # Iter.of_stack s |> Iter.to_list ;;
@ -78,7 +81,8 @@ a hashtable (in an undefined order that depends on the
underlying hash function): underlying hash function):
```ocaml ```ocaml
# let h = Hashtbl.create 16;; # let h: (int, string) Hashtbl.t = Hashtbl.create 16;;
val h : (int, string) Hashtbl.t = <abstr>
# for i = 0 to 10 do # for i = 0 to 10 do
Hashtbl.add h i (string_of_int i) Hashtbl.add h i (string_of_int i)
done;; done;;
@ -87,8 +91,8 @@ underlying hash function):
# Hashtbl.length h;; # Hashtbl.length h;;
- : int = 11 - : int = 11
(* now to get the values *) # (* now to get the values *)
# Iter.of_hashtbl h |> Iter.map snd |> Iter.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"]
``` ```
@ -124,6 +128,7 @@ A small λ-calculus AST, and some operations on it.
| Var of string | Var of string
| App of term * term | App of term * term
| Lambda of term ;; | Lambda of term ;;
type term = Var of string | App of term * term | Lambda of term
# let rec subterms : term -> term Iter.t = # let rec subterms : term -> term Iter.t =
fun t -> fun t ->
@ -135,19 +140,20 @@ A small λ-calculus AST, and some operations on it.
| App (a,b) -> | App (a,b) ->
Iter.append (subterms a) (subterms b)) Iter.append (subterms a) (subterms b))
;; ;;
val subterms : term -> term Iter.t = <fun>
(* Now we can define many other functions easily! *) # (* Now we can define many other functions easily! *)
# let vars t = let vars t =
Iter.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 Iter.t = <fun>
# let size t = Iter.length (subterms t) ;; # let size t = Iter.length (subterms t) ;;
val size : term -> int = <fun > val size : term -> int = <fun>
# let vars_list l = Iter.(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 Iter.t = <fun>
``` ```
### Permutations ### Permutations
@ -161,16 +167,19 @@ enumerating the ways we can insert an element in a list.
```ocaml ```ocaml
# open Iter.Infix;; # open Iter.Infix;;
# module S = Iter ;; # module S = Iter ;;
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 ->
S.append S.append
S.(insert x tl >|= fun tl' -> y :: tl') S.(insert x tl >|= fun tl' -> y :: tl')
(S.return (x :: l)) ;; (S.return (x :: l)) ;;
val insert : 'a -> 'a list -> 'a list S.t = <fun>
# let rec permute l = match l with # let rec permute l = match l with
| [] -> S.return [] | [] -> S.return []
| x :: tl -> permute tl >>= insert x ;; | x :: tl -> permute tl >>= insert x ;;
val permute : 'a list -> 'a list S.t = <fun>
# permute [1;2;3;4] |> S.take 2 |> S.to_list ;; # permute [1;2;3;4] |> S.take 2 |> S.to_list ;;
- : int list list = [[4; 3; 2; 1]; [4; 3; 1; 2]] - : int list list = [[4; 3; 2; 1]; [4; 3; 1; 2]]

8
dune Normal file
View file

@ -0,0 +1,8 @@
(alias
(name runtest)
(deps README.md)
(action (progn
(run mdx test %{deps})
(diff? %{deps} %{deps}.corrected))))

View file

@ -16,6 +16,7 @@ depends: [
"dune" {build} "dune" {build}
"qcheck" {with-test} "qcheck" {with-test}
"qtest" {with-test} "qtest" {with-test}
"mdx" {with-test}
"odoc" {with-doc} "odoc" {with-doc}
] ]
tags: [ "sequence" "iterator" "iter" "fold" ] tags: [ "sequence" "iterator" "iter" "fold" ]