Skip to content

Instantly share code, notes, and snippets.

View eiriktsarpalis's full-sized avatar

Eirik Tsarpalis eiriktsarpalis

View GitHub Profile
@eiriktsarpalis
eiriktsarpalis / PriorityQueue.cs
Last active September 22, 2020 17:34 — forked from safern/PriorityQueue.cs
PriorityQueue api surface
namespace System.Collections.Generic
{
class PriorityQueue<TElement, TPriority> : IReadOnlyCollection<(TElement Element, TPriority Priority)>
{
#region Constructors
public PriorityQueue() { }
public PriorityQueue(IComparer<TPriority> comparer) { }
public PriorityQueue(IEnumerable<(TElement Element, TPriority Priority)> values) { }
public PriorityQueue(IEnumerable<(TElement Element, TPriority Priority)> values, IComparer<TPriority> comparer) { }
#endregion
@eiriktsarpalis
eiriktsarpalis / Maybe.cs
Last active April 6, 2020 10:26
Encoding a Maybe monad in C# using Eff, https://github.com/nessos/Eff
namespace Maybe
{
public struct Maybe<T>
{
public bool HasValue { get; }
public T Value { get; }
private Maybe(T value)
{
HasValue = true;
@eiriktsarpalis
eiriktsarpalis / Results.md
Last active February 5, 2020 16:53
ObjectIdGenerator Performance
Method N Mean Error StdDev Gen 0 Gen 1 Gen 2 Allocated
ObjectIdGenerator 10 261.0 ns 3.92 ns 3.28 ns 0.0176 - - 280 B
DictObjectIdGenerator 10 415.4 ns 7.92 ns 8.81 ns 0.0167 - - 264 B
ObjectIdGenerator 100 2,237.4 ns 38.87 ns 32.45 ns 0.0458 - - 776 B
DictObjectIdGenerator 100 3,697.2 ns 72.95 ns 71.64 ns 0.0648 - - 1040 B
ObjectIdGenerator 1000 27,641.9 ns 465.12 ns 412.32 ns 0.0916 - - 1912 B
DictObjectIdGenerator 1000 35,959.2 ns
@eiriktsarpalis
eiriktsarpalis / ackermann.cs
Created November 19, 2019 12:48
Task vs ValueTask performance
using System;
using System.Threading.Tasks;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
namespace ValueTaskBenchmarks
{
// Uses the Ackermann function to measure performance of task and friends
// c.f. https://en.wikipedia.org/wiki/Ackermann_function
@eiriktsarpalis
eiriktsarpalis / ThunkBuilder.cs
Last active April 6, 2020 10:26
Delayable MethodBuilders
using System;
using System.Collections.Concurrent;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.ExceptionServices;
using System.Security;
using System.Threading.Tasks;
namespace ThunkBuilder
{
@eiriktsarpalis
eiriktsarpalis / Atom.cs
Created June 22, 2019 12:20
Clojure-style atoms for C#
using System;
using System.Threading;
using System.Threading.Tasks;
namespace Atom
{
public class Atom<T> where T : class
{
private T _value;
@eiriktsarpalis
eiriktsarpalis / slnTools.fsx
Created December 17, 2018 13:40
Solution Generation
[<RequireQualifiedAccess>]
module SlnTools
// Simple tool for generating Solution files out of a list of projects.
open System
open System.IO
open System.Text.RegularExpressions
let fsharpProjectTypeGuid = Guid.Parse "6EC3EE1D-3C4E-46DD-8F32-0CC8E7565705"
@eiriktsarpalis
eiriktsarpalis / keybase.md
Created October 6, 2018 18:05
KeyBase proof

Keybase proof

I hereby claim:

  • I am eiriktsarpalis on github.
  • I am eiriktsarpalis (https://keybase.io/eiriktsarpalis) on keybase.
  • I have a public key ASDsjo4ajiE7h4qGM_HbGrq2lco-Kj5LfRn2uCjZ2axtIQo

To claim this, I am signing this object:

@eiriktsarpalis
eiriktsarpalis / for-cancellation.fsx
Created October 26, 2017 12:01
Encoding Cancellation on top of foreach loops
// One of the common annoyances when writing imperative code in F# is that the
// expression-based nature of the language makes it effectively impossible to
// break or return in the midst of a for-loop. This is an attempt to encode
// lightweight cancellation on top of native for loops. As usual, this is intended
// as a PoC and should not be considered seriously for production code.
module Seq =
open System.Collections
open System.Collections.Generic
@eiriktsarpalis
eiriktsarpalis / test.fsx
Created August 2, 2017 13:59
Class vs Struct Index Performance
#time "on"
open System
open System.Collections.Generic
let gdata = [|for i in 1 .. 1000000 -> Guid.NewGuid()|]
let sdata = gdata |> Array.map string
// Real: 00:00:17.774, CPU: 00:00:17.703, GC gen0: 378, gen1: 61, gen2: 1
let gmap = ref Map.empty<Guid, int>