What is the difference between Trial.lift and Trial.bind in chess error handling?

I look through a chessie . I often see how the Trial.lift and Trial.bind functions are used. If I understand correctly, Trial.lift takes a function parameter and executes and returns this function if the result in the channel is successful. If true, does Trial.bind not do the same?

+4
source share
1 answer

These functions are subtly different: they must return a to the bindfunction , but it accepts any ordinary function.fResult<_>lift

Think of it this way: bind“attaches” another, possibly failed calculation to the previous calculation chain:

let isOdd x = if x % 2 = 0 then ok x else fail "Even!"
let x = ok 5
let oddX = x |> bind isOdd

while lift "" "" Result<_>:

let plus5 x = x + 5  // plus5 : int -> int
let liftedPlus5 = lift plus5  // lisftedPlus5 : Result<int,_> -> Result<int,_>
let seven = liftedPlus5 (ok 2)

, : . . ( bind)

P.S. , - F # .

+7

Source: https://habr.com/ru/post/1681311/


All Articles