Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handy F# snippets [closed]

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.


Infix Operator

I got this from http://sandersn.com/blog//index.php/2009/10/22/infix-function-trick-for-f go to that page for more details.

If you know Haskell, you might find yourself missing infix sugar in F#:

// standard Haskell call has function first, then args just like F#. So obviously
// here there is a function that takes two strings: string -> string -> string 
startsWith "kevin" "k"

//Haskell infix operator via backQuotes. Sometimes makes a function read better.
"kevin" `startsWith` "K" 

While F# doesn't have a true 'infix' operator, the same thing can be accomplished almost as elegantly via a pipeline and a 'backpipeline' (who knew of such a thing??)

// F# 'infix' trick via pipelines
"kevin" |> startsWith <| "K"

Multi-Line Strings

This is pretty trivial, but it seems to be a feature of F# strings that is not widely known.

let sql = "select a,b,c \
           from table \
           where a = 1"

This produces:

val sql : string = "select a,b,c from table where a = 1"

When the F# compiler sees a back-slash followed by a carriage return inside a string literal, it will remove everything from the back-slash to the first non-space character on the next line. This allows you to have multi-line string literals that line up, without using a bunch of string concatenation.


Generic memoization, courtesy of the man himself

let memoize f = 
  let cache = System.Collections.Generic.Dictionary<_,_>(HashIdentity.Structural)
  fun x ->
    let ok, res = cache.TryGetValue(x)
    if ok then res
    else let res = f x
         cache.[x] <- res
         res

Using this, you could do a cached reader like so:

let cachedReader = memoize reader

Simple read-write to text files

These are trivial, but make file access pipeable:

open System.IO
let fileread f = File.ReadAllText(f)
let filewrite f s = File.WriteAllText(f, s)
let filereadlines f = File.ReadAllLines(f)
let filewritelines f ar = File.WriteAllLines(f, ar)

So

let replace f (r:string) (s:string) = s.Replace(f, r)

"C:\\Test.txt" |>
    fileread |>
    replace "teh" "the" |>
    filewrite "C:\\Test.txt"

And combining that with the visitor quoted in the question:

let filereplace find repl path = 
    path |> fileread |> replace find repl |> filewrite path

let recurseReplace root filter find repl = 
    visitor root filter |> Seq.iter (filereplace find repl)

Update Slight improvement if you want to be able to read 'locked' files (e.g. csv files which are already open in Excel...):

let safereadall f = 
   use fs = new FileStream(f, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
   use sr = new StreamReader(fs, System.Text.Encoding.Default)
   sr.ReadToEnd()

let split sep (s:string) = System.Text.RegularExpressions.Regex.Split(s, sep)

let fileread f = safereadall f
let filereadlines f = f |> safereadall |> split System.Environment.NewLine