Handy F# snippets

Benjol picture Benjol · May 7, 2009 · Viewed 12.1k times · Source

There are already two questions about F#/functional snippets.

However what I'm looking for here are useful snippets, little 'helper' functions that are reusable. Or obscure but nifty patterns that you can never quite remember.

Something like:

open System.IO

let rec visitor dir filter= 
    seq { yield! Directory.GetFiles(dir, filter)
          for subdir in Directory.GetDirectories(dir) do 
              yield! visitor subdir filter} 

I'd like to make this a kind of handy reference page. As such there will be no right answer, but hopefully lots of good ones.

EDIT Tomas Petricek has created a site specifically for F# snippets http://fssnip.net/.

Answer

Juliet picture Juliet · Sep 25, 2009

Perl style regex matching

let (=~) input pattern =
    System.Text.RegularExpressions.Regex.IsMatch(input, pattern)

It lets you match text using let test = "monkey" =~ "monk.+" notation.