Skip to content

Instantly share code, notes, and snippets.

View JonCanning's full-sized avatar

Jon Canning JonCanning

View GitHub Profile
@JonCanning
JonCanning / gist:c95340adaa3bf4d8c215
Created June 9, 2014 21:34
RavenDB IDocumentStore Extensions
module IDocumentStoreExtensions
open Raven.Client
open System.Linq
type IDocumentStore with
member this.getAll<'a>() =
let rec load results =
use session = this.OpenSession()
module portFinder
open System.Net
open System.Net.Sockets
let nextPort() =
TcpListener(IPAddress.Loopback, 0) |> fun l ->
l.Start()
(l, (l.LocalEndpoint :?> IPEndPoint).Port) |> fun (l, p) ->
l.Stop()
module SitemapParser
open FSharp.Data
open System.IO
open System.IO.Compression
open System.Net.Http
type SiteMapIndexProvider = XmlProvider< """<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>location</loc><lastmod>2000-01-01T00:00:00+00:00</lastmod></sitemap><sitemap><loc>location</loc><lastmod>2000-01-01T00:00:00+00:00</lastmod></sitemap></sitemapindex>""" >
type SiteMapProvider = XmlProvider< """<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><url><loc>location</loc><lastmod>2000-01-01T00:00:00+00:00</lastmod><changefreq>monthly</changefreq><priority>0.0</priority></url><url><loc>location</loc><lastmod>2000-01-01T00:00:00+00:00</lastmod><changefreq>monthly</changefreq><priority>0.0</priority></url></urlset>""" >
@JonCanning
JonCanning / gist:6ebe1cd0afb21c73b04b
Created September 20, 2014 10:10
Local IP Addresses
System.Net.Dns.GetHostName() |> System.Net.Dns.GetHostEntry |> fun x -> x.AddressList |> Seq.filter (fun x -> x.AddressFamily = System.Net.Sockets.AddressFamily.InterNetwork) |> Seq.map (fun x -> x.ToString())
@JonCanning
JonCanning / gist:3176a06b0e6d4816dea4
Last active May 23, 2017 11:17
FizzBuzz decision tree
type Result =
| Text of string
| Number of int
override self.ToString() =
match self with
| Text s -> s
| Number i -> i.ToString()
type Tree =
| Branch of (int -> Result) * Tree
let (>>=) (i1, s1) k =
let i2, s2 = k i1
(i2, sprintf "%s\r%s" s1 s2)
let half i =
let half = i / 2
(half, sprintf "half of %i is %i" i half)
half 8 >>= half >>= half |> snd |> printfn "%s"
let logs = ["Awesome";"Win";"Mathematical";"Skill"]
let (>>=) m k r = (m r, r) ||> k
let add1 i (logs : string list) =
let result = i + 1
logs.[result] |> printfn "%s"
result
let add2 i (logs : string list) =
let result = i + 2
logs.[result] |> printfn "%s"
result
let retry f =
let rec innerRetry f i =
try
f()
with
| ex when i > 0 ->
Syslog.Error ex
Syslog.Info("Retry no {0}", (i - 5) * -1)
Async.Sleep 1000 |> Async.RunSynchronously
innerRetry f <| i - 1
let asQueryString o =
o.GetType().GetProperties()
|> Array.map (fun p -> (p.Name, p.GetValue(o) |> string))
|> Array.map (fun (name, value) ->
let m = Regex.Match(value, "Some\((?<value>.*?)\)")
(name, if m.Groups.Count = 2 then m.Groups.["value"].Value else value))
|> Array.map (fun (name, value) -> sprintf "%s=%s" name value)
|> String.concat "&"
|> sprintf "?%s"
@JonCanning
JonCanning / TestFramework
Created May 22, 2015 19:31
TestFramework
[<AutoOpen>]
module TestFramework
open NUnit.Framework
let private assrt assertion actual expected = assertion(box expected, box actual, sprintf "Expected: %+A\rActual: %+A" expected actual)
let (==) actual expected = assrt Assert.AreEqual actual expected
let (!=) actual expected = assrt Assert.AreNotEqual actual expected
type Test = TestAttribute