I'm still new when it comes to many areas of F #. I ask this question more out of curiosity than out of real business need. Is there a way to match the first n items in a list, no matter what order they appear? To clarify, consider the following example:
type MyEnum =
| Foo of int
| Bar of float
| Baz of string
let list = [ Foo 1 ; Bar 1.0 ; Baz "1" ]
Now suppose I want to name some_funcif the first two items in the list are Fooand a Barin any order. It is easy enough to combine two possible permutations:
let result =
match list with
| Foo n :: Bar x :: _
| Bar x :: Foo n :: _ -> some_func n x
| _ -> failwith "First 2 items must be Foo and Bar"
However, what if I need to call a function if the first 3 elements are Foo, Barand Bazin any order? Using the same method above, I need to write all 6 different permutations (or n! For n elements). Ideally, I would like to do something in accordance with this:
let result =
match list with
| (AnyOrder [ Foo n ; Bar x ; Baz s ]) :: _ -> some_func n x s
| _ -> failwith "First 3 items must be Foo, Bar, and Baz"
Is there a way to do this with the active template without requiring hard code of different permutations?
source
share