Question

So basically I am writing some OCaml code like this:

let p_op p =
Show.show<op> p
|> Str.split (Str.regexp " +")
|> List.nth items 1           (items is the result of Str.split)
|> String.lowercase

So the problem is that how can I implicitly pass the result of Str.split to List.nth ?

if I know the label of List.nth, then I guess I can do this,

List.nth ~num:1

But basically as I don't use Core, I just don't know how to get the label of List.nth could anyone give me some help?

Then probably I need to wrapper the List.nth with labels myself?

Was it helpful?

Solution

The obvious place to look for this is in ListLabels. But for whatever reason the parameters of ListLabels.nth aren't labelled!

You can use flip:

let flip f x y = f y x

... |> flip List.nth 1 |> ...

flip is defined in OCaml Batteries Included as BatPervasives.flip (and also in Core).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top