Skip to content

Instantly share code, notes, and snippets.


Justin Spahr-Summers jspahrsummers

View GitHub Profile
jspahrsummers /
Last active Jan 4, 2020
Python asynchronous generator semantics and types

How to use asynchronous generators

The documentation "Asynchronous generator-iterator methods" is phrased really ambiguously. To make matters worse, typing.AsyncGenerator is not specified fully correctly.

Here, I attempt to more clearly capture the actual interface contract, based on what I've read and observed. See also PEP 492 -- Coroutines with async and await syntax and PEP 525 -- Asynchronous Generators.

This might be useful to implement lower-level behaviors than you can with async for, like sending values into the generator function.

TSend = TypeVar('TSend', contravariant=True)
jspahrsummers / MultipleDispatch.cpp
Last active Aug 20, 2016
Example of using RTTI and template specialization for multiple dispatch in C++
View MultipleDispatch.cpp
template<typename Left, typename Right>
struct Intersect
// This will fail to compile if there's no specialization for Intersect<Right, Left>,
// thereby verifying that we've handled all combinations.
typename Intersect<Right, Left>::Result operator() (const Left &lhs, const Right &rhs) const
return Intersect<Right, Left>()(rhs, lhs);
jspahrsummers / ErrorType.swift
Created Aug 8, 2016
An unexpected error has occurred.
View ErrorType.swift
do {
try myFunctionWhichOnlyThrowsOneErrorType()
} catch let err as ThatOneErrorType {
} catch let err {
preconditionFailure("Unexpected error: \(err)")
jspahrsummers / bad.m
Last active Jan 20, 2021
Synchronizing with multiple GCD queues
View bad.m
// DON'T do this, or else you risk a deadlock (e.g., by accidentally performing it in a different order somewhere)
dispatch_async(firstQueue, ^{
dispatch_sync(secondQueue, ^{
// code requiring both queues
jspahrsummers / main.m
Created Nov 24, 2015
[NSThread isMainThread] is probably not what you want!
View main.m
#import <Foundation/Foundation.h>
int main (int argc, const char **argv) {
@autoreleasepool {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"is main thread? %i", (int)[NSThread isMainThread]);
jspahrsummers /
Last active Sep 12, 2015
Excerpt from about API design, especially as it relates to RAC

carlossless [09:50] So anyone used RxSwift? I’m wondering what are your thoughts and opinions vs RAC3.

jspahrsummers [10:00] @carlossless:

carlossless [10:13] Yeah, I was looking for a more practical point of view but even so. RxSwift has UI bindings, it follows the haskell naming for most transformation and composition functions ​map​, ​filter​ etc. rather than ​select​, ​where​.

carlossless [10:14]

View iflet.m
#import <Foundation/Foundation.h>
#define iflet(LHS, RHS) \
for (id obj_ = (RHS); obj_ != nil;) \
for (LHS = (obj_ ?: (RHS)); obj_ != nil; obj_ = nil)
int main(int argc, char *argv[]) {
@autoreleasepool {
NSString *x = nil;
NSString *y = @"y";
jspahrsummers / GHRunLoopWatchdog.h
Created Jan 28, 2015
A class for logging excessive blocking on the main thread
View GHRunLoopWatchdog.h
/// Observes a run loop to detect any stalling or blocking that occurs.
/// This class is thread-safe.
@interface GHRunLoopWatchdog : NSObject
/// Initializes the receiver to watch the specified run loop, using a default
/// stalling threshold.
- (id)initWithRunLoop:(CFRunLoopRef)runLoop;
/// Initializes the receiver to detect when the specified run loop blocks for
jspahrsummers / errors.txt
Last active Aug 29, 2015
swiftc error “Global is external, but doesn't have external or weak linkage!”
View errors.txt
CompileSwift normal x86_64 /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/ReactiveCocoa/Swift/Property.swift
cd /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa
/Applications/ -frontend -c /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/ReactiveCocoa/Swift/Identity.swift /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/ReactiveCocoa/Swift/TupleExtensions.swift /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/ReactiveCocoa/Swift/OptionalExtensions.swift /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/ReactiveCocoa/Swift/Errors.swift /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/ReactiveCocoa/Swift/Event.swift /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/ReactiveCocoa/Swift/Disposable.swift /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/ReactiveCocoa/Swift/FoundationExtensions.swift /Users/justin/GitHub/Mac/Carthage.checkout/ReactiveCocoa/Reac
jspahrsummers / gist:2a66e64afd0d44fbc92a
Created Oct 27, 2014
Build failures for ObjectiveGit iOS
View gist:2a66e64afd0d44fbc92a
*** Cleaning ObjectiveGit iOS...
*** Building and testing ObjectiveGit iOS...
[Info] Loading settings for scheme 'ObjectiveGit iOS' ... (5076 ms)
=== TEST ===
xcodebuild build build
Quick / Quick-iOS (Debug)
~ Check dependencies (227 ms)