-
Notifications
You must be signed in to change notification settings - Fork 101
Define Tree and Forest modules for sidebars and index #1228
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| include List | ||
|
|
||
| let rec concat_map ?sep ~f = function | ||
| | [] -> [] | ||
| | [ x ] -> f x | ||
| | x :: xs -> ( | ||
| let hd = f x in | ||
| let tl = concat_map ?sep ~f xs in | ||
| match sep with None -> hd @ tl | Some sep -> hd @ (sep :: tl)) | ||
|
|
||
| let rec filter_map acc f = function | ||
| | hd :: tl -> | ||
| let acc = match f hd with Some x -> x :: acc | None -> acc in | ||
| filter_map acc f tl | ||
| | [] -> List.rev acc | ||
|
|
||
| let filter_map f x = filter_map [] f x | ||
|
|
||
| (** @raise [Failure] if the list is empty. *) | ||
| let rec last = function | ||
| | [] -> failwith "Odoc_utils.List.last" | ||
| | [ x ] -> x | ||
| | _ :: tl -> last tl | ||
|
|
||
| (* From ocaml/ocaml *) | ||
| let rec find_map f = function | ||
| | [] -> None | ||
| | x :: l -> ( | ||
| match f x with Some _ as result -> result | None -> find_map f l) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| module List = Odoc_list | ||
|
|
||
| type 'a tree = { node : 'a; children : 'a forest } | ||
| and 'a forest = 'a tree list | ||
|
|
||
| module type S = sig | ||
| type 'a t | ||
|
|
||
| val fold_left : f:('acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'acc | ||
| val iter : f:('a -> unit) -> 'a t -> unit | ||
| val map : f:('a -> 'b) -> 'a t -> 'b t | ||
| end | ||
|
|
||
| type 'a t = 'a tree | ||
|
|
||
| let leaf node = { node; children = [] } | ||
|
|
||
| let rec fold_left ~f acc { node; children } = | ||
| let acc = f acc node in | ||
| fold_left_forest ~f acc children | ||
|
|
||
| and fold_left_forest ~f acc forest = List.fold_left (fold_left ~f) acc forest | ||
|
|
||
| let rec iter ~f { node; children } = | ||
| let () = f node in | ||
| iter_forest ~f children | ||
|
|
||
| and iter_forest ~f forest = List.iter (iter ~f) forest | ||
|
|
||
| let rec map ~f { node; children } = | ||
| let node = f node in | ||
| let children = map_forest ~f children in | ||
| { node; children } | ||
|
|
||
| and map_forest ~f forest = List.map (map ~f) forest | ||
|
|
||
| let rec filter_map ~f { node; children } = | ||
| match f node with | ||
| | None -> None | ||
| | Some node -> | ||
| let children = filter_map_forest ~f children in | ||
| Some { node; children } | ||
|
|
||
| and filter_map_forest ~f forest = List.filter_map (filter_map ~f) forest | ||
gpetiot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| module Forest = struct | ||
| type 'a t = 'a forest | ||
|
|
||
| let fold_left = fold_left_forest | ||
| let iter = iter_forest | ||
| let map = map_forest | ||
| let filter_map = filter_map_forest | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| type 'a tree = { node : 'a; children : 'a forest } | ||
| and 'a forest = 'a tree list | ||
|
|
||
| val leaf : 'a -> 'a tree | ||
|
|
||
| module type S = sig | ||
| type 'a t | ||
|
|
||
| val fold_left : f:('acc -> 'a -> 'acc) -> 'acc -> 'a t -> 'acc | ||
| val iter : f:('a -> unit) -> 'a t -> unit | ||
| val map : f:('a -> 'b) -> 'a t -> 'b t | ||
| end | ||
gpetiot marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| include S with type 'a t = 'a tree | ||
|
|
||
| module Forest : sig | ||
| include S with type 'a t = 'a forest | ||
|
|
||
| val filter_map : f:('a -> 'b option) -> 'a t -> 'b t | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.