-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Summary: `Fmt` provides two very useful `iter` and `iter_bindings` functions that can be used to pretty-print data-structures using their iterator functions. Those iterator are expected to follow the "stdlib" style, taking their arguments (in particular, the `'a -> unit` function being called) without labels. However, the Core library uses a different convention, with some labels added here and there. This commit provides adapters for `iter` and `iter_bindings` that expect there arguments to be Core-style iterators. This makes it easier to define pretty-printers for Core-based data structures. Reviewed By: jvillard Differential Revision: D45357585 fbshipit-source-id: 15c51ca3a16ddbab3fcfcf369c78acd975747f7d
- Loading branch information
1 parent
b5b2133
commit 04e4c32
Showing
6 changed files
with
53 additions
and
17 deletions.
There are no files selected for viewing
This file contains 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 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 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 @@ | ||
(* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*) | ||
|
||
open! IStd | ||
|
||
let colon_sp ppf _ = | ||
Format.pp_print_string ppf ":" ; | ||
Fmt.sp ppf () | ||
|
||
|
||
module Labelled = struct | ||
let iter ?sep iter pp_elt = Fmt.iter ?sep (fun f x -> iter x ~f) pp_elt | ||
|
||
let iter_bindings ?sep iteri pp_binding = | ||
Fmt.iter_bindings ?sep (fun f x -> iteri x ~f:(fun ~key ~data -> f key data)) pp_binding | ||
end |
This file contains 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,27 @@ | ||
(* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
* | ||
* This source code is licensed under the MIT license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
*) | ||
|
||
(** Low-level combinators built on {!Fmt}. Sophisticated pretty-printing functions should rather go | ||
in {!Pp}. *) | ||
|
||
val colon_sp : 'a Fmt.t | ||
(** [colon_sp] is [Fmt.any ":@ "]. It prints a colon and a space break hint. *) | ||
|
||
module Labelled : sig | ||
(** Adapters of {!Fmt} functions using labelled interfaces *) | ||
|
||
val iter : ?sep:unit Fmt.t -> ('a -> f:('elt -> unit) -> unit) -> 'elt Fmt.t -> 'a Fmt.t | ||
[@@warning "-unused-value-declaration"] | ||
(** {!Fmt.iter} using a Core-style labelled iterator function *) | ||
|
||
val iter_bindings : | ||
?sep:unit Fmt.t | ||
-> ('a -> f:(key:'key -> data:'data -> unit) -> unit) | ||
-> ('key * 'data) Fmt.t | ||
-> 'a Fmt.t | ||
(** {!Fmt.iter_bindings} using a Core-style (key, data) labelled iterator function *) | ||
end |
This file contains 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 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