I’m writing a web API wrapper where I have a bunch of functions that return cohttp responses. I’d like to be able to expose both these “lowlevel” functions directly and versions that parse the responses into more user-friendly values. I’d like to do this without having to rewrite the function arguments for each pair of functions - I could write
let f x y z = [stuff returning cohttp response] and then
let f' x y z = f x y z |> [parser], but that gets tedious with lots of functions to cover.
Conceptually, I’d like something with a type of
('b -> 'a) -> ('c_1 -> ... -> 'c_n -> 'b) -> 'c_1 -> ... -> 'c_n -> 'a, but I don’t believe I can write that function in OCaml. Is there some other technique that would work well here?
I have something working right now where I pass a continuation into each of the “lowlevel” functions and pass the cohttp response into the continuation, but I’ve had to do some extra wrangling with optional arguments and the value restriction after making this change. I’m wondering if there are better alternatives.