Skip to content

Instantly share code, notes, and snippets.

@timvw
Last active August 29, 2015 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timvw/8717796 to your computer and use it in GitHub Desktop.
Save timvw/8717796 to your computer and use it in GitHub Desktop.
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