Skip to content

Instantly share code, notes, and snippets.

@serjKim
Created June 11, 2019 14:32
Show Gist options
  • Save serjKim/dc374256c465f599ec0dbf8279661b96 to your computer and use it in GitHub Desktop.
Save serjKim/dc374256c465f599ec0dbf8279661b96 to your computer and use it in GitHub Desktop.
apply vs bind vs keilsli
open System
type EmployeeEmail =
| Email of string
| Invalid of string list
let bind f x =
match x with
| Email x -> f x
| Invalid m -> Invalid m
let ( >=> ) f g x =
match f x with
| Email x -> g x
| Invalid m -> Invalid m
let switch f g x =
match f x, g x with
| Email a, Email b -> Email a
| Invalid m, Email _ -> Invalid m
| Email _, Invalid m -> Invalid m
| Invalid a, Invalid b -> Invalid (a @ b)
let ( &&& ) = switch
let checkForEmpty x =
if String.IsNullOrEmpty x then
Invalid ["Empty or null"]
else
Email x
let checkForWhiteSpace x =
if String.IsNullOrWhiteSpace x then
Invalid ["Whitespace"]
else
Email x
let hasAtSymbol x =
if not <| String.exists (fun input -> input = '@') x then
Invalid ["Lack of @"]
else
Email x
let hasDotSymbol x =
if not <| String.exists (fun input -> input = '.') x then
Invalid ["Lack of ."]
else
Email x
let checkForLength x =
if String.length x < 10 then
Invalid ["Length < 10"]
else
Email x
let createEmployeeEmail =
checkForEmpty
>=> checkForWhiteSpace
>=> (hasAtSymbol &&& checkForLength &&& hasDotSymbol)
createEmployeeEmail "asd." |> printfn "%A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment