Skip to content

Instantly share code, notes, and snippets.

type Position =
| Property of string // name of the property
| Station of string // name of the station
| Chance
| CommunityChest
| FreeParking
| GoToJail
| Jail
| Tax of int // amount of tax
let printName position =
match position with
| Property(propertyName) -> sprintf "Property '%s'" propertyName // prints "Property 'Old Kent Road'"
| Tax(amount) -> sprintf "Tax of £%d" amount // prints "Tax of £100"
| CommunityChest -> sprintf "Community Chest"
// etc..
let calculateMove position =
match position with
| GoToJail -> Some Jail // option type - some Position, in this case Jail
| Chance -> randomChancePosition // draws a random Chance card which may or may not be a movement card
| CommunityChest -> randomCommunityChestPosition // as above
| _ -> None // return nothing back
let roll currentPosition dice =
let position = currentPosition |> moveBy dice // return new position, do not mutate current position
printfn "Landed on %s" (printName position)
match calculateMove position with
| Some(movedTo) -> printfn "Moved to %s" (printName movedTo)
movedTo
| None -> position
@isaacabraham
isaacabraham / machinelearning.fs
Created June 18, 2013 17:22
Sample solution for the Kaggle Digits machine learning problem. It does a bit more as the main function also returns the data in a shape that can be bound to a UI in a 28x28 grid. Excuse the lack of a discriminated union for the Matched / Failed bit at the end - it gets directly bound onto the UI in my project
module MachineLearning
open System
open System.IO
let public TRAINING_DATA = "digitssample.csv";
let public LIVE_DATA = "digitscheck.csv"
type RawScribble = { Number:int; Pixels:int[] }
type Scribble = { Number:int; PixelData:int[][] }
let createAgent() =
let tokenSource = new System.Threading.CancellationTokenSource()
Agent.Start((fun inbox -> async {
printfn "spun up a new agent..."
while true do
let! data = inbox.Receive()
let! result = // Do some async work here...
match result with
// Everything done, we can cancel
| AllDone -> printfn "completed!"
@isaacabraham
isaacabraham / memoize-client.cs
Last active December 22, 2015 22:09
Simple memoize function in C# along with client calling code.
// Raw Add method
public static int Add(Tuple<int, int> numbers)
{
return numbers.Item1 + numbers.Item2;
}
static void Main(string[] args)
{
// Memoized version of Add
var add = Memoize<Tuple<int, int>, int>(Add);
@isaacabraham
isaacabraham / memoize-client.fs
Last active December 22, 2015 23:49
Demonstrates a simple generate memoize function in F#
// Raw add function
let add (x,y) = x + y
// Memoized version
let add = memoize add
add (5,10) // Adding (5,10) to cache
add (5,10) // Cache hit for (5,10)
@isaacabraham
isaacabraham / asyncsample.cs
Created October 2, 2013 11:00
Sample of sequential vs parallel vs async code
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApplication1
@isaacabraham
isaacabraham / JustMockRecursiveMocks.cs
Last active December 24, 2015 23:48
A few tests that illustrate how JustMockLite easily handles recursive mocks.
public interface Service
{
OtherService GetOtherService();
}
public interface OtherService
{
Int32 GetNumber();
}