Skip to content

Instantly share code, notes, and snippets.

View Porges's full-sized avatar
🏠
Working from home

George Pollard Porges

🏠
Working from home
View GitHub Profile
@Porges
Porges / async_linq.cs
Last active July 17, 2017 01:11
Support for LINQing over IAsyncEnumerable and or/async results
// Implementations for:
// Select
// - { Enumerable, AsyncEnumerable } * { Action, Func, Func<_,Task>, Func<_,Task<T>> }
// = 8
// - Enumerable/Action and Enumerable/Func (bad? and already defined)
// = 6
// + 2 CancellationToken versions
// = 8

The original code (~7.2s on my laptop).

import System.Random
import System.CPUTime

rainfall :: [Int] -> Int
rainfall xs = sum (zipWith (-) mins xs)
{-# LANGUAGE RankNTypes, PolyKinds, TupleSections, TypeFamilies, TypeFamilyDependencies #-}
import Data.Kind (Type)
type LensFamily (outer :: t -> Type) (inner :: t -> Type) =
forall a b f. Functor f => (inner a -> f (inner b)) -> (outer a -> f (outer b))
type family Snd b a = r | r -> b a where Snd b a = (a, b)
type family Fst a b = r | r -> a b where Fst a b = (a, b)
type family Id t = r | r -> t where Id x = x
struct Six : Nat { public int Value => 6; }
void Main()
{
var four = new Mod<Six>(4);
var two = new Mod<Six>(2);
Console.WriteLine(four + two == 0);
}
@Porges
Porges / QueueBackgroundWork.cpp
Last active August 23, 2017 03:16
Helper for native threadpool use
#include <Windows.h>
#include <functional>
#include <memory>
#include <system_error>
void CALLBACK InvokeFunction(PTP_CALLBACK_INSTANCE, void* context, PTP_WORK)
{
std::unique_ptr<std::function<void()>> owned{ static_cast<std::function<void()>*>(context) };
(*owned)();
@Porges
Porges / example_loop.cs
Created October 17, 2017 23:41
Producer/consumer based on BlockingCollection with a max number of outstanding tasks
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace ExampleLoop
{
class Program
{
const int maxQueuedItems = 10;
@Porges
Porges / menu.ps1
Last active November 9, 2017 03:56
function Menu {
$cmds =
@{ 'notepad' = 'notepad.exe'
; 'paint' = 'mspaint.exe'
}
$procs = @{}
void Main()
{
var digits = Digits.Create("093884765");
var digitsOfLength3 = Digits3.Create("007");
Console.WriteLine($"digits: [{digits}] digitsOfLength3: [{digitsOfLength3}]");
}
static class DigitsDef
{
async Task Main()
{
var c = new AsyncCache<int, int>(async x => { await Task.Delay(x); return x; });
var s = Stopwatch.StartNew();
var x1 = Enumerable.Range(1, 1000);
var results =
await Task.WhenAll(x1.Select(async x =>
await Task.WhenAll(
// hit it in parallel
@Porges
Porges / tree.cs
Last active March 14, 2018 05:29
multiple implicit conversions using tuple syntax
void Main()
{
Tree<int> m = (((1, 2), (3, 4)), (5, (6, 7)));
Console.WriteLine(m.Sum());
}
abstract class Tree<T> : IEnumerable<T>
{
class Leaf : Tree<T>
{