Search code examples
f#computation-expressioncustom-operator

Cannot define a computation expression custom "condition" operator


I am having serious difficulty coming up with a definition for condition in the following code. Hoping for an example and insight:

// a computation expression builder class
type Builder() =
    .
    .
    .

    [<CustomOperation( "condition",
      MaintainsVariableSpaceUsingBind = true )>]
    member this.Condition(p, [<ProjectionParameter>] b) = 
        condition p b

let attemp = AttemptBuilder()

let test =
    attempt { let x, y = exp1, exp2
              condition booleanExpr(x, y)   
              return (x, y) }

I presume b is implicitly ( fun x, y -> booleanExpr(x, y) ). The term booleanExpr(x, y) is just some Boolean expression involving x and y.


Solution

  • Found It:

    let condition p guard = ( fun () ->
        match p() with
        | Some x when guard x -> Some x
        | _ -> None )