Practice question from Railway-Oriented Programming

Fix this code to properly chain the operations:

let parseNumber s =
    match System.Int32.TryParse(s) with
    | true, n -> Ok n
    | false, _ -> Error "Not a number"

let isPositive n =
    if n > 0 then Ok n
    else Error "Not positive"

let result = parseNumber "42" |> Result.____ isPositive

Answer

bind

Explanation

Since isPositive returns a Result type, we must use Result.bind (not map). Using map would produce Result<Result<int, string>, string> (nested Results). Bind unwraps the inner Result properly.

More questions from Functional Programming with F#