Skip to content

Instantly share code, notes, and snippets.

View CoditCompany's full-sized avatar
👋
You can find us on @CoditEU

Codit CoditCompany

👋
You can find us on @CoditEU
View GitHub Profile
let pcustomer = pipe6 country fullname street city state postal optCustomer
let customerRE = "[A-Z]{2}\s+[A-Za-z ]+[0-9A-Za-z ]+[A-Za-z ]+[A-Z]{2}\s[0-9 ]{5}"
let genString xs l =
Gen.elements xs
|> Gen.map string
|> Gen.listOfLength l
|> Gen.map (List.reduce (+))
|> Gen.filter (String.length >> (>=) l)
let pheader = PO >>. pipe3 year_dash month_dash day datetime
let exercise p x =
match run p x with
| Success _ -> true
| Failure _ -> false
let headerRE = "PO[0-9]{4}-[0-9]{2}-[0-9]{2}"
let matchRegex pattrn input = Regex.IsMatch (input, pattrn)
let item i d q u n =
{ Id = i
Description = d
Quantity = q
UnitPrice = u
Notes = n }
let optItem iOpt dOpt qOpt uOpt nOpt =
let (<*>) = optApply
let (<!>) = Option.map
let itemRE = "^ITEM[0-9]{3}-[A-Z]{2}$"
type ItemId = Id of string
let itemId = function
| NotMatch itemRE -> None
| s -> Some <| Id s
type PosInt = PosInt of int
let posInt i =
if i >= 0 then Some <| PosInt i
let customer country name street city state postal =
{ Country = country
FullName = name
Street = street
State = state
City = city
PostalCode = postal }
let optApply fOpt xOpt =
match fOpt, xOpt with
let country = countString 10 (caps <|> space) |>> cap2
let fullname = countString 20 (letter <|> space) |>> stringAZ
let street = countString 20 (letter <|> space <|> digit) |>> stringAZ123
let city = countString 15 (letter <|> space) |>> stringAZ
let state = countString 3 (caps <|> space) |>> cap2
let postal = countString 5 (caps <|> digit <|> space) |>> (int >> posint5)
let (|NotMatch|_|) pattern s =
if Regex.IsMatch (s, pattern) then None
else Some NotMatch
type Cap2 = private Cap2 of string
let cap2 = function
| NotMatch "^[A-Z]{2}$" -> None
| s -> Some <| Cap2 s
type StringAZ = private StringAZ of string
type Customer =
{ Country : string
FullName : string
Street : string
City : string
State : string
PostalCode : int }
let comma = pchar ','
let pitems = (skipString "ITEMS,") >>. (sepBy pitem comma)
type Item =
{ Id : string
Description : string
Quantity : int
UnitPrice : float
Notes : string }
let item i d q u n = { Id = i; Description = d; Quantity = q; UnitPrice = u; Notes = n }
let pipe = skipChar '|'