Commit
Author: Kevin Schoon [kevinschoon@gmail.com]
Hash: 9171a68bbad5704ac50ccd0f23931b87823e42a4
Timestamp: Fri, 09 Jul 2021 20:02:06 +0000 (3 years ago)

+12 -9 +/-3 browse
simplify flatten
1diff --git a/lib/note.ml b/lib/note.ml
2index 6be04a2..fa5491a 100644
3--- a/lib/note.ml
4+++ b/lib/note.ml
5 @@ -111,11 +111,14 @@ let of_string ?(path = None) content =
6 module Tree = struct
7 type tree = Tree of (t * tree list)
8
9- let rec flatten ?(accm = []) tree =
10- let (Tree (note, others)) = tree in
11- List.fold ~init:(note :: accm)
12- ~f:(fun accm note -> flatten ~accm note)
13- others
14+ let flatten tree =
15+ let rec flatten ~accm tree =
16+ let (Tree (note, others)) = tree in
17+ List.fold ~init:(note :: accm)
18+ ~f:(fun accm note -> flatten ~accm note)
19+ others
20+ in
21+ tree |> flatten ~accm:[]
22
23 let fst tree =
24 let (Tree (note, _)) = tree in
25 diff --git a/lib/note.mli b/lib/note.mli
26index d811946..33b631d 100644
27--- a/lib/note.mli
28+++ b/lib/note.mli
29 @@ -28,7 +28,7 @@ module Tree : sig
30 val fst : tree -> t
31 (* return the top level note of a given tree *)
32
33- val flatten : ?accm:t list -> tree -> t list
34+ val flatten : tree -> t list
35 (* flatten a tree into a list of notes *)
36 end
37
38 diff --git a/test/note_test.ml b/test/note_test.ml
39index db2edb4..6d25e88 100644
40--- a/test/note_test.ml
41+++ b/test/note_test.ml
42 @@ -31,17 +31,17 @@ let adapter () =
43 let tree = options |> Note.load ~path:"/" in
44 Alcotest.(check int)
45 "initialized" 1
46- (tree |> Note.Tree.flatten ~accm:[] |> List.length);
47+ (tree |> Note.Tree.flatten |> List.length);
48 options |> Note.create ~content:(Some "bar") ~path:"/fuu";
49 let tree = options |> Note.load ~path:"/" in
50 Alcotest.(check int)
51 "note added" 2
52- (tree |> Note.Tree.flatten ~accm:[] |> List.length);
53+ (tree |> Note.Tree.flatten |> List.length);
54 options |> Note.remove ~path:"/fuu";
55 let tree = options |> Note.load ~path:"/" in
56 Alcotest.(check int)
57 "note removed" 1
58- (tree |> Note.Tree.flatten ~accm:[] |> List.length)
59+ (tree |> Note.Tree.flatten |> List.length)
60
61 let suggest_path () =
62 let options : Note.options =