Tuesday, 24 January 2017

Compiling regular expressions with Antimorov derivatives

The F# Journal just published an article:

"Regular expressions are a popular and powerful tool for string manipulation. In 1964, Brzozowski introduced the concept of derivatives to the study of regular expressions. Although academically interesting such derivatives were of little practice use until Antimorov introduced the concept of partial derivatives of regular expressions. This article demonstrates how partial derivatives can be used to compile regular expressions to deterministic finite automata (DFA)..."

If you subscribe to the F# Journal then can read this article here otherwise subscribe to the The F# Journal today to read this article and many more!

Sunday, 22 January 2017

Scraping websites part 1: simple examples

The F# Journal just published an article:

"Gathering information from the internet by hand can be tedious. Many modern websites are database driven and that underlying database is often the thing of interest. This article is the first in a series looking at simple ways to extract data from websites in a more automated way. In this case we scrape two different websites, the first from a single page and the second from a sequence of pages..."

If you subscribe to the F# Journal then can read this article here otherwise subscribe to the The F# Journal today to read this article and many more!

Scraping websites to make a torrent search tool

The F# Journal just published an article:

"Torrent search websites can be notoriously difficult to navigate and accidents can lead to malware. This article takes a look at the challenge of building a WPF application that scrapes a popular torrent site in order to download torrents more quickly and safely..."

If you subscribe to the F# Journal then can read this article here otherwise subscribe to the The F# Journal today to read this article and many more!

Tuesday, 20 September 2016

State machines

Someone on the internet recently asserted that F# is “atrocious” for implementing state machines compared to C#. I just Googled C# state machine and found this. I translated the example into the following F# code:

type State = Inactive | Active | Paused | Exited
type Transition = Begin | End | Pause | Resume | Exit
 
let move = function
  | Inactive, Begin -> Active
  | (Active | Paused), End -> Inactive
  | Active, Pause -> Paused
  | Paused, Resume -> Active
  | Inactive, Exit -> Exited
  | state, transition -> failwithf "Invalid transition: %A -> %A" state transition

Here is their original C# for comparison:

using System;
using System.Collections.Generic;
 
namespace Juliet
{
    public enum ProcessState
    {
        Inactive,
        Active,
        Paused,
        Terminated
    }
 
    public enum Command
    {
        Begin,
        End,
        Pause,
        Resume,
        Exit
    }
 
    public class Process
    {
        class StateTransition
        {
            readonly ProcessState CurrentState;
            readonly Command Command;
 
            public StateTransition(ProcessState currentState, Command command)
            {
                CurrentState = currentState;
                Command = command;
            }
 
            public override int GetHashCode()
            {
                return 17 + 31 * CurrentState.GetHashCode() + 31 * Command.GetHashCode();
            }
 
            public override bool Equals(object obj)
            {
                StateTransition other = obj as StateTransition;
                return other != null && this.CurrentState == other.CurrentState && this.Command == other.Command;
            }
        }
 
        Dictionary<StateTransition, ProcessState> transitions;
        public ProcessState CurrentState { get; private set; }
 
        public Process()
        {
            CurrentState = ProcessState.Inactive;
            transitions = new Dictionary<StateTransition, ProcessState>
            {
                { new StateTransition(ProcessState.Inactive, Command.Exit), ProcessState.Terminated },
                { new StateTransition(ProcessState.Inactive, Command.Begin), ProcessState.Active },
                { new StateTransition(ProcessState.Active, Command.End), ProcessState.Inactive },
                { new StateTransition(ProcessState.Active, Command.Pause), ProcessState.Paused },
                { new StateTransition(ProcessState.Paused, Command.End), ProcessState.Inactive },
                { new StateTransition(ProcessState.Paused, Command.Resume), ProcessState.Active }
            };
        }
 
        public ProcessState GetNext(Command command)
        {
            StateTransition transition = new StateTransition(CurrentState, command);
            ProcessState nextState;
            if (!transitions.TryGetValue(transition, out nextState))
                throw new Exception("Invalid transition: " + CurrentState + " -> " + command);
            return nextState;
        }
 
        public ProcessState MoveNext(Command command)
        {
            CurrentState = GetNext(command);
            return CurrentState;
        }
    }
 
 
    public class Program
    {
        static void Main(string[] args)
        {
            Process p = new Process();
            Console.WriteLine("Current State = " + p.CurrentState);
            Console.WriteLine("Command.Begin: Current State = " + p.MoveNext(Command.Begin));
            Console.WriteLine("Command.Pause: Current State = " + p.MoveNext(Command.Pause));
            Console.WriteLine("Command.End: Current State = " + p.MoveNext(Command.End));
            Console.WriteLine("Command.Exit: Current State = " + p.MoveNext(Command.Exit));
            Console.ReadLine();
        }
    }
}

 

 

Monday, 19 September 2016

Relating PDFs

The F# Journal just published an article about processing documents:
"This article tackles the challenge of computing the relationships between a set of PDF files using the commonality of words within them. The iTextSharp library is used to extract the text in PDF documents and the StemmersNet library is then used to convert the words into word stems. A simple numerical method is used to compute the commonality between the word frequencies in different documents and the resulting relationships are visualized using GraphViz..."

To read this article and more, subscribe to The F# Journal today!

Monday, 29 August 2016

Fun with sequences: part 2

The F# Journal just published an article about sequences:
"The previous article solved a variety of problems that permitted simple solutions. This article takes a look at another problem, run-length encoding, that is simple enough to describe but challenging to implement in F# using sequences..."

To read this article and more, subscribe to The F# Journal today!

Fun with sequences: part 1

The F# Journal just published an article about sequences:
"This article takes a look at sequences in F# in the form of the .NET `IEnumerable` class and its more common alias the `seq` type in F#. A variety of sequences are studied and the advantages and disadvantages of sequences discussed, both in the small and also the higher-level impact that sequences can have when exposed over APIs..."
To read this article and more, subscribe to The F# Journal today!