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/.
toggle code to sql
More trivial than most on this list, but handy nonetheless:
I'm always taking sql in and out of code to move it to a sql environment during development. Example:
needs to be 'stripped' to:
keeping the formatting. It's a pain to strip out the code symbols for the sql editor, then put them back again by hand when I've got the sql worked out. These two functions toggle the sql back and forth from code to stripped:
then when you are ready to put it back into your code source file:
I'd love to get rid of the input file but can't even begin to grok how to make that happen. anyone?
edit:
I figured out how to eliminate the requirement of a file for these functions by adding a windows forms dialog input/output. Too much code to show, but for those who would like to do such a thing, that's how I solved it.
List comprehensions for float
This
[23.0 .. 1.0 .. 40.0]
was marked as deprecated a few versions backed.But apparently, this works:
(BTW, there's a floating point gotcha in there. Discovered at fssnip - the other place for F# snippets)
Generic memoization, courtesy of the man himself
Using this, you could do a cached reader like so:
F# Map <-> C# Dictionary
(I know, I know, System.Collections.Generic.Dictionary isn't really a 'C#' dictionary)
C# to F#
(From Brian, here, with improvement proposed by Mauricio in comment below.
(|KeyValue|)
is an active pattern for matching KeyValuePair - from FSharp.Core - equivalent to(fun kvp -> kvp.Key, kvp.Value)
)Interesting alternative
To get all of the immutable goodness, but with the O(1) lookup speed of Dictionary, you can use the
dict
operator, which returns an immutable IDictionary (see this question).I currently can't see a way to directly convert a Dictionary using this method, other than
F# to C#
What is weird here is that FSI will report the type as (for example):
but if you feed
dict [("a",1);("b",2)]
back in, FSI reportsPairwise and pairs
I always expect Seq.pairwise to give me [(1,2);(3;4)] and not [(1,2);(2,3);(3,4)]. Given that neither exist in List, and that I needed both, here's the code for future reference. I think they're tail recursive.
Simple read-write to text files
These are trivial, but make file access pipeable:
So
And combining that with the visitor quoted in the question:
Update Slight improvement if you want to be able to read 'locked' files (e.g. csv files which are already open in Excel...):