Skip to content

Instantly share code, notes, and snippets.

@isaacabraham
isaacabraham / azuretypeprovidertables.fs
Last active August 29, 2015 13:56
Sample of using the Azure Table Storage F# Type Provider
type myStorageAccount = Elastacloud.FSharp.AzureTypeProvider.AzureAccount< "AccountName", "AccountKey">
// Get all Fulham players, get the name cost, sorting in value
myStorageAccount.Tables.Players.GetPartition("Fulham")
|> Seq.map(fun player -> player.Name, player.Cost)
|> Seq.sortBy snd
|> Seq.toArray
@isaacabraham
isaacabraham / hadoopfs_sample.fs
Last active August 29, 2015 13:56
Example of word count in HadoopFs
/// A sample map reducer that counts words in a document. Notice how the outputs of both map and reduce do not have to be strings.
module HadoopFs.Samples.WordCount
open System
/// A sample mapper that splits lines based on spaces into words and counts the number of occurences within a line.
let Mapper (row : string) =
row.Split([| ' ' |], StringSplitOptions.RemoveEmptyEntries)
|> Seq.countBy id
/// A sample reducer that counts words supplied from the word count mapper.
@isaacabraham
isaacabraham / hadoopfs_runner.fs
Last active August 29, 2015 13:56
Sample showing how to use HadoopFs
/// A mapper exe
let mainMap argv =
doMap <| ManyOutputs WordCount.Mapper
0
/// A reducer exe
let mainReduce argv =
doReduce <| SingleOutput WordCount.Reducer
0
/// The different types of outcomes an aspect can have
type AspectResult<'TInput, 'TOutput> =
| ContinueWith of 'TInput
| ErrorOf of Exception
| ReturnWith of 'TOutput
/// The signature that any aspect has
type Aspect<'TInput, 'TOutput> = 'TInput -> AspectResult<'TInput, 'TOutput>
// Function that we will decorate
let add (first, second) = first + second
/// Prints the provided arguments to the console and passes control to the next item in the pipeline
let printArgsAspect args =
printfn "in print!"
printfn "arguments are: %A" args
ContinueWith args
/// Returns an error if the secondary value is zero, otherwise continues
/// Connects two aspects to one another
let boltAspect aspect aspectResult =
match aspectResult with
| ContinueWith args ->
printfn "continuing"
aspect args
| _ ->
printfn "prematurely exiting"
aspectResult
// Construct add with three aspects
let addWithAspects =
stopDivideByZeroAspect
>> boltAspect postiveOnlyAspect
>> boltAspect printArgsAspect
>> closeBolt add
// Call the "composed" add - has the same signature as the original add function
addWithAspects(10,15)
/// Bolt two aspects together.
let (->>) x y = x >> boltAspect y
/// Bolts an aspect to a function.
let (>>!) x y = x >> closeBolt y
let addWithAspects = stopDivideByZeroAspect ->> postiveOnlyAspect ->> printArgsAspect >>! add
addWithAspects(0, 10)
(*
try
{
Foo.Bar();
}
catch (System.IO.FileLoadException)
{
Console.WriteLine("File load exception!");
}
catch (System.Net.HttpListenerException ex)
{
@isaacabraham
isaacabraham / exampleIfThenOnNumbers.cs
Last active August 29, 2015 14:01
What pattern matching might look like in C#
var numbers = Foo.GetNumbers();
if (numbers.Length == 2 && numbers[0] == 7)
{
var second = numbers[1];
Console.WriteLine("7,{1}", second);
}
else if (numbers.Length == 2)
{
var first = numbers[0];