Last active
September 19, 2019 18:41
-
-
Save sjsyrek/0ef2c82cbda6981417d38bef7e412715 to your computer and use it in GitHub Desktop.
Revisions
-
sjsyrek revised this gist
Dec 6, 2017 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -46,9 +46,6 @@ data ValidatedForm = ValidatedForm Email Password type FormValidation = Validation [Error] 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 mkForm :: String -> String -> FormValidation ValidatedForm mkForm email password = validateForm $ Form email password -
sjsyrek revised this gist
Dec 5, 2017 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -41,11 +41,11 @@ newtype Email = Email String newtype Password = Password String deriving Show data ValidatedForm = ValidatedForm Email Password deriving Show type FormValidation = Validation [Error] mkForm :: String -> String -> Form mkForm email password = Form email password -
sjsyrek revised this gist
Dec 5, 2017 . 1 changed file with 8 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
sjsyrek revised this gist
Dec 5, 2017 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -
sjsyrek revised this gist
Dec 5, 2017 . 1 changed file with 9 additions and 9 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,4 @@ 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 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 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 -
sjsyrek revised this gist
Dec 5, 2017 . 1 changed file with 10 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 -> FormValidation String notEmpty "" = Failure [EmptyField] notEmpty str = Success str minLength :: String -> Int -> FormValidation String minLength str n | length str >= n = Success str | otherwise = Failure [NotMinLength] minPasswordLength :: Int minPasswordLength = 8 validateEmail :: String -> FormValidation Email validateEmail input = notEmpty input $> Email input validatePassword :: String -> FormValidation Password validatePassword input = notEmpty input *> minLength input minPasswordLength $> Password input validateForm :: Form -> FormValidation ValidatedForm validateForm (Form email password) = ValidatedForm <$> validateEmail email <*> -
sjsyrek revised this gist
Dec 5, 2017 . 1 changed file with 28 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,6 @@ 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 str = Success str minLength :: String -> Int -> Validation Error String minLength str n | length str >= n = Success str | otherwise = Failure NotMinLength minPasswordLength :: Int minPasswordLength = 8 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 -> ValidatedForm validateForm (Form email password) = ValidatedForm <$> validateEmail email <*> validatePassword password -
sjsyrek created this gist
Dec 5, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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)