Skip to content

Instantly share code, notes, and snippets.

View Strilanc's full-sized avatar

Craig Gidney Strilanc

View GitHub Profile
[Test]
public void TestEmpty()
{
CollectionAssert.AreEqual(
new int[0].MinsBy(i => i),
new int[0]);
}
[Test]
public void TestPreserveOrderAndValue()
@Strilanc
Strilanc / gist:7094527
Created October 22, 2013 02:56
Using grand central dispatch to run a callback after a delay, with hacky partial cancellation. Cancelling allows the callback to be deallocated, but doesn't completely clean up what's in GCD.
+(void) runBlock:(VoidBlock)callback
afterDelay:(NSTimeInterval)delayInSeconds
unless:(TOCCancelToken*)unlessCancelledToken {
__block VoidBlock callbackForBlock = callback;
NSObject* lock = [NSObject new];
VoidBlock allowDeallocWrapper = ^{
VoidBlock callbackPeek;
@synchronized(lock) {
@Strilanc
Strilanc / gist:7094786
Created October 22, 2013 03:30
The TOCCancelTokenAndSource.m file from https://github.com/Strilanc/ObjC-CollapsingFutures, stripped of details related to the implementation of whenCancelledDo:unless:. Clone collapsing futures, paste this gist over TOCCancelTokenAndSource.m, and try to get the tests passing again. (Note that the tests aren't testing for thread safety. You may …
#import "TOCCancelTokenAndSource.h"
#import "TOCFutureAndSource.h"
#import "TOCInternal.h"
#include <libkern/OSAtomic.h>
static TOCCancelToken* SharedCancelledToken = nil;
static TOCCancelToken* SharedImmortalToken = nil;
@implementation TOCCancelToken {
@private NSMutableArray* _cancelHandlers;
@Strilanc
Strilanc / gist:7585407
Created November 21, 2013 16:54
Code for NSNotificationCenter subscription controlled by cancellation token.
#import <XCTest/XCTest.h>
#import "TwistedoakCollapsingFutures.h"
#define notificationName @"note"
static int counter = 0;
@interface NSNotificationCenter (CancelToken)
-(void) addObserverForName:(NSString*)name
Imports Tinker.Pickling
Namespace WC3
Public NotInheritable Class GameServer
Inherits DisposableWithTask
Private Shared ReadOnly InitialConnectionTimeout As TimeSpan = 5.Seconds
Private ReadOnly inQueue As CallQueue = MakeTaskedCallQueue
Private ReadOnly outQueue As CallQueue = MakeTaskedCallQueue
@Strilanc
Strilanc / FakeTimeBasedScheduler
Created January 3, 2014 06:16
A test fake for a reactive cocoa scheduler, allowing manual advancement of time.
// FakeScheduler.h
#import "ReactiveCocoa.h"
@interface FakeScheduler : RACScheduler
-(instancetype) init;
-(void) advanceTimeBy:(NSTimeInterval)duration;
-(NSTimeInterval)time;
-(NSDate*) date;
-(NSDate*) dateAtTimeZero;
@Strilanc
Strilanc / BitsPredicateEquivalence
Created November 12, 2014 03:48
Determines whether two Predicate<Predicate<Integer>>s are equivalent. Really.
// An infinite bit-string.
public interface Bits {
// requires n >= 0
boolean bit(int n);
}
// Determines the equivalence of predicates over infinite-bit-strings.
// Requires that the given predicates halt for all inputs.
public final class BitsPredicateEquivalence extends Equivalence<Predicate<Bits>> {
@Strilanc
Strilanc / Arranged_Box_Draw_Chars.md
Last active March 5, 2016 19:29
Various usefully arranged unicode characters

Math

π θ Ψ φ ε ∞
⊕ · ⊗ ∫ ± ∂ √ ∛ ∜
〈 〉 ⟨ ⟩ °
≈ ≤ ≥ ∝
⁰¹²³⁴⁵⁶⁷⁸⁹₀₁₂₃₄₅₆₇₈₉
⁺⁻⁼⁽⁾ⁱⁿ₊₋₌₍₎ₐₑₒₓₔₕₖₗₘₙₚₛₜ
⅟ ½ ⅓ ¼ ⅕ ⅙ ⅐ ⅛ ⅑ ⅒

⅔ ⅖

@Strilanc
Strilanc / increment_to_linear_toffoli.py
Created June 9, 2015 05:54
Hacky python code to build n-bit reversible increment gates out of O(n) Toffoli gates and one ancilla bit.
# coding=utf-8
import math
import itertools
def evaluate_circuit(gates, initial_bits=None):
"""
:param gates: [( [controls], [targets] )]
:param initial_bits: set(on_bits)
@Strilanc
Strilanc / walters_bqp_np_CROT.py
Created November 1, 2015 19:27
A python 3 program that print the operation matrix for a CROT operation from the paper at http://arxiv.org/abs/1510.00409 . The printed operation is just a controlled quarter-turn around the Y axis.
# -*- coding: utf-8 -*-
import numpy as np
import math
import cmath
np.set_printoptions(precision=2, suppress=True)
qubit_count = 2
σx = np.mat([[0, 1], [1, 0]])
σy = np.mat([[0, -1j], [1j, 0]])
σz = np.mat([[1, 0], [0, -1]])