■
ExtList.List.filter_mapが死ぬほど便利.
type t = A of int | B of string let filter_a xs = List.filter (fun x -> match x with A i -> true | _ -> false) xs let get_a_integers xs = List.map (fun x -> match x with A i -> i | _ -> assert false) (filter_a xs)
みたいな,あほですか?といいたくなるようなプログラム(assert falseあたり)が,
type t = A of int | B of string let get_a_integers xs = ExtList.List.filter_map (fun x -> match x with A i -> Some i | _ -> None) xs
とちょうすっきり書ける.あ,fold使えばいいじゃん,というのは無しで.
RubyのArrayにもこういうメソッドが欲しい.Array#select_collect?Scheme風*1にArray#secollectとか?やっぱりinject使えばいいじゃん,というのは無しで.
*1:cadrとかをイメージ