Map (写像)モジュール

Map は「マッピング(写像)」を作成する。 例えば、 ユーザーとユーザーに関連づけられたパスワードからなるデータがあるとしよう。 Map モジュールでユーザー名からそのパスワードへの写像を作成できる。 Map モジュールは単にこれをするのではなく、とても効率的に行う。 また関数的方法で行う。 以下の例では文字列から文字列への写像を行うが、 そのような異なったデータ型でも写像を行える。

Map を作るにはこうする:

# module MyUsers = Map.Make(String);;
module MyUsers : sig type key = String.t type 'a t = 'a Map.Make(String).t val empty : 'a t val is_empty : 'a t -> bool val mem : key -> 'a t -> bool val add : key -> 'a -> 'a t -> 'a t val update : key -> ('a option -> 'a option) -> 'a t -> 'a t val singleton : key -> 'a -> 'a t val remove : key -> 'a t -> 'a t val merge : (key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t val union : (key -> 'a -> 'a -> 'a option) -> 'a t -> 'a t -> 'a t val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool val iter : (key -> 'a -> unit) -> 'a t -> unit val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b val for_all : (key -> 'a -> bool) -> 'a t -> bool val exists : (key -> 'a -> bool) -> 'a t -> bool val filter : (key -> 'a -> bool) -> 'a t -> 'a t val filter_map : (key -> 'a -> 'b option) -> 'a t -> 'b t val partition : (key -> 'a -> bool) -> 'a t -> 'a t * 'a t val cardinal : 'a t -> int val bindings : 'a t -> (key * 'a) list val min_binding : 'a t -> key * 'a val min_binding_opt : 'a t -> (key * 'a) option val max_binding : 'a t -> key * 'a val max_binding_opt : 'a t -> (key * 'a) option val choose : 'a t -> key * 'a val choose_opt : 'a t -> (key * 'a) option val split : key -> 'a t -> 'a t * 'a option * 'a t val find : key -> 'a t -> 'a val find_opt : key -> 'a t -> 'a option val find_first : (key -> bool) -> 'a t -> key * 'a val find_first_opt : (key -> bool) -> 'a t -> (key * 'a) option val find_last : (key -> bool) -> 'a t -> key * 'a val find_last_opt : (key -> bool) -> 'a t -> (key * 'a) option val map : ('a -> 'b) -> 'a t -> 'b t val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t val to_seq : 'a t -> (key * 'a) Seq.t val to_rev_seq : 'a t -> (key * 'a) Seq.t val to_seq_from : key -> 'a t -> (key * 'a) Seq.t val add_seq : (key * 'a) Seq.t -> 'a t -> 'a t val of_seq : (key * 'a) Seq.t -> 'a t end

よし、これでMyUsersモジュールが作られたのでこのなかに何か入れはじめよう。 どこから始めようか? うーん、空の写像からはじめてみよう。

# let m = MyUsers.empty;;
val m : 'a MyUsers.t = <abstr>

ふぅむ。空の写像はちょっとつまらないので、何かデータを加えよう。

# let m = MyUsers.add "fred" "sugarplums" m;;
val m : string MyUsers.t = <abstr>

ここで "fred" と、 彼のパスワードである "sugarplums" をモジュールに加えた。 とても重要な点を指摘する。 一旦、文字列 "sugarplums" を加えると写像の型が固定される。 つまりこれは MyUsers モジュールが文字列から文字列への写像であるということだ。 もし文字列から整数への写像や整数から何かへの写像が欲しければ 別の写像を作らないといけない。

ついでにいくつか追加データを加えよう。

# let m = MyUsers.add "tom" "ilovelucy" m;;
val m : string MyUsers.t = <abstr> # let m = MyUsers.add "mark" "ocamlrules" m;;
val m : string MyUsers.t = <abstr> # let m = MyUsers.add "pete" "linux" m;;
val m : string MyUsers.t = <abstr>

写像のなかにいくつかデータがあるので、 どこかでデータが見られると親切じゃないかな? 簡単な表示関数を作ってみよう。

# let print_user key password =
    print_string(key ^ " " ^ password ^ "\n");;
val print_user : string -> string -> unit = <fun>

ふたつの文字列、キーとパスワードをとって それらを最後の改行も込みにして表示してくれる関数である。 これで必要なのは写像に適用する関数だ。 それはこんな感じになるだろう:

# MyUsers.iter print_user m;;
fred sugarplums mark ocamlrules pete linux tom ilovelucy - : unit = ()

だが写像にデータを置く理由は、 データを素早く見つけ出したいからかもしれない。 見つけ出しかたを実際に示す:

# MyUsers.find "fred" m;;
- : string = "sugarplums"

これは素早く効率的にFredのパスワードである "sugarplums" を返す。