3 people like it.

Array tail match

Match the first elements of an array an returns tail.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
let (|Array|_|) l a =
    let rec loop i =
        function
        | h :: tail when h = Array.get a i -> loop (i+1) tail
        | [] -> a |> Seq.skip i |> Seq.toArray |> Some
        | _ -> None
    loop 0 l

match [| 1;2;3;4;5;6 |] with
| Array [ 1;2] tail -> tail
| _ -> [||]
module Array

from Microsoft.FSharp.Collections
val l : 'a list (requires equality)
val a : 'a [] (requires equality)
val loop : (int -> 'a list -> 'a [] option) (requires equality)
val i : int
val h : 'a (requires equality)
val tail : 'a list (requires equality)
val get : array:'T [] -> index:int -> 'T

Full name: Microsoft.FSharp.Collections.Array.get
module Seq

from Microsoft.FSharp.Collections
val skip : count:int -> source:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Collections.Seq.skip
val toArray : source:seq<'T> -> 'T []

Full name: Microsoft.FSharp.Collections.Seq.toArray
union case Option.Some: Value: 'T -> Option<'T>
union case Option.None: Option<'T>
Multiple items
active recognizer Array: 'a list -> 'a [] -> 'a [] option

Full name: Script.( |Array|_| )

--------------------
module Array

from Microsoft.FSharp.Collections
val tail : int []
Raw view Test code New version

More information

Link:http://fssnip.net/lu
Posted:10 years ago
Author:thinkbeforecoding
Tags: f# , active patterns , array