I’m looking for a way to reduce this list to a boolean. Here is the original:
let ones = [1;1;1;1]
let twos = [2;2;2;2]
let bad = [1;2;3]
let isAllOnes = List.forall (fun op -> op = 1)
let isAllTwos = List.forall (fun op -> op = 2)
let isOneOrTwo ops = isAllOnes ops || isAllTwos ops
isOneOrTwo ones |> should be True
isOneOrTwo twos |> should be True
isOneOrTwo bad |> should be False
I’m trying to refactor this using a kind of reduce. Something like this:
let isOneOrTwo ops = [isAllOnes; isAllTwos] |> List.tryFind (fun acc -> acc ops)
(isOneOrTwo ones).IsSome |> should be True
(isOneOrTwo twos).IsSome |> should be True
(isOneOrTwo bad).IsSome |> should be False
I don’t like how isOneOrTwo reduces to an Option. I really would like to reduce the list to a boolean so that my assertions can look like this:
isOneOrTwo ones |> should be True
isOneOrTwo twos |> should be True
isOneOrTwo bad |> should be False
Anyone know how to make this happen? List.reduce didn’t work because the types were different.
Replace List.tryFind with List.exists