2 people like it.
Like the snippet!
Pluck and TryPluck
Functions to select the first element in an array that passes some predicate, and separately all the other array elements. (I'm not sure if this type of operation has a standard name. Please tweet me if you know!)
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:

module Array =
/// Returns a tuple containing the first element in
/// the input array for which f returns true, and
/// a new array containing all input elements apart
/// from the selected one. It is an error for
/// no element to be found.
let pluck f a =
let hit = Array.find f a
let remainder =
a > Array.filter (fun x > x <> hit)
hit, remainder
/// Returns Some tuple containing the first element in
/// the input array for which f returns true, and
/// a new array containing all input elements apart
/// from the selected one. If no element is found
/// returns None.
let tryPluck f a =
let hit = Array.tryFind f a
match hit with
 Some h >
let remainder =
a > Array.filter (fun x > x <> h)
Some (h, remainder)
 None > None

module Array
from Microsoft.FSharp.Collections
val pluck : f:('a > bool) > a:'a [] > 'a * 'a [] (requires equality)
Full name: Script.Array.pluck
Returns a tuple containing the first element in
the input array for which f returns true, and
a new array containing all input elements apart
from the selected one. It is an error for
no element to be found.
val f : ('a > bool) (requires equality)
val a : 'a [] (requires equality)
val hit : 'a (requires equality)
val find : predicate:('T > bool) > array:'T [] > 'T
Full name: Microsoft.FSharp.Collections.Array.find
val remainder : 'a [] (requires equality)
val filter : predicate:('T > bool) > array:'T [] > 'T []
Full name: Microsoft.FSharp.Collections.Array.filter
val x : 'a (requires equality)
val tryPluck : f:('a > bool) > a:'a [] > ('a * 'a []) option (requires equality)
Full name: Script.Array.tryPluck
Returns Some tuple containing the first element in
the input array for which f returns true, and
a new array containing all input elements apart
from the selected one. If no element is found
returns None.
val hit : 'a option (requires equality)
val tryFind : predicate:('T > bool) > array:'T [] > 'T option
Full name: Microsoft.FSharp.Collections.Array.tryFind
union case Option.Some: Value: 'T > Option<'T>
val h : 'a (requires equality)
union case Option.None: Option<'T>
More information