【发布时间】:2016-09-27 12:34:17
【问题描述】:
我可以按键过滤我的map:
module PairKeys =
struct
type t = string * string
let compare (x0,y0) (x1,y1) =
match String.compare x0 x1 with
| 0 -> String.compare y0 y1
| c -> c
end
module StringMap = Map.Make(PairKeys);;
....
let put_key_values_into_a_list (key_searched : string) =
StringMap.filter (fun key -> key = key_searched)
(* should return a list of the values in the filtered map *)
之后,我想将这些值放入一个列表中。 我怎样才能在 OCaml 中做到这一点?
Map.Make 文档: http://caml.inria.fr/pub/docs/manual-ocaml/libref/Map.Make.html
谢谢
【问题讨论】:
-
您使用的映射不能为同一个键保存多个值,即它不是 multimap。除非您更改过滤谓词,否则您想要的代码大致相当于
MyMap.find key_searched m或更准确地说是[MyMap.find key_searched m](并且列表将始终是单例) -
也许将这些信息包含在帖子中是个好主意,这样人们就不会尝试“优化”代码:)
-
@Anton Trunov 好的,谢谢!我改进了我的问题。
-
仍然,
key匹配整个对,而不是它的第一个或第二个组件。你想要的可能类似于StringMap.filter (fun (key, _) _ -> key = key_searched)
标签: ocaml