Useful F# Scripts

f#, scripting

Solution

I use F# in a similar way when I need to quickly pre-process some data or convert data between various formats. F# has a great advantage that you can create higher-order functions for doing all sorts of similar tasks.

For example, I needed to load some data from SQL database and generate Matlab script files that load the data. I needed to do this for a couple of different SQL queries, so I wrote these two functions:

// Runs the specified query 'str' and reads results using 'f'
let query str f = seq { 
  let conn = new SqlConnection("<conn.str>");
  let cmd = new SqlCommand(str, conn)
  conn.Open()
  use rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
  while rdr.Read() do yield f(rdr) } 

// Simple function to save all data to the specified file
let save file data = 
  File.WriteAllLines(@"C:\...\" + file, data |> Array.ofSeq)

Now I could easily write specific calls to read the data I need, convert them to F# data types, does some pre-processing (if needed) and print the outputs to a file. For example for processing companies I had something like:

let comps = 
  query "SELECT [ID], [Name] FROM [Companies] ORDER BY [ID]" 
        (fun rdr ->  rdr.GetString(1) )
let cdata = 
  seq { yield "function res = companies()"
        yield "  res = {"
        for name in comps do yield sprintf "   %s" name
        yield "  };"
        yield "end" }
save "companies.m" cdata

Generating output as a sequence of strings is also pretty neat, though you could probably write a more efficient computation builder using `StringBuilder`.

Another example of using F# in the interactive way is described in my functional programming book in Chapter 13 (you can get the source code here). It connects to the World Bank database (which contains a lots of information about various countries), extracts some data, explores the structure of the data, convert them to F# data types and calculates some results (and visualizes them). I think this is (one of many) kinds of tasks that can be very nicely done in F#.

Problem

I have been investigating the use of F# for development and have found (for my situations) building scripts to help me simplify some complex tasks is where I can get value from it (at the moment). My most common complex task is concatenating files for many tasks (mostly SQL related). I do this often and every time I try to improve on my F# script to do this. This is my best effort so far: ``` open System.IO let path = "C:\\FSharp\\" let pattern = "*.txt" let out_path = path + "concat.out" File.Delete(out_path) Directory.GetFiles(path, pattern) |> Array.collect (fun file -> File.ReadAllLines(file)) |> (fun content -> File.WriteAllLines(out_path, content) ) ``` I'm sure others have scripts which makes their sometimes complex/boring tasks easier. What F# scripts have you used to do this or what other purposes for F# scripts have you found useful? I found the best way for me to improve my F# was to browse other scripts to get ideas on how to tackle specific situations. Hopefully this question will help me and others in the future. :) I have found an article on generating F# scripts that may be of interest: http://blogs.msdn.com/chrsmith/archive/2008/09/12/scripting-in-f.aspx

Original source

Related problems