Dictionary Ocaml映射到_seq用法

Dictionary Ocaml映射到_seq用法,dictionary,iterator,ocaml,Dictionary,Iterator,Ocaml,假设我有以下几点 module IntPairs = struct type t = int * int let compare (x0,y0) (x1,y1) = match Stdlib.compare x0 x1 with 0 -> Stdlib.compare y0 y1 | c -> c end module PairsMap = Map.Make(IntPairs) 我添加了一些元素

假设我有以下几点

module IntPairs =
   struct
     type t = int * int
     let compare (x0,y0) (x1,y1) =
       match Stdlib.compare x0 x1 with
           0 -> Stdlib.compare y0 y1
         | c -> c
   end

module PairsMap = Map.Make(IntPairs)
我添加了一些元素:

let m = PairsMap.(empty |> add (1,1) 1 |> add (2,1) 1 |> add (1,2) |> add (2,2))
我如何按升序打印密钥?
我对ocaml中的迭代器不太熟悉

这更多的是对ocaml教程的要求,而不是关于代码中特定问题的问题。阅读文档通常比在StackOverflow上单独提问更快

话虽如此,
Seq
界面如下所示:

type 'a t = unit -> 'a node
and 'a node = Nil | Cons of 'a * 'a t
val empty : 'a t
val return : 'a -> 'a t
val map : ('a -> 'b) -> 'a t -> 'b t
val filter : ('a -> bool) -> 'a t -> 'a t
val filter_map : ('a -> 'b option) -> 'a t -> 'b t
val flat_map : ('a -> 'b t) -> 'a t -> 'b t
val fold_left : ('a -> 'b -> 'a) -> 'a -> 'b t -> 'a
val iter : ('a -> unit) -> 'a t -> unit
如您所见,它提供了许多高阶遍历函数。您可能希望使用的是
iter
,因为您希望打印值,而不是使用它们进行计算。也就是说,您所需的使用没有返回值

但是,您应该注意,
Map
接口已经有了一个
iter
功能。据我所知,在进行迭代之前,没有理由转换为序列。
Map.iter
文件说明:

绑定按键类型的顺序递增地传递给f

这就是你想要的,所以
Map.iter
似乎能奏效

PairsMap.iter my_print_function m