Skip to content

Instantly share code, notes, and snippets.


Dan Rosenstark drosenstark

View GitHub Profile
drosenstark / DispatchQueue+Util.swift
Created Feb 21, 2022
Extension for easy testing of async code
View DispatchQueue+Util.swift
// Copyright 2022 Confusion Studios LLC
// by Dan Rosenstark
import Foundation
/// Extension on DispatchQueue? for Testing
/// If you don't have a DispatchQueue, run immediately (on current queue)
extension Optional where Wrapped : DispatchQueue {
public func asyncIfNotNil(execute block: @escaping ()->()) {
if let self = self {
self.async(execute: block)
drosenstark / NSArray+Filter.m
Created Jan 18, 2021
Simple block filter on NSArray in Objective-C
View NSArray+Filter.m
#import "NSArray+Filter.h"
@implementation NSArray (Filter)
- (NSArray *) filteredArrayUsingBlock:(BOOL (^)(id obj))block {
NSIndexSet *const filteredIndexes = [self indexesOfObjectsPassingTest:^BOOL (id _Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
return block(obj);
return [self objectsAtIndexes:filteredIndexes];
drosenstark / Permutations.swift
Created Apr 26, 2019
String permutations in a way that makes some sense
View Permutations.swift
import Foundation
func permute(_ s: String) -> [String] {
var arr = Array(s)
var permutations = [[Character]]()
permute(&arr, 0, &permutations)
return { String($0) }
func permute<T>(_ arr: inout [T], _ k: Int, _ result: inout [[T]]) {
drosenstark /
Created Apr 21, 2019
Watch and Swift file and run it on change
# swiftWatchAndRun
if [ $# -ne 1 ]; then
echo "Use like this:"
echo " $0 filename-to-watch"
exit 1
if which fswatch >/dev/null; then
echo "Watching swift file $1"
while true; do fswatch --one-event $1 >/dev/null && echo "----------------"; echo `date +"%m-%d-%y %I:%M%p"`; echo "----------------" && swift $1; sleep 0.1; done
drosenstark /
Last active Apr 17, 2019
Paste the clipboard in to your shell for realz
pbpaste > /tmp/
echo "Execute this?"
cat /tmp/
read -p "Press enter to run it..."
bash /tmp/
drosenstark / xctest-in-repl.swift
Created Mar 17, 2019
Run XCTest stuff using the Swift command line on macOS
View xctest-in-repl.swift
/// Run with:
/// swift -F/Applications/ xctest-in-repl.swift
import Foundation
import XCTest
class ATest : XCTestCase {
func testIt() {
print("Oh yeah, it's on")
XCTAssertTrue(false, "That needed to be true!")
drosenstark / WeakArray.swift
Created Nov 18, 2018
Hold protocolized objects weakly (with just a few, minor changes)
View WeakArray.swift
@objc public protocol Equalable: class {
@objc func isEqual(_ object: Any?) -> Bool
/// Store AnyObject subclasses weakly
/// * Note: if you wish to use a protocol, it must:
/// - be marked with `@objc`
/// - have all methods marked with `@objc`
/// - refine Equalable
public struct WeakArray<Element: Equalable> {
drosenstark / mermaid-to-diagrams.rb
Last active May 21, 2018
Convert all Mermaid Markdown files (mermaid-*.md) to jpg with white background.
View mermaid-to-diagrams.rb
#!/usr/bin/env ruby
def convert_mermaid_md_to_jpg
Dir["mermaid-*.md"].each do |f|
slug = f["mermaid-".length...f.length-".md".length]
command = "~/node_modules/.bin/mmdc -i mermaid-#{slug}.md -o mermaid-#{slug}.png -b '#FFFFFF'"
puts "Converting mermaid-#{slug}.md to mermaid-#{slug}.png"
drosenstark / delete-all-ios-simulators.rb
Created Apr 17, 2018
Deletes all your iOS Simulators
View delete-all-ios-simulators.rb
#!/usr/bin/env ruby
`xcrun simctl shutdown all`
device_lines = `xcrun simctl list`.split("\n")
device_lines.each do |line|
matches = /[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}/.match(line)
if matches
`xcrun simctl delete #{matches}`
puts "Deleting sim with ID #{matches}"
drosenstark / git-rbranch
Last active Feb 8, 2018
Creates a local branch and pushes it up
View git-rbranch
git checkout -b $1 && git push --set-upstream origin $1