The example from Result.map3 can be solved using the result computation expression as below:
// Result<int, string>let addResult = result {let! x = tryParseInt "35"let! y = tryParseInt "5"let! z = tryParseInt "2"return add x y z}
Example 2
The example from Result.map3 can be solved using the result computation expression as below:
// Result<CreatePostRequest,string>let createPostRequestResult = result {let! lat = Latitude.TryCreate 13.067439let! lng = Longitude.TryCreate 80.237617let! tweet = Tweet.TryCreate "Hello, World!"return createPostRequest userId lat lng tweet}
Example 3
Given the following functions:
tryGetUser : string -> User optionisPwdValid : string -> User -> boolauthorize : User -> Result<unit, AuthError>createAuthToken : User -> AuthToken
Here's how a simple login use-case can be written (using some helpers from the Result module):
typeLoginError= InvalidUser | InvalidPwd | Unauthorized ofAuthErrorlet login (username :string)(password :string):Result<AuthToken,LoginError>= result {// requireSome unwraps a Some value or gives the specified error if Nonelet! user = username |> tryGetUser |> Result.requireSome InvalidUser// requireTrue gives the specified error if falsedo! user |> isPwdValid password |> Result.requireTrue InvalidPwd// Error value is wrapped/transformed (Unauthorized has signature AuthError -> LoginError)do! user |> authorize |> Result.mapError Unauthorizedreturn user |> createAuthToken}