Skip to content

Instantly share code, notes, and snippets.

View theburningmonk's full-sized avatar

Yan Cui theburningmonk

View GitHub Profile
@theburningmonk
theburningmonk / gist:1186303
Created September 1, 2011 14:41
An implementation of protobuf-net body deserialize to use with Nancy
public sealed class ProtobufNetBodyDeserializer : IBodyDeserializer
{
public bool CanDeserialize(string contentType)
{
return IsProtoBufType(contentType);
}
public object Deserialize(string contentType, Stream bodyStream, BindingContext context)
{
// deserialize the body stream into the destination type
@theburningmonk
theburningmonk / gist:1186335
Created September 1, 2011 14:55
An simple Nancy module which uses protobuf-net to serialize the response body
public class MainModule : NancyModule
{
public MainModule()
{
Post["/bindProtobuf"] =
x =>
{
User data = this.Bind();
return new Response
@theburningmonk
theburningmonk / ProtoBufResponse.cs
Created September 1, 2011 15:16
A simple wrapper of Nancy.Response that takes in an object to use as the response body
public class ProtoBufResponse : Response
{
public ProtoBufResponse(object body)
{
ContentType = "application/x-protobuf";
Contents = stream => ProtoBuf.Serializer.Serialize(stream, body);
}
public ProtoBufResponse WithStatusCode(HttpStatusCode httpStatusCode)
{
@theburningmonk
theburningmonk / gist:1188378
Created September 2, 2011 10:57
Nancy self-hosting example
// a simple module to be hosted in the console app
public class MainModule : NancyModule
{
public MainModule()
{
Get["/"] = x => { return "Hello World"; };
}
}
static void Main(string[] args)
@theburningmonk
theburningmonk / gist:1191999
Created September 4, 2011 00:22
Simple nancy module
public class MainModule : NancyModule
{
public MainModule()
{
Get["/"] = x =>
{
return "This is the root.";
};
}
}
@theburningmonk
theburningmonk / gist:1199393
Created September 7, 2011 00:23
ProjectEuler - Problem 99 Solution
open System.IO
let (answer, _) =
File.ReadAllLines(@"c:\temp\base_exp.txt")
|> Array.mapi (fun i l -> (i + 1, l.Split(',') |> Array.map float))
|> Array.map (fun t -> match t with | (i, arr) -> (i, arr.[1] * (log arr.[0])))
|> Array.maxBy (fun t -> match t with | (i, l) -> l)
@theburningmonk
theburningmonk / gist:1200242
Created September 7, 2011 10:35
ProjectEuler - Problem 81 Solution
open System
open System.IO
let dimension = 80
// load the original matrix
let matrix = array2D (File.ReadAllLines(@"c:\temp\matrix.txt")
|> Array.map (fun l -> l.Split(',') |> Array.map int32))
// init the shortes sum matrix
@theburningmonk
theburningmonk / gist:1200719
Created September 7, 2011 14:30
ProjectEuler - Problem 85 Solution
open System
let target = 2000000
// function to work out the number of rects in a grid of x by y
let getRectCount x y = (x * x + x) * (y * y + y) / 4
// try x and y dimensions up to 100
let answer =
seq {
@theburningmonk
theburningmonk / gist:1201111
Created September 7, 2011 17:02
ProjectEuler - Problem 205 Solution
// recursive function that'll return all the possible scores
let rec getScores numbers acc iter max =
if iter < max
then numbers |> List.collect (fun n -> getScores numbers (acc + n) (iter + 1) max)
else numbers |> List.map (fun n -> n + acc)
// gets the possible scores and the associated probability of achieving that score
let getScoreProbs numbers count =
// given the set of possible numbers on each dice and the number of dice
// what's the prob of getting each permutation (1, 1, 2, 3)
@theburningmonk
theburningmonk / gist:1201134
Created September 7, 2011 17:12
ProjectEuler - Problem 206 Solution
let predicate n =
let nStr = n.ToString()
// make sure n is a 19 digit number
if nStr.Length <> 19 then false
// check the number matches the pattern 1_2_3_4_5_6_7_8_9_0
else [1..10] |> List.forall (fun i -> int(nStr.[2*(i-1)].ToString()) = i % 10)
// wonderful bit of brute force CPU..
let answer =
let mutable n = 1000000000L