Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

F#: How do I use Map with a Collection (like Regex Matches)?

Tags:

map

f#

Soo... F# no longer has IEnumerable.map_with_type... which is the way people were mapping over collections. How do I do that now?

let urlPat = "href\\s*=\\s*(?:(?:\\\"(?<url>[^\\\"]*)\\\")|(?<url>[^\\s]* ))";;
let urlRegex = new Regex(urlPat)
let matches = 
    urlRegex.Matches(http("http://www.google.com"))

let matchToUrl (urlMatch : Match) = urlMatch.Value
let urls = List.map matchToUrl matches

Thanks!

like image 449
Justin Bozonier Avatar asked Nov 19 '08 20:11

Justin Bozonier


2 Answers

you would write the last line like this:

let urls = Seq.map matchToUrl (Seq.cast matches);;

And this can be written in a nicer way using pipelining operator:

let urls = matches|> Seq.cast |> Seq.map matchToUrl;;

F# automatically figures out what is the right target type (because it knows what matchToUrl looks like). This is available only for Seq, so you can use List.of_seq to get the data into a list again.

like image 85
Tomas Petricek Avatar answered Oct 15 '22 14:10

Tomas Petricek


Is Seq.cast what you are looking for?

like image 41
Chris Smith Avatar answered Oct 15 '22 12:10

Chris Smith