Sunday, 22 January 2017

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!

Sunday, 31 July 2016

Evolution: the weasel program

The F# Journal just published an article about genetic programming:
"In 1986, Richard Dawkins published a book called the Blind Watchmaker about evolution that included a program that was intended to demonstrate evolutionary algorithms running on a computer. The program proved contentious. This article looks at an F# implementation of the program and considers some variations of it to address people's objections to the original..."
To read this article and more, subscribe to The F# Journal today!

Sunday, 12 June 2016

Generating JSON serializers for other languages

The F# Journal just published an article about serialization:
"Serialization is a ubiquitous challenge when writing back-end systems. This article describes how we wrote code in F# that generates OCaml code including both type definitions and JSON serialization/deserialization as well as code to generate many example values adhering to the API for a recent project...."
To read this article and more, subscribe to The F# Journal today!