Pipe Right 直通运算符 PipeThrough

Pipe Right pass-through operator PipeThrough

我声明了以下运算符以帮助使我的柯里化代码更易读

Pipe-Through 允许通过一个方法传递值并从另一端继续传递。我认为这有助于使我的代码更简洁。

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

使用示例

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

现在变成

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

这个有用还是我重新发明了轮子

它很有用,就像所有优秀的发明一样,它也是由其他人独立完成的。

Scott Wlaschin 称它为 teehttps://fsharpforfunandprofit.com/rop/

建议的运算符是|>!:

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

此定义与您的略有不同,因为它不使用 ignore

感谢分享!