2 people like it.
Like the snippet!
An active pattern combinator for list inputs
A few active pattern combinators useful when pattern matching on lists is necessary.
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:
27:
28:
29:
30:
31:
|
module List =
// single case ap combinator
let (|Map|) (f : 'T -> 'S) ts = List.map f ts
// partial case ap combinators
let (|Choose|) (f : 'T -> 'S option) ts = List.choose f ts
let (|TryMap|_|) (f : 'T -> 'S option) (ts : 'T list) =
let rec aux acc ts =
match ts with
| [] -> List.rev acc |> Some
| t :: rest ->
match f t with
| Some s -> aux (s :: acc) rest
| None -> None
aux [] ts
// a trivial example
let (|NonNegative|_|) (x : int) = if x >= 0 then Some x else None
let test =
function
| Some (List.TryMap (|NonNegative|_|) values) -> values
| Some (List.Choose (|NonNegative|_|) values) ->
printfn "found some non-negative values in inputs"
values
| _ -> []
test <| Some [-10..10]
test <| Some [1..10]
|
Multiple items
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
Multiple items
module Map
from Microsoft.FSharp.Collections
--------------------
type Map<'Key,'Value (requires comparison)> =
interface IEnumerable
interface IComparable
interface IEnumerable<KeyValuePair<'Key,'Value>>
interface ICollection<KeyValuePair<'Key,'Value>>
interface IDictionary<'Key,'Value>
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
member Add : key:'Key * value:'Value -> Map<'Key,'Value>
member ContainsKey : key:'Key -> bool
override Equals : obj -> bool
member Remove : key:'Key -> Map<'Key,'Value>
...
Full name: Microsoft.FSharp.Collections.Map<_,_>
--------------------
new : elements:seq<'Key * 'Value> -> Map<'Key,'Value>
val f : ('T -> 'S)
val ts : 'T list
val map : mapping:('T -> 'U) -> list:'T list -> 'U list
Full name: Microsoft.FSharp.Collections.List.map
val f : ('T -> 'S option)
type 'T option = Option<'T>
Full name: Microsoft.FSharp.Core.option<_>
val choose : chooser:('T -> 'U option) -> list:'T list -> 'U list
Full name: Microsoft.FSharp.Collections.List.choose
type 'T list = List<'T>
Full name: Microsoft.FSharp.Collections.list<_>
val aux : ('S list -> 'T list -> 'S list option)
val acc : 'S list
val rev : list:'T list -> 'T list
Full name: Microsoft.FSharp.Collections.List.rev
union case Option.Some: Value: 'T -> Option<'T>
val t : 'T
val rest : 'T list
val s : 'S
union case Option.None: Option<'T>
val x : int
Multiple items
val int : value:'T -> int (requires member op_Explicit)
Full name: Microsoft.FSharp.Core.Operators.int
--------------------
type int = int32
Full name: Microsoft.FSharp.Core.int
--------------------
type int<'Measure> = int
Full name: Microsoft.FSharp.Core.int<_>
val test : _arg1:int list option -> int list
Full name: Script.test
Multiple items
module List
from Script
--------------------
module List
from Microsoft.FSharp.Collections
--------------------
type List<'T> =
| ( [] )
| ( :: ) of Head: 'T * Tail: 'T list
interface IEnumerable
interface IEnumerable<'T>
member Head : 'T
member IsEmpty : bool
member Item : index:int -> 'T with get
member Length : int
member Tail : 'T list
static member Cons : head:'T * tail:'T list -> 'T list
static member Empty : 'T list
Full name: Microsoft.FSharp.Collections.List<_>
active recognizer TryMap: ('T -> 'S option) -> 'T list -> 'S list option
Full name: Script.List.( |TryMap|_| )
active recognizer NonNegative: int -> int option
Full name: Script.( |NonNegative|_| )
val values : int list
active recognizer Choose: ('T -> 'S option) -> 'T list -> 'S list
Full name: Script.List.( |Choose| )
val printfn : format:Printf.TextWriterFormat<'T> -> 'T
Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
More information