12 people like it.

Typed access to IEnumerable

This snippet provides a way to gain typed access to System.Collection.IEnumerable values (such as instances of System.Text.RegularExpression.MatchCollection), as long as they support a Count property and an Item accessor.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
module Seq =
    let inline toTyped (coll:#System.Collections.IEnumerable) =
        seq {
            for i in 0 .. (^t : (member Count : int) coll) - 1 ->
                (^t : (member get_Item : int -> _) (coll,i))
        }

// Example usage:
System.Text.RegularExpressions.Regex.Matches("abcde","a|c")
|> Seq.toTyped
|> Seq.map (fun m -> m.Value)
module Seq

from Microsoft.FSharp.Collections
val toTyped : coll:'a -> seq<'b> (requires 'a :> System.Collections.IEnumerable and member get_Count and member get_Item)

Full name: Script.Seq.toTyped
val coll : 'a (requires 'a :> System.Collections.IEnumerable and member get_Count and member get_Item)
namespace System
namespace System.Collections
type IEnumerable =
  member GetEnumerator : unit -> IEnumerator

Full name: System.Collections.IEnumerable
Multiple items
val seq : sequence:seq<'T> -> seq<'T>

Full name: Microsoft.FSharp.Core.Operators.seq

--------------------
type seq<'T> = System.Collections.Generic.IEnumerable<'T>

Full name: Microsoft.FSharp.Collections.seq<_>
val i : 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<_>
namespace System.Text
namespace System.Text.RegularExpressions
Multiple items
type Regex =
  new : pattern:string -> Regex + 1 overload
  member GetGroupNames : unit -> string[]
  member GetGroupNumbers : unit -> int[]
  member GroupNameFromNumber : i:int -> string
  member GroupNumberFromName : name:string -> int
  member IsMatch : input:string -> bool + 1 overload
  member Match : input:string -> Match + 2 overloads
  member Matches : input:string -> MatchCollection + 1 overload
  member Options : RegexOptions
  member Replace : input:string * replacement:string -> string + 5 overloads
  ...

Full name: System.Text.RegularExpressions.Regex

--------------------
System.Text.RegularExpressions.Regex(pattern: string) : unit
System.Text.RegularExpressions.Regex(pattern: string, options: System.Text.RegularExpressions.RegexOptions) : unit
System.Text.RegularExpressions.Regex.Matches(input: string, pattern: string) : System.Text.RegularExpressions.MatchCollection
System.Text.RegularExpressions.Regex.Matches(input: string, pattern: string, options: System.Text.RegularExpressions.RegexOptions) : System.Text.RegularExpressions.MatchCollection
Multiple items
module Seq

from Script

--------------------
module Seq

from Microsoft.FSharp.Collections
val map : mapping:('T -> 'U) -> source:seq<'T> -> seq<'U>

Full name: Microsoft.FSharp.Collections.Seq.map
val m : System.Text.RegularExpressions.Match
property System.Text.RegularExpressions.Capture.Value: string
Raw view New version

More information

Link:http://fssnip.net/4f
Posted:5 years ago
Author:kvb
Tags: Sequences