Last active
May 28, 2018 19:24
-
-
Save jkachmar/7903e54527e855376a987fa0590985b7 to your computer and use it in GitHub Desktop.
LambdaConf 2018 - Exercise 1.1
This file contains 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 characters
module Main where | |
import Prelude | |
import Control.Monad.Eff.Console (log, logShow) | |
import Data.Array (fromFoldable) | |
import Data.List.NonEmpty (NonEmptyList, singleton) | |
import Data.String (length, null, toLower, toUpper) | |
import Data.Generic.Rep (class Generic) | |
import Data.Generic.Rep.Show (genericShow) | |
import Data.Validation.Semigroup (V, invalid, unV) | |
import TryPureScript (render, withConsole) | |
-------------------------------------------------------------------------------- | |
-- EXERCISE 1.1 - REPLACE `?solution` WITH YOUR SOLUTION! | |
-- | Validate that an input string is not empty | |
validateNonEmpty :: String -> V (NonEmptyList InvalidPrimitive) String | |
validateNonEmpty input = ?solution | |
-------------------------------------------------------------------------------- | |
main = render =<< withConsole do | |
let validTest = validateNonEmpty "Hello!" | |
logShow (printValidation validTest) | |
log "\n" | |
let invalidTest = validateNonEmpty "" | |
logShow (printValidation invalidTest) | |
-------------------------------------------------------------------------------- | |
-- TYPES AND FUNCTIONS DEFINED EARLIER IN THE PRESENTATION | |
-- | Type of validation errors encountered when validating primitive input | |
data InvalidPrimitive | |
= EmptyField | |
| InvalidEmail String | |
| TooShort Int Int | |
| TooLong Int Int | |
| NoLowercase String | |
| NoUppercase String | |
-- | Validate that an input string is at least as long as some given `Int` | |
validateMinimumLength | |
:: String | |
-> Int | |
-> V (NonEmptyList InvalidPrimitive) String | |
validateMinimumLength input minLength | |
| (length input) < minLength = invalid (singleton (TooShort (length input) minLength)) | |
| otherwise = pure input | |
-- | Validate that an input string contains at least one lowercase element | |
validateContainsLowercase | |
:: String | |
-> V (NonEmptyList InvalidPrimitive) String | |
validateContainsLowercase input | |
| (toUpper input) == input = invalid (singleton (NoLowercase input)) | |
| otherwise = pure input | |
--------------------------------------------------------------------------------- | |
-- !!! BOILERPLATE TO MAKE EVERYTHING A LITTLE EASIER TO WORK WITH !!! | |
--------------------------------------------------------------------------------- | |
-- | Helper function to print validations | |
printValidation | |
:: forall err result | |
. Show err | |
=> V (NonEmptyList err) String | |
-> String | |
printValidation = | |
unV (show <<< fromFoldable) (\result -> "Valid: " <> result) | |
-- | Derive a `Generic` instance for `InvalidPrimitive` so we can get a `Show` | |
-- | instance to print to the console. | |
derive instance genericInvalidPrimitive :: Generic InvalidPrimitive _ | |
-- | Derive `show` for `InvalidPrimitive` using the `Generic` instance. | |
instance showInvalidPrimitive :: Show InvalidPrimitive where | |
show = genericShow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment