Skip to content

Instantly share code, notes, and snippets.

@sjsyrek
Last active September 19, 2019 18:41
Show Gist options
  • Select an option

  • Save sjsyrek/0ef2c82cbda6981417d38bef7e412715 to your computer and use it in GitHub Desktop.

Select an option

Save sjsyrek/0ef2c82cbda6981417d38bef7e412715 to your computer and use it in GitHub Desktop.

Revisions

  1. sjsyrek revised this gist Dec 6, 2017. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions validation-example.hs
    Original file line number Diff line number Diff line change
    @@ -46,9 +46,6 @@ data ValidatedForm = ValidatedForm Email Password

    type FormValidation = Validation [Error]

    mkForm :: String -> String -> Form
    mkForm email password = Form email password

    notEmpty :: String -> FormValidation String
    notEmpty "" = Failure [EmptyField]
    notEmpty str = Success str
    @@ -76,4 +73,7 @@ validateForm :: Form -> FormValidation ValidatedForm
    validateForm (Form email password) =
    ValidatedForm <$>
    validateEmail email <*>
    validatePassword password
    validatePassword password

    mkForm :: String -> String -> FormValidation ValidatedForm
    mkForm email password = validateForm $ Form email password
  2. sjsyrek revised this gist Dec 5, 2017. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions validation-example.hs
    Original file line number Diff line number Diff line change
    @@ -41,11 +41,11 @@ newtype Email = Email String
    newtype Password = Password String
    deriving Show

    type FormValidation = Validation [Error]

    data ValidatedForm = ValidatedForm Email Password
    deriving Show

    type FormValidation = Validation [Error]

    mkForm :: String -> String -> Form
    mkForm email password = Form email password

  3. sjsyrek revised this gist Dec 5, 2017. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions validation-example.hs
    Original file line number Diff line number Diff line change
    @@ -17,6 +17,14 @@ instance Semigroup err => Applicative (Validation err) where
    Failure e <*> Success _ = Failure e
    Failure e <*> Failure e' = Failure (e <> e')

    instance Foldable (Validation err) where
    foldr f x (Success a) = f a x
    foldr _ x (Failure _) = x

    instance Traversable (Validation err) where
    traverse f (Success a) = Success <$> f a
    traverse _ (Failure e) = pure (Failure e)

    data Form = Form {
    email :: String
    , password :: String
  4. sjsyrek revised this gist Dec 5, 2017. 1 changed file with 3 additions and 0 deletions.
    3 changes: 3 additions & 0 deletions validation-example.hs
    Original file line number Diff line number Diff line change
    @@ -28,12 +28,15 @@ data Error =
    deriving Show

    newtype Email = Email String
    deriving Show

    newtype Password = Password String
    deriving Show

    type FormValidation = Validation [Error]

    data ValidatedForm = ValidatedForm Email Password
    deriving Show

    mkForm :: String -> String -> Form
    mkForm email password = Form email password
  5. sjsyrek revised this gist Dec 5, 2017. 1 changed file with 9 additions and 9 deletions.
    18 changes: 9 additions & 9 deletions validation-example.hs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    import Data.Monoid
    import Data.Semigroup
    import Data.Functor
    import Control.Applicative

    @@ -9,30 +9,30 @@ instance Functor (Validation err) where
    fmap f (Success a) = Success (f a)
    fmap _ (Failure e) = Failure e

    instance Monoid err => Applicative (Validation err) where
    instance Semigroup err => Applicative (Validation err) where
    pure = Success

    Success f <*> Success a = Success (f a)
    Success _ <*> Failure e = Failure e
    Failure e <*> Success _ = Failure e
    Failure e <*> Failure e' = Failure (e <> e')

    data Error =
    EmptyField
    | NotMinLength
    deriving Show

    type FormValidation = Validation [Error]

    data Form = Form {
    email :: String
    , password :: String
    } deriving Show

    data Error =
    EmptyField
    | NotMinLength
    deriving Show

    newtype Email = Email String

    newtype Password = Password String

    type FormValidation = Validation [Error]

    data ValidatedForm = ValidatedForm Email Password

    mkForm :: String -> String -> Form
  6. sjsyrek revised this gist Dec 5, 2017. 1 changed file with 10 additions and 8 deletions.
    18 changes: 10 additions & 8 deletions validation-example.hs
    Original file line number Diff line number Diff line change
    @@ -22,6 +22,8 @@ data Error =
    | NotMinLength
    deriving Show

    type FormValidation = Validation [Error]

    data Form = Form {
    email :: String
    , password :: String
    @@ -36,30 +38,30 @@ data ValidatedForm = ValidatedForm Email Password
    mkForm :: String -> String -> Form
    mkForm email password = Form email password

    notEmpty :: String -> Validation Error String
    notEmpty "" = Failure EmptyField
    notEmpty :: String -> FormValidation String
    notEmpty "" = Failure [EmptyField]
    notEmpty str = Success str

    minLength :: String -> Int -> Validation Error String
    minLength :: String -> Int -> FormValidation String
    minLength str n
    | length str >= n = Success str
    | otherwise = Failure NotMinLength
    | otherwise = Failure [NotMinLength]

    minPasswordLength :: Int
    minPasswordLength = 8

    validateEmail :: String -> Validation Error Email
    validateEmail :: String -> FormValidation Email
    validateEmail input =
    notEmpty input $>
    Email input

    validatePassword :: String -> Validation Error Password
    validatePassword :: String -> FormValidation Password
    validatePassword input =
    notEmpty input *>
    minLength input minPasswordLength *>
    minLength input minPasswordLength $>
    Password input

    validateForm :: Form -> ValidatedForm
    validateForm :: Form -> FormValidation ValidatedForm
    validateForm (Form email password) =
    ValidatedForm <$>
    validateEmail email <*>
  7. sjsyrek revised this gist Dec 5, 2017. 1 changed file with 28 additions and 14 deletions.
    42 changes: 28 additions & 14 deletions validation-example.hs
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,6 @@
    import Data.Monoid ((<>))
    import Data.Monoid
    import Data.Functor
    import Control.Applicative

    data Validation err a = Failure err | Success a
    deriving Show
    @@ -25,28 +27,40 @@ data Form = Form {
    , password :: String
    } deriving Show

    newtype Email = Email String

    newtype Password = Password String

    data ValidatedForm = ValidatedForm Email Password

    mkForm :: String -> String -> Form
    mkForm email password = Form email password

    notEmpty :: String -> Validation [Error] String
    notEmpty "" = Failure [EmptyField]
    notEmpty :: String -> Validation Error String
    notEmpty "" = Failure EmptyField
    notEmpty str = Success str

    minLength :: String -> Int -> Validation [Error] String
    minLength :: String -> Int -> Validation Error String
    minLength str n
    | length str >= n = Success str
    | otherwise = Failure [NotMinLength]
    | otherwise = Failure NotMinLength

    minPasswordLength :: Int
    minPasswordLength = 8

    validateForm :: Form -> Validation [Error] Form
    validateForm form = Form
    <$> notEmpty (email form)
    <*> minLength (password form) minPasswordLength
    validateEmail :: String -> Validation Error Email
    validateEmail input =
    notEmpty input $>
    Email input

    validatePassword :: String -> Validation Error Password
    validatePassword input =
    notEmpty input *>
    minLength input minPasswordLength *>
    Password input

    validateForm' :: Form -> Validation [Error] Form
    validateForm' form = form
    <$ (notEmpty (email form)
    *> notEmpty (password form)
    *> minLength (password form) minPasswordLength)
    validateForm :: Form -> ValidatedForm
    validateForm (Form email password) =
    ValidatedForm <$>
    validateEmail email <*>
    validatePassword password
  8. sjsyrek created this gist Dec 5, 2017.
    52 changes: 52 additions & 0 deletions validation-example.hs
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,52 @@
    import Data.Monoid ((<>))

    data Validation err a = Failure err | Success a
    deriving Show

    instance Functor (Validation err) where
    fmap f (Success a) = Success (f a)
    fmap _ (Failure e) = Failure e

    instance Monoid err => Applicative (Validation err) where
    pure = Success

    Success f <*> Success a = Success (f a)
    Success _ <*> Failure e = Failure e
    Failure e <*> Success _ = Failure e
    Failure e <*> Failure e' = Failure (e <> e')

    data Error =
    EmptyField
    | NotMinLength
    deriving Show

    data Form = Form {
    email :: String
    , password :: String
    } deriving Show

    mkForm :: String -> String -> Form
    mkForm email password = Form email password

    notEmpty :: String -> Validation [Error] String
    notEmpty "" = Failure [EmptyField]
    notEmpty str = Success str

    minLength :: String -> Int -> Validation [Error] String
    minLength str n
    | length str >= n = Success str
    | otherwise = Failure [NotMinLength]

    minPasswordLength :: Int
    minPasswordLength = 8

    validateForm :: Form -> Validation [Error] Form
    validateForm form = Form
    <$> notEmpty (email form)
    <*> minLength (password form) minPasswordLength

    validateForm' :: Form -> Validation [Error] Form
    validateForm' form = form
    <$ (notEmpty (email form)
    *> notEmpty (password form)
    *> minLength (password form) minPasswordLength)