Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does a tee function exist somewhere in F# library?

Tags:

f#

...or in FSharpx?

let tee sideEffect =
    fun x ->
        do sideEffect x
        x

The usage could be something like

f >> tee (printfn "F returned: %A") >> g >> h

Or is there another simple way to do this?

thanks!

like image 949
vidi Avatar asked Oct 31 '14 10:10

vidi


2 Answers

ExtCore includes a function called tap which does exactly what you want. I use it for primarily for inspecting intermediate values within an F# "pipeline" (hence the name).

For example:

[| 1;2;3 |]
|> Array.map (fun x -> x * 2)
|> tap (fun arr ->
    printfn "The mapped array values are: %A" arr)
|> doOtherStuffWithArray
like image 92
Jack P. Avatar answered Oct 22 '22 15:10

Jack P.


The closest I've seen is actually in WebSharper. The definition is:

let inline ( |>! ) x sideEffect =
    do sideEffect x
    x

Usage:

(x |>! printf "%A") |> nextFunc
like image 28
Christopher Stevenson Avatar answered Oct 22 '22 15:10

Christopher Stevenson