Search code examples
f#currying

Pipe Right pass-through operator PipeThrough


I have declared the following operator to help make my curried code a bit more legible

Pipe-Through allows a value to be passed through a method and continue out the other side. I think it helps make my code more succinct.

let (|>!) x f = x |> f |> ignore; x

Example of use

let y = x |> transform
y |> logger.LogInformation
y
|> process
|> return

Now becomes

x 
|> transform 
|>! logger.LogInformation 
|> process 
|> return

Is this useful or have I reinvented the wheel


Solution

  • It is useful and like all good inventions it has been independently made by others as well.

    Scott Wlaschin called it tee: https://fsharpforfunandprofit.com/rop/

    The proposed operator is |>!:

    let inline    tee f v   = f v ; v
    let inline  (|>!) v f   = f v ; v
    let inline  (>>!) g f   = g >> fun v -> f v ; v    /// composition
    
    (5 * 8) |> tee (printfn "value = %d") |> doSomethingElse
    (5 * 8) |>!     printfn "value = %d"  |> doSomethingElse
    

    This definition is slightly different than yours as it does not use ignore.

    Thanks for sharing!