Skip to content

Instantly share code, notes, and snippets.

@neilpa
neilpa / keybase.md
Created March 24, 2014 22:08
keybase.io proof

Keybase proof

I hereby claim:

  • I am neilpa on github.
  • I am neilpa (https://keybase.io/neilpa) on keybase.
  • I have a public key whose fingerprint is 206A 9788 172B A7F0 C5DD 0930 5BDE 41AD 8199 B734

To claim this, I am signing this object:

@neilpa
neilpa / CStringArray.swift
Last active August 7, 2020 02:24
Swift wrappers for C functions taking char** arguments
// Usage
let argv = CStringArray(["ls", "/"])
posix_spawnp(nil, argv.pointers[0], nil, nil, argv.pointers, nil)
// Is this really the best way to extend the lifetime of C-style strings? The lifetime
// of those passed to the String.withCString closure are only guaranteed valid during
// that call. Tried cheating this by returning the same C string from the closure but it
// gets dealloc'd almost immediately after the closure returns. This isn't terrible when
// dealing with a small number of constant C strings since you can nest closures. But
@neilpa
neilpa / nv21-convert.sh
Last active October 12, 2023 13:14
Use ffmpeg to convert raw Android camera frame buffers
# Assuming the raw byte[] buffer from onPreviewFrame was written at $1
INPUT=$1
# Need preview size since we dumped to a raw file w/out header info
SIZE=1280x960
# Converting to JPEG
ffmpeg -f image2 -vcodec rawvideo -s $SIZE -pix_fmt nv21 -i $INPUT out.jpeg
# Converting to PNG
@neilpa
neilpa / translit.swift
Created February 5, 2015 23:04
Simple CLI around CFStringTransform for Unicode to ASCII transliteration
#!/usr/bin/env swift
//
// Naive wrapper around CFStringTransform that attempts to transliterate
// Unicode strings to lower-case ASCII
//
// http://nshipster.com/cfstringtransform/
//
import Foundation
@neilpa
neilpa / zipWith.swift
Created February 11, 2015 05:43
Signal.zipWith
private enum Either<T, U> {
case Left(Box<T>)
case Right(Box<U>)
}
public func zipWith<T, U, E>(otherSignal: Signal<U, E>)(signal: Signal<T, E>) -> Signal<(T, U), E> {
return Signal { observer in
var lock = NSRecursiveLock()
lock.name = "org.reactivecocoa.ReactiveCocoa.zipWith"
@neilpa
neilpa / Random.swift
Last active August 29, 2015 14:19
Randomness
protocol Randomizable {
static func random() -> Self
}
protocol IntervalRandomizable: Randomizable, Comparable {
static func random(interval: HalfOpenInterval<Self>) -> Self
static func random(interval: ClosedInterval<Self>) -> Self
}
extension CGFloat: Randomizable {
// Similar to `enumerate` but provides the collection's index type
// rather than an Int for the position
public func iterate<C: CollectionType>(collection: C) -> SequenceOf<(C.Index, C.Generator.Element)> {
var index = collection.startIndex
// type-inference doesn't want to work without this
return SequenceOf { _ -> GeneratorOf<(C.Index, C.Generator.Element)> in
return GeneratorOf {
if index == collection.endIndex {
return nil
@neilpa
neilpa / concatSplices.swift
Last active October 8, 2015 15:17
Building collections from signals[-of-signals] of collection mutations
//
// A half-baked approach (not thread-safe, poor disposable management) for creating
// aggregate collections from multiple signals of collection mutations. In this
// case we are concating multiple mutation streams into a single container. So for
// each inner signal we need to offset subsequent splices by the count of preceding
// items (which can be recovered by scanning previous splices).
//
// Example:
//
// let (first, sink1) = SignalProducer<Splice<Int>, NoError>.buffer()
@neilpa
neilpa / CollectionViewDataSource.swift
Created October 28, 2015 17:04 — forked from andymatuschak/CollectionViewDataSource.swift
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
- (RACSignal*) continueInBackground
{
return [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
UIApplication* app = UIApplication.sharedApplication;
__block UIBackgroundTaskIdentifier taskID;
RACCompoundDisposable* compoundDisposable = [RACCompoundDisposable compoundDisposable];
RACDisposable* backgroundTaskDisposable = [RACDisposable disposableWithBlock:^{
[app endBackgroundTask:taskID];
}];