Phrase of the week

July 29, 2013 at 3:32 AMMichele Mottini

In order that we should understand things fully, the winter of ninety forty-one was given to us as a measure.

Konstantin Simonov, as quoted in 'Leningrad' by Anna Reid

Posted in: Opinion

Tags:

Phrase of the week

July 21, 2013 at 7:05 AMMichele Mottini

[It is like] shearing a piglet…a lot of squealing and very little wool

Vladimir Putin, quoted by The Economist in 'Travels and travails'

Posted in: Opinion

Tags:

Phrase of the week

July 14, 2013 at 8:13 AMMichele Mottini

Infinite monkeys impractical - seeking talented developers instead

Austin Digital (GE Aviation) job ad title

Posted in: Opinion

Tags:

State machine using recursive functions

July 8, 2013 at 7:58 AMMichele Mottini

I had a log file containing sequences of operations descriptions like this:

/*
  DBLoadSchema
  2013-06-21T15:01:46.222-04:00
  . . .

and I needed to extract just the name (‘DBLoadSchema’) and date-time of each operation, plus their line number within the log file.

The ‘standard’ way would be to use a loop processing all the lines one by one, using some variables to keep track of the current state: ‘found /*’, ‘found operation name XXX’ etc. – a state machine.

The scripting language I use now is mostly F# – but using mutable variables to keep track of the state like that feels ‘wrong’ in a functional language like F# – a solution without mutable variables would be preferable.

Half-remembering something I read in a blog (or maybe was a book?) I came up with using separate functions – each representing a state of the state machine – that simply call each other to transition between states:

let rec processStart (log: StreamReader) lineNumber =
  if log.EndOfStream then
    Seq.empty
  else
    let line = log.ReadLine()
    let lineNumber = lineNumber + 1
    if line="/*" then 
      processStartComment log lineNumber
    else 
      processStart log lineNumber
and processStartComment (log: StreamReader) lineNumber =
  if log.EndOfStream then
    Seq.empty
  else
    let line = log.ReadLine()
    let lineNumber = lineNumber + 1
    processProc (line.Trim()) log lineNumber
and processProc procName (log: StreamReader) lineNumber =
  if log.EndOfStream then
    Seq.empty
  else
    let line = log.ReadLine()
    let lineNumber = lineNumber + 1
    let dateTimeString = line.Trim()
    let (ok, dateTime) = DateTime.TryParse(dateTimeString)
    seq {
      if ok then
        yield (lineNumber, procName, dateTime)
      yield! processStart log lineNumber
    }

The source stream and the current line number are passed as a parameter to each function, and the state variables are parameters as well – in this case just the name of the operation, that is passed from ‘processStartComment’ to ‘processProc’. The functions return the result as a sequence of triplets line number, operation name, operation date-time. To process a log file simply open it and call ‘processStart’:

let filterLog (path: string) =
  use log = new StreamReader(path)
  processStart log 1

All in all this ‘functional’ way requires a little bit to get used to – coming from a procedural background – but looks much simpler to use, especially if the state machine gets more complex. The use of sequences is nice as well: once you have a sequence of the parsed values it is easy to create pipelines to do further processing.

Posted in: Programming

Tags: ,

Phrase of the week

July 7, 2013 at 10:30 AMMichele Mottini

I think he was taken aback. (He might not have been. It can be hard to tell with engineers.)

Frances Woolley in 'The lunch problem'

Posted in: Opinion

Tags: , ,