As an extension on the desco answer, you can use the function hidden in Test with pattern matching:
type Test<'a> = Test of ('a -> bool)
let applyTest (Test f) x = f x
Example:
let upperCaseTest = Test (fun (s:string) -> s.ToUpper() = s)
let primeTest =
Test (fun n ->
let upper = int (sqrt (float n))
n > 1 && (n = 2 || [2..upper] |> List.forall (fun d -> n%d <> 0))
)
In FSI:
> applyTest upperCaseTest "PIGSMIGHTFLY";;
val it : bool = true
> applyTest upperCaseTest "PIGSMIgHTFLY";;
val it : bool = false
> [1..30] |> List.filter (applyTest primeTest);;
val it : int list = [2; 3; 5; 7; 11; 13; 17; 19; 23; 29]
cfern source
share