Search code examples
f#functional-programmingpipelinepartial-applicationpipelining

Is "a -> b -> (a -> b -> c) -> c" to apply two parameters a standard functional concept?


I came across the need for a function with the signature 'a -> 'b -> ('a -> 'b -> 'c) -> 'c to use for applying two arguments when piping:

let apply2 x y f =
  f x y

I needed this because I am using a function

myFun : MyType -> TypeA -> TypeB -> ResultType

and I use it in another function like this:

let useCase someValue (valueA: TypeA) (valueB: TypeB) =
  someValue
  |> ...
  |> toMyType
  |> myFun
  |> apply2 valueA valueB

apply2 fits the bill, but I can't shake the feeling that I could use a built-in function or operator or that I am missing some more fundamental way of doing this (barring lambdas, which IMHO reads worse in this case). Note that I can't easily switch the parameter order of myFun (it's a Giraffe HttpHandler, so the last two parameters have to be HttpFunc and HttpContext, designated by TypeA and TypeB above).

Is the apply2 function with the signature I've described a fair thing to use in functional programming, or am I missing something obvious? If this is a well-known concept, does it have a better name?


Solution

  • In my opinion the code is much clearer if you bind the intermediate value with let.

    let useCase someValue (valueA: TypeA) (valueB: TypeB) =
        let myValue =
            someValue
            |> ...
            |> toMyType
        myFun myValue valueA valueB
    

    You can also use backward pipes as follows

    let useCase someValue (valueA: TypeA) (valueB: TypeB) =
        someValue
        |> ...
        |> toMyType
        |> myFun <| valueA <| valueB