I’m writing a CLI with Core.command and to write the command I need to apply a function f : 'a1 -> 'a2 -> ... -> 'an -> unit -> unit to arguments 'a1 t, 'a2 t, ... 'an t after trying some stuff, I ended up finding monadic binary operators :
(*>) : unit t -> 'a t -> 'a t
(<*) : 'a t -> unit t -> 'a t
(<*>) : ('a -> 'b) t -> 'a t -> 'b t
(>>|) : 'a t -> ('a -> 'b) -> 'b t
So I end up writing f +> a1 <*> a2 <*> ... <*> an with +> being >>| but flipped, which works fine but left me with some question.I don’t understand the point of *> and <* . For me they don’t make any sense.
I don’t understand how to use >>| correctly with many argument functions. (In my case that would be a1 >>| f <*> a2 ... <*> an which is just weird.
Or is there a way to use >>| better ? I thought that a1 <*> .... <*> an >>| f could be nice but that doesn’t really work either, you would need <*> to be flipped and change the priority of operation.
Do you guys have some ideas?
And if not, it looks pretty easy to use let-ops which can be defined easily in the language itself:
let ( let+ ) = Command.Let_syntax.( >>| )
let ( and+ ) = Command.Let_syntax.Let_syntax.both
let param =
let+ a1 = a1 (* This will pretty soon be just 'let+ a1' *)
and+ a2 = a2
and+ a3 = a3 in
fun () -> f a1 a2 a3
Command is definitely designed to be used with let-syntax (specifically with the %map_open ppx but using (let+) should also work), see the relevant chapter of Real World OCaml for a useful guide.
Let me guess, Batteries? It has a weird restriction and needs to be manually updated for each OCaml version. I think because of the way it uses Stdlib modules.