Skip to content

How to use Validation applicative from F# #1293

Answered by louthy
glen-nicol asked this question in Q&A
Discussion options

You must be logged in to vote

Ok, so the short answer to your question is that you need to treat the Apply function as an extension method and not a static method:

let apply<'F, 'A, 'B, 'R>
    (f: System.Func<'A, 'B, 'R>)
    struct (a: Validation<'F, 'A>, b: Validation<'F, 'B>)
    : Validation<'F, 'R> =
    let t = System.ValueTuple.Create(a, b)
    t.Apply(f)

The longer answer is to define an operator that does this in a much better way.

First, I'll define the one and only wrapper needed for apply (and map):

let apply (mf : Validation<'f, 'a -> 'b>) (ma : Validation<'f, 'a>) : Validation<'f, 'b> =
    mf.Disjunction(ma)
      .Map(fun _ -> (successValue mf) (successValue ma))

let map (f : 'a -> 'b) (ma : Validation<

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@louthy
Comment options

@glen-nicol
Comment options

@louthy
Comment options

Answer selected by glen-nicol
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants