簡體   English   中英

列出過濾器,然后在OCaml中列出地圖

[英]List filter then List map in OCaml

如何在OCaml中的列表映射之前應用列表過濾器? 我正在嘗試管道操作器,但沒有成功:

let r infile =
match List.tl (In_channel.read_lines infile ) with
| None -> []
| Some body ->
  List.filter body ~f:(fun line -> true)
  |> List.map body ~f:(fun line ->
    match split_on_comma line with
    | _ :: _ :: num :: street :: unit :: city :: _ :: region :: _ ->
      String.strip (num ^ " " ^ addr_case street ^ ", " ^ addr_case city ^ " " ^ region)
    | _ -> assert false)

優拓給我:

“此表達式具有類型字符串列表,但是期望為類型字符串列表->'a”的表達式

我知道List.filter目前不執行任何操作。 我只是想在List.map之前使用它

標准的OCaml List.filter沒有〜f ~f:參數。 您很有可能正在使用Core。

我目前尚未設置Core,因此無法測試。 但是一個可能的問題是您在List.map調用中使用body 我認為您應該忽略它。 您要處理List.filter表達式的結果。 您不想處理正文,它是匹配中的原始值。

這是使用OCaml標准庫版本的函數的類似表達式:

# ListLabels.filter [1; 2; 3; 4]
      ~f: (fun x -> x mod 2 = 0) |>
  ListLabels.map ~f: (fun x -> x + 10) ;;
- : int list = [12; 14]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM