Skip to content

Instantly share code, notes, and snippets.

View mathias-brandewinder's full-sized avatar

Mathias Brandewinder mathias-brandewinder

View GitHub Profile
namespace FSharpTests
module Analytics =
open System
let getData (skuId: int) =
// fake data for now:
// a list of (date, value) observations
[ DateTime(2010, 1, 1), 10.;
@mathias-brandewinder
mathias-brandewinder / sampler.fsx
Created June 3, 2013 19:07
Sampling from items with known proportions.
open System
// Sample with replacement
let replaceSampler (rng: Random) (counts: int[]) =
let N = counts |> Array.sum
let draw = rng.Next(N)
let rec find index cumul =
let n = counts.[index]
if draw < (cumul + n)
then index
open System
open System.IO
let path = @"C:\Users\Mathias\Desktop\Dojo\DigitsSample.csv"
let data = File.ReadAllLines(path)
let parsed = data |> Array.map (fun line -> line.Split(','))
let parsed2 = parsed.[1..] |> Array.map (fun line -> line |> Array.map (fun x -> Convert.ToInt32(x)))
type Example = { Number:int; Pixels:int[] }
let sample = parsed2 |> Array.map (fun x -> { Number = x.[0]; Pixels = x.[1..] })
@mathias-brandewinder
mathias-brandewinder / GrabCityLocation
Created February 14, 2013 23:08
Simple usage of WSDL type provider to grab city location
#I @"C:\Program Files (x86)\Reference Assemblies\Microsoft\FSharp\3.0\Runtime\v4.0\Type Providers"
open System
#r "System.ServiceModel.dll"
open Microsoft.FSharp.Linq
#r "FSharp.Data.TypeProviders"
type terraService = Microsoft.FSharp.Data.TypeProviders.WsdlService<"http://terraserver-usa.com/TerraService2.asmx?WSDL">
let terraClient = terraService.GetTerraServiceSoap ()
@mathias-brandewinder
mathias-brandewinder / excel and f# demo
Created February 14, 2013 19:17
Run k-means clustering on list of cities and plot results as labeled scatterplot in Excel
type City = { Name: string; Lat: float; Lng: float }
let cities =
[ ("San Francisco", "CA", "United States");
("New York", "NY", "United States");
("Hoboken", "NJ", "United States");
("Redmond", "WA", "United States");
("Seattle", "WA", "United States");
("Boston", "MA", "United States");
("Boston", "MA", "United States");
@mathias-brandewinder
mathias-brandewinder / Write to Excel from F#
Created January 21, 2013 00:54
Attach to running Excel instance in F# and write to a Worksheet
#r "office.dll"
#r "Microsoft.Office.Interop.Excel"
open Microsoft.Office.Interop.Excel
open System.Runtime.InteropServices
let xl = Marshal.GetActiveObject("Excel.Application") :?> Microsoft.Office.Interop.Excel.Application
let wbs = xl.Workbooks
let wb = wbs.[1]
let sh = wb.Worksheets.[1] :?> Worksheet
sh.Cells.[1,1] <- "Hello from F#!"
@mathias-brandewinder
mathias-brandewinder / cards.clj
Last active November 9, 2015 01:49
Wonderland katas
(def suits [:spade :club :diamond :heart])
(def ranks [2 3 4 5 6 7 8 9 10 :jack :queen :king :ace])
@mathias-brandewinder
mathias-brandewinder / turtle.fsx
Last active September 10, 2015 16:25
Turtle: draft dojo
(*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
'Of Turtles & Discriminated Unions'
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
The goal of this exercise is to introduce F# discriminated
unions, and how they can be used to design a DSL.
open System
let readInt () = Console.In.ReadLine() |> int
let N = readInt ()
let readline = [ for i in 0 .. N - 1 -> readInt () ]
let minimumBetweenTwoNumbers n1 n2 = min n1 n2
let rec minimum data mini =
let rec minimum data mini =
match data with
| first::second::tail ->
let min = abs (first - second) |> minimumBetweenTwoNumbers mini
match tail with
| [] -> min
| _ -> minimum second::tail min