Skip to content

Instantly share code, notes, and snippets.

View kjnilsson's full-sized avatar

Karl Nilsson kjnilsson

View GitHub Profile
@kjnilsson
kjnilsson / blocking collection corruption test.cs
Created June 29, 2012 12:51
blocking collection corruption test
var queue = new ConcurrentQueue<string>();
var bc = new BlockingCollection<string>(queue, 1);
bc.Add("first");
string s;
queue.TryDequeue(out s);
bc.Add("blocking"); // blocks here even though internal queue is empty
@kjnilsson
kjnilsson / gist:5014286
Last active December 14, 2015 02:29
recursive merge multiple sorted sequences
open FSharpx.Collections
let first = LazyList.ofSeq(seq { 0..3..30 } |> Seq.map (fun x -> (x, "first") ))
let second = LazyList.ofSeq(seq { 0..2..30 } |> Seq.map (fun x -> (x, "second")))
let third = LazyList.ofSeq(seq { 0..30 } |> Seq.map (fun x -> (x, "third")))
module LazyList =
let takeWhileAsList pred (list:LazyList<'T>) =
let rec inner l (r:List<'T>) =
@kjnilsson
kjnilsson / gist:5956856
Created July 9, 2013 12:08
Why can't I deconstruct the tuple that returns a function that returns and async and a function?
let test name =
fun (msg : obj) ->
async {
printfn name }, id
let f, f2 = Ipc.test "hello"
//gives:
//error FS0001: This expression was expected to have type
@kjnilsson
kjnilsson / interop.fs
Created October 4, 2013 08:30
Interop with libraries written in csharp
let dependencies (package : IPackage) =
match package.DependencySets with
| null -> []
| sets ->
sets
|> filterNulls
|> Seq.map (fun d -> d.Dependencies)
|> filterNulls
|> Seq.concat
|> filterNulls
@kjnilsson
kjnilsson / gist:7005246
Created October 16, 2013 09:41
gcroot dump
0:000> !gcroot -nostacks 02bc6fb8
HandleTable:
00131120 (strong handle)
-> 02afd94c System.Threading._ThreadPoolWaitOrTimerCallback
-> 02afd904 System.Threading.WaitOrTimerCallback
-> 02afd8e0 <StartupCode$FSharp-Core>.$Control+AwaitWaitHandle@1548-2
-> 027b1f3c Microsoft.FSharp.Control.AsyncParamsAux
-> 027b1f2c Microsoft.FSharp.Control.AsyncBuilderImpl+econt@807-3[[Microsoft.FSharp.Core.Unit, FSharp.Core]]
-> 027b1f20 <StartupCode$FSharp-Core>.$Control+p@2287-1[[System.Tuple`2[[System.Guid, mscorlib],[System.Object, mscorlib]], mscorlib]]
-> 027ba178 Microsoft.FSharp.Control.FSharpMailboxProcessor`1[[System.Tuple`2[[System.Guid, mscorlib],[System.Object, mscorlib]], mscorlib]]
@kjnilsson
kjnilsson / gist:7708091
Created November 29, 2013 16:18
Null ref exception when compiling fsharp
warning FS0075: The command-line option '--version' has been deprecated
warning FS0075: The command-line option 'times' is for internal use only
TIME: 0.2 Delta: 0.0 Mem: 26 G0: 0 G1: 0 G2: 0 [Import mscorlib]
TIME: 0.2 Delta: 0.0 Mem: 26 G0: 0 G1: 0 G2: 0 [Import mscorlib and FSharp.Core.dll]
TIME: 0.4 Delta: 0.2 Mem: 57 G0: 6 G1: 1 G2: 1 [Import system references]
/home/karl/fsharp/src/fsharp/FSharp.Build-proto/.libs/proto/FSBuild.fs(17,5): error FS0193: internal error: Object reference not set to an instance of an object
/home/karl/fsharp/src/utils/CompilerLocationUtils.fs(9,9): error FS0193: internal error: Object reference not set to an instance of an object
@kjnilsson
kjnilsson / gist:8317102
Created January 8, 2014 13:57
Deduping agent
open System
open System.Collections.Concurrent
type DedupeProtocol<'T> =
| Add of 'T
| Remove of 'T
type DedupingAgent<'T when 'T : comparison>(handle : 'T -> unit) =
let queue = new BlockingCollection<'T>()
@kjnilsson
kjnilsson / gist:300a7693f401ca104512
Created May 8, 2014 10:58
Delegating handler to capture request processing times
type TimingHandler() =
inherit DelegatingHandler ()
let cont (sw : Stopwatch) (t: Task<HttpResponseMessage>) =
sw.Stop()
debug "request time: %i" sw.ElapsedMilliseconds
t.Result // dont like this
override this.SendAsync (request, token) =
let sw = new Stopwatch()
Found 1 unique roots (run '!GCRoot -all' to see all roots).
0:000> !gcroot 161f97f0
HandleTable:
044f4788 (strong handle)
-> 161f9b64 System.Threading._ThreadPoolWaitOrTimerCallback
-> 161f9b1c System.Threading.WaitOrTimerCallback
-> 161f9af8 <StartupCode$FSharp-Core>.$Control+AwaitWaitHandle@1624-2
-> 161f9a80 Microsoft.FSharp.Control.AsyncBuilderImpl+args@797-1[[System.Boolean, mscorlib],[Microsoft.FSharp.Core.FSharpOption`1[[Riemann.Proto.Event, Riemann]], FSharp.Core]]
-> 161f9a60 Microsoft.FSharp.Control.AsyncParams`1[[Microsoft.FSharp.Core.FSharpOption`1[[Riemann.Proto.Event, Riemann]], FSharp.Core]]
type IdGenerator () =
let agent = MailboxProcessor<AsyncReplyChannel<int>>.Start(fun inbox ->
let rec loop c = async {
let! rc = inbox.Receive ()
rc.Reply (c + 1)
return! loop (c + 1) }
loop 0)
member this.Next () =
agent.PostAndReply(fun rc -> rc)