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/.
Naive CSV reader (i.e., won't handle anything nasty)
(Using filereadlines and List.transpose from other answers here)
Example
Date Range
simple but useful list of dates between
fromDate
andtoDate
A handy cache function that keeps up to
max
(key,reader(key))
in a dictionary and use aSortedList
to track the MRU keysFlatten a List
if you have something like this:
and want to 'flatten' it down to a singe list so the result is like this:
it can be done thusly:
Active Patterns, aka "Banana Splits", are a very handy construct that let one match against multiple regular expression patterns. This is much like AWK, but without the high performance of DFA's because the patterns are matched in sequence until one succeeds.
Some examples of use:
Perl style regex matching
It lets you match text using
let test = "monkey" =~ "monk.+"
notation.