F #: turn old IEnumerable.GetEnumerator () into a style iterator in seq

There is something else in the standard .NET libraries that only exposes the old-school iterator IEnumerable.GetEnumerator()to the outside world, which is not very convenient for the F # seq processing style. I quickly did google on how to get the result groups Regex.Match(...)into a list that I could process, and didn't find anything.

I had this:

open System.Text.RegularExpressions
let input = "args=(hello, world, foo, bar)"
let mtc = Regex.Match( input, "args=\(([\w\s,]+)\)" )

I would like to access mtc.Groupseither seq or list, but this does not allow this, because it is olde ICollection, which provides only the method GetEnumerator(). Therefore, while you can do

mtc.Groups.[1].Value

you cannot do

mtc.Groups |> Seq.skip 1 // <=== THIS QUESTION IS ABOUT HOW TO ACHIEVE THIS

as this leads to

error FS0001: The type 'Text.RegularExpressions.GroupCollection' is not compatible with the type 'seq<'a>

(For clarity, GroupCollectionimplements ICollection, which is a sub-interface IEnumerable.)

, : GetEnumerator() seq?

+4
1

, , . , seq {...} seq<obj> , .

seq { let i = mtc.Groups.GetEnumerator() in while i.MoveNext() do yield i.Current } 
|> Seq.cast<Text.RegularExpressions.Group> 
|> Seq.map (fun m -> m.Value)
|> List.ofSeq

, , :

val input : string = "args=(hello, world, foo, bar)"
val mtc : Match = args=(hello, world, foo, bar)
val it : string list = ["args=(hello, world, foo, bar)"; "hello, world, foo, bar"]

, googler , , , downvotes, dupe .

: Seq.cast , IEnumerable . , seq- , Seq.cast<Text.RegularExpressions.Group>! , .

+3

All Articles