Skip to content

Commit

Permalink
Renamed Validation Right|Left to Success|Failure.
Browse files Browse the repository at this point in the history
Active pattern is no longer necessary.
  • Loading branch information
mausch committed Dec 23, 2011
1 parent 00ef72d commit b579a09
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 19 deletions.
13 changes: 4 additions & 9 deletions src/FSharpx.Core/Monad.fs
Expand Up @@ -461,19 +461,14 @@ module Choice =
| None -> Choice2Of2 o

let toValidation = function
| Choice1Of2 x -> Right x
| Choice2Of2 x -> Left x
| Choice1Of2 x -> Success x
| Choice2Of2 x -> Failure x

module Validation =

let toChoice = function
| Right x -> Choice1Of2 x
| Left x -> Choice2Of2 x

let (|Success|Failure|) =
function
| Right a -> Success a
| Left e -> Failure e
| Success x -> Choice1Of2 x
| Failure x -> Choice2Of2 x

let inline seqValidator f =
let zero = pure' []
Expand Down
12 changes: 6 additions & 6 deletions src/FSharpx.Core/Prelude.fs
Expand Up @@ -250,7 +250,7 @@ module Prelude =


// Applicative
type Validation< 'err,'v> = Left of 'err | Right of 'v
type Validation< 'err,'v> = Failure of 'err | Success of 'v

type Pure = Pure with
member inline this.Base x = return' x
Expand All @@ -260,7 +260,7 @@ module Prelude =
static member (?<-) (_, _Applicative:Pure, _:'a Async ) = fun (x:'a) -> Pure.Pure.Base x :'a Async
static member (?<-) (_, _Applicative:Pure, _:'a Nullable ) = fun (x:'a) -> Pure.Pure.Base x :'a Nullable
static member (?<-) (_, _Applicative:Pure, _:Choice<'a,'b> ) = fun (x:'a) -> Pure.Pure.Base x :Choice<'a,'b>
static member (?<-) (_, _Applicative:Pure, _:Validation<'b,'a>) = fun (x:'a) -> Right x :Validation<'b,'a>
static member (?<-) (_, _Applicative:Pure, _:Validation<'b,'a>) = fun (x:'a) -> Success x :Validation<'b,'a>
let inline pure' x : ^R = (() ? (Pure) <- Unchecked.defaultof< ^R>) x


Expand All @@ -273,10 +273,10 @@ module Prelude =
static member (?<-) (f:Choice<_,'e> , _Applicative:Ap, x:Choice<_,'e> ) = Ap.Ap.Base f x
static member inline (?<-) (f:Validation< ^e,_>, _Applicative:Ap, x:Validation< ^e,_>) =
match f,x with
| Right f, Right x -> Right (f x)
| Left e, Right x -> Left e
| Right f, Left e -> Left e
| Left e1, Left e2 -> Left (mappend e1 e2)
| Success f, Success x -> Success (f x)
| Failure e, Success x -> Failure e
| Success f, Failure e -> Failure e
| Failure e1, Failure e2 -> Failure (mappend e1 e2)
let inline (<*>) x y : ^R = (x ? (Ap) <- y)

let inline lift2 f a b = pure' f <*> a <*> b // Same as liftM2 but requires just Applicative Functor
Expand Down
8 changes: 4 additions & 4 deletions tests/FSharpx.Tests/ValidationTests.fs
Expand Up @@ -11,8 +11,8 @@ open NUnit.Framework

let validator pred error value =
if pred value
then Right value
else Left [error]
then Success value
else Failure [error]

let (==) = LanguagePrimitives.PhysicalEquality
let inline (!=) a b = not (a == b)
Expand Down Expand Up @@ -97,8 +97,8 @@ let ``validation with monoid``() =
// count the number of broken rules
let validator pred value =
if pred value
then Right value
else Left (Sum 1)
then Success value
else Failure (Sum 1)
let notEqual a = validator ((<>) a)
let lengthNotEquals l = validator (fun (x: string) -> x.Length <> l)
let validateString x =
Expand Down

0 comments on commit b579a09

Please sign in to comment.