Skip to content

Instantly share code, notes, and snippets.

View JonCanning's full-sized avatar

Jon Canning JonCanning

View GitHub Profile
@JonCanning
JonCanning / gist:2039149
Created March 14, 2012 20:09
ToVerbsString
private static string ToVerbsString(ApplyTo verbs)
{
return string.Join(" ", Enum.GetValues(typeof(ApplyTo)).Cast<ApplyTo>().Where(x => x != ApplyTo.All && x != ApplyTo.None && verbs.Has(x)).Select(x => x.ToString().ToUpper()));
}
@JonCanning
JonCanning / gist:2204835
Created March 26, 2012 12:48
Example User Service
public class RegisterUserService : IService<RegisterUser>
{
public object Execute(RegisterUser request)
{
//save user
//map saved user from User to UserResponse
return new UserResponse();
}
}
@JonCanning
JonCanning / gist:5120091
Created March 8, 2013 21:37
ChildModelBindingPlugin
public class ChildModelBindingPlugin : IPlugin
{
public void Register(IAppHost appHost)
{
appHost.RequestFilters.Add(RequestFilter);
}
static void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
{
requestDto = Bind(requestDto, req.FormData);
@JonCanning
JonCanning / gist:6120931
Last active December 20, 2015 11:09
RomanNumeral
struct RomanNumeral
{
static readonly List<RomanNumeral> RomanNumerals;
readonly int integer;
readonly string roman;
static RomanNumeral()
{
RomanNumerals = new List<RomanNumeral>
{
@JonCanning
JonCanning / gist:7607687
Last active December 29, 2015 03:29
Project Euler
Useful functions:
Convert number to array of numbers:
Seq.unfold (function | x when x = 0 -> None | x when x > 10 -> Some(x / 10, x % 10) | x -> Some(x, 0)) 81
Primes:
Seq.filter (fun x -> {2..x |> float |> sqrt |> int} |> Seq.forall(fun y -> x = 2 || x % y > 0))
-----------------------------------------------
@JonCanning
JonCanning / gist:8446758
Last active January 3, 2016 09:59
GivenWhen base class for tests
abstract class Given
{
protected static When Given_(Action action)
{
action();
return new When();
}
protected class When
{
@JonCanning
JonCanning / gist:8482165
Last active January 3, 2016 15:19
Random user data
open System
let random low hi len =
let random = Random()
seq {for i in 1..len -> char <| random.Next(int low, int hi) } |> String.Concat
let text() =
random 'a' 'z' 10
let email() =
[<TestFixture>]
type When() =
[<DefaultValue>] val mutable message : string
do
printfn "Constructor"
[<TestFixtureSetUp>]
member x.TestFixtureSetUp() =
module IISExpress
open System.Diagnostics
open System
open System.IO
let killProcess processName =
let processStartInfo = ProcessStartInfo(FileName = "taskkill",
Arguments = "/F /IM " + processName,
UseShellExecute = false,
@JonCanning
JonCanning / gist:2f5c22c48bfbd24367ac
Last active August 29, 2015 14:02
FizzBuzzLucky
type Result =
| Text of string
| Number of int
override self.ToString() = match self with | Text s -> s | Number i -> i.ToString()
let (>>=) f1 f2 m = match f1 m with | Text _ as r -> r | Number i -> f2 i
let rule f s = function | i when f i -> Text s | i -> Number i
let fizz = rule (fun i -> i % 3 = 0) "fizz"
let buzz = rule (fun i -> i % 5 = 0) "buzz"
let fizzbuzz = rule (fun i -> i % 3 = 0 && i % 5 = 0) "fizzbuzz"
let lucky = rule (fun i -> i.ToString().Contains "3") "lucky"