0
votes

How do you apply a List filter before a List map in OCaml? I'm trying the pipe operator but with no success:

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)

utop is giving me:

"This expression has type string list but an expression was expected of type string list -> 'a"

I'm aware the List.filter does nothing at present. I'm simply trying to use it before the List.map

1

1 Answers

2
votes

The standard OCaml List.filter doesn't have the ~f: parameter. Most likely you're using Core.

I don't have Core set up right now, so I can't test. But one possible problem is that you're using body in the List.map call. I think you should leave it out. You want to process the result of the List.filter expression. You don't want to process body, which is the original value from the match.

Here is a similar expression using the OCaml standard library versions of the functions:

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