Given some CreditScoreInput:
type CreditScoreInput = { id: string; score: string; years: int }
let input = [
{ id = "CUSTOMER001"; score = "Medium"; years = 1 }
{ id = "CUSTOMER001"; score = "Medium"; years = 1 }
{ id = "CUSTOMER002"; score = "Medium"; years = 10 }
{ id = "CUSTOMER003"; score = "Bad"; years = 0 }
{ id = "CUSTOMER003"; score = "Bad"; years = 0 }
{ id = "CUSTOMER003"; score = "Bad"; years = 0 }
{ id = "CUSTOMER004"; score = "Good"; years = 0 }
{ id = "CUSTOMER005"; score = "Good"; years = 10 }
]
My function validateDuplicates is out looking for duplicates:
let validate list =
match list with
| [] -> failwith "No customers supplied!"
| _ -> list
let validateDuplicates (group:string * list<CreditScoreInput>) =
match group with
| (id, g) when g.Length = 1 -> printf $"No duplicates for {id} is OK.\n"
| (id, [input1; input2]) -> printf $"Two duplicates for {id} is OK.\n"
| (id, g) when g.Length > 2 -> printf $"More than two duplicates for {id} is NOT OK.\n"
true
input
|> validate
|> List.groupBy (fun i -> i.id)
|> List.forall (fun i -> i |> validateDuplicates)
|> ignore
Inside of validateDuplicates I notice a little squiggly under group, leading to the warning:
Incomplete pattern matches on this expression. For example, the value
(_,[_;_;_])may indicate a case not covered by the pattern(s). However, a pattern rule with awhenclause might successfully match this value.
Is there a way I can play nice with the compiler to avoid this warning?
Update
I am not sure whether I should do this here but here are my changes based on the excellent guidance:
let validateDuplicates (group:string * list<CreditScoreInput>) =
match group with
| (id, [_]) -> printf $"No duplicates for {id} is OK.\n"
| (id, [_; _]) -> printf $"Two duplicates for {id} is OK.\n"
| (id, g) -> printf $"More than two duplicates for {id} is NOT OK.\n"
input
|> validate
|> List.groupBy (fun i -> i.id)
|> List.iter (fun i -> i |> validateDuplicates)