Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
let (|Array|_|) pattern toMatch =
let patternLength = Array.length pattern
let toMatchLength = Array.length toMatch
let tailLength = toMatchLength - patternLength
if patternLength > toMatchLength then
None
else
let firstElementsAreEqual = [ 0 .. (patternLength - 1) ] |> Seq.forall (fun i -> pattern.[i] = toMatch.[i])
if firstElementsAreEqual then
Some(Array.sub toMatch patternLength tailLength)
else
None
match [|1;2;3|] with
| Array [|1|] tail -> sprintf "bingo %i" (tail |> Array.sum) // the tail is [|2;3|]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.