Skip to content

Instantly share code, notes, and snippets.

View dnedrow's full-sized avatar

David Nedrow dnedrow

View GitHub Profile
@dnedrow
dnedrow / doOnce.swift
Created May 15, 2018 14:46 — forked from rnapier/doOnce.swift
A function that only executes once. Good Swift or over-clever?
// Swift3 gets rid of dispatch_once and recommends replacing it with a lazy global.
// That's very straightforward when dispach_once is used to initialize something, but
// isn't an exact match when you want something to execute once, and then become a noop
// in a thread-safe way.
// The following approach seems completely "correct" and I guess actually a bit elegant,
// if by "elegant" you mean "terse and not immediately obvious to the reader, which makes
// you look very clever."
var doOnce: () -> Void = {
@dnedrow
dnedrow / doOnce.swift
Created May 15, 2018 14:48 — forked from samsonjs/doOnce.swift
A function that only executes once. Good Swift or over-clever?
// Swift3 gets rid of dispatch_once and recommends replacing it with a lazy global.
// That's very straightforward when dispach_once is used to initialize something, but
// isn't an exact match when you want something to execute once, and then become a noop
// in a thread-safe way.
// The following approach seems completely "correct" and I guess actually a bit elegant,
// if by "elegant" you mean "terse and not immediately obvious to the reader, which makes
// you look very clever."
var doOnce: () -> Void = {
@dnedrow
dnedrow / version_comparator.sh
Last active November 18, 2020 12:21
bash function for comparing version numbers
#!/usr/bin/env bash
function version_gt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" > "$1"; }
function version_eq() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" = "$1"; }
function version_lt() { test "$(printf '%s\n' "$@" | sort -V | head -n 1)" < "$1"; }
RUBY_VERSION=$(/usr/bin/ruby -v | awk '{print $2}' | cut -d'p' -f1)
echo "ruby version is ${RUBY_VERSION}"
@dnedrow
dnedrow / Signal.swift
Created September 22, 2018 12:35 — forked from danielt1263/Signal.swift
Swift replacement for KVO
//
// Signal.swift
//
// Created by Daniel Tartaglia on 9/6/15.
// Copyright © 2016 Daniel Tartaglia. MIT License.
//
public protocol Disposable {
func dispose()
}
//
// UIImage+Extensions.swift
//
// Created by Daniel Tartaglia on 4/25/16.
// Copyright © Daniel Tartaglia. MIT License.
//
import UIKit

Two Level Type Erasing in Swift 3

Recently I converted a project of mine to Swift 3 (https://github.com/dtartaglia/XStreamSwift) and I had to deal with a problem that I couldn't find an answer for. This article is about the problem and the solution I discovered.

There are lots of articles on the web about type erasing in Swift, but all the ones I found only dealt with a single level. I will recap the concept quickly:

protocol Listener
{

associatedtype ListenerValue

//
// UIViewAdditions.swift
//
// Created by Daniel Tartaglia on 04/15/15.
// Copyright © 2016. MIT License.
//
import UIKit
@dnedrow
dnedrow / RandomNumbers.swift
Created October 2, 2018 21:17 — forked from jstn/RandomNumbers.swift
generate random numbers for 64-bit types while mitigating modulo bias
/*
`arc4random_uniform` is very useful but limited to `UInt32`.
This defines a generic version of `arc4random` for any type
expressible by an integer literal, and extends some numeric
types with a `random` method that mitigates for modulo bias
in the same manner as `arc4random`.
`lower` is inclusive and `upper` is exclusive, thus:
@dnedrow
dnedrow / pag.sh
Last active December 26, 2018 19:54
Simple bash function that uses `ag` (Silver Surfer) to search for a process with the given name, while excluding `ag` itself.
#!/bin/bash
# Simple function that uses ag (Silver Surfer) to grep for a process,
# excluding ag itself. See the use of [] in AG_ARG.
function pag() {
if [ -x "$(which 'ag')" ]; then
typeset PROC_FIRST="${1[1]}"
typeset PROC_REST="${1:1}"
typeset AG_ARG="[${PROC_FIRST}]${PROC_REST}"
ps ax | ag "$AG_ARG"
@dnedrow
dnedrow / createbranch
Last active February 13, 2019 13:59
Script to create a new git branch from a given repo/branch
#!/bin/zsh
# If necessary, adjust the above to point to your ZSH executable.
# Script to create a new git branch from a given repo/branch
# Written by David E Nedrow
# Last updated 2019-02-13 00:42 Eastern
# Check for the most recent version at:
# https://gist.github.com/dnedrow/4467592b0d8ecd7260451e7b01bf4f12
local base="upstream/develop"