Skip to content

Instantly share code, notes, and snippets.

View drosenstark's full-sized avatar

Dan Rosenstark drosenstark

View GitHub Profile
@drosenstark
drosenstark / DispatchQueue+Util.swift
Created February 21, 2022 21:59
Extension for easy testing of async code
// 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
drosenstark / NSArray+Filter.m
Created January 18, 2021 02:31
Simple block filter on NSArray in Objective-C
#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];
// ==UserScript==
// @name Workflowy Images and iFrames
// use image:http://example.com/image.png and iframe:http://example.com/somePlace.html in your note text for Workflowy
// @namespace http://confusionstudios.com
// @version 1.1
// @author Dan Rosenstark (DR2050)
// @match https://workflowy.com/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
// @require https://code.jquery.com/ui/1.12.0/jquery-ui.js
// @require https://cdn.rawgit.com/noelboss/featherlight/1.5.0/release/featherlight.min.js
@drosenstark
drosenstark / workflowyPlusRefac.js
Last active November 15, 2020 14:45
Userscript to display images and iframes in Workflowy
// ==UserScript==
// @name WorkflowyPlus Refac
// @namespace http://confusionstudios.com
// @version 0.2
// @author Wizmann, DR2050
// @match https://workflowy.com/*
// @grant none
// ==/UserScript==
/* jshint -W097 */
'use strict';
@drosenstark
drosenstark / workflowyPlusRefac2.js
Last active May 6, 2020 19:06
Userscript to lazily display images and iframes in Workflowy
// ==UserScript==
// @name WorkflowyPlus Refac 2
// @namespace http://confusionstudios.com
// @version 0.2
// @author Wizmann, DR2050
// @match https://workflowy.com/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js
// @grant none
// ==/UserScript==
/* jshint -W097 */
@drosenstark
drosenstark / GameLoop.swift
Created February 17, 2015 20:20
Gameloop in Swift
import UIKit
class GameLoop : NSObject {
var doSomething: () -> ()!
var displayLink : CADisplayLink!
var frameInterval : Int!
init(frameInterval: Int, doSomething: () -> ()) {
self.doSomething = doSomething
@drosenstark
drosenstark / Permutations.swift
Created April 26, 2019 12:26
String permutations in a way that makes some sense
import Foundation
func permute(_ s: String) -> [String] {
var arr = Array(s)
var permutations = [[Character]]()
permute(&arr, 0, &permutations)
return permutations.map { String($0) }
}
func permute<T>(_ arr: inout [T], _ k: Int, _ result: inout [[T]]) {
@drosenstark
drosenstark / swiftWatchAndRun.sh
Created April 21, 2019 22:07
Watch and Swift file and run it on change
#!/bin/sh
# swiftWatchAndRun
if [ $# -ne 1 ]; then
echo "Use like this:"
echo " $0 filename-to-watch"
exit 1
fi
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
drosenstark / paste_from_shell.sh
Last active April 17, 2019 20:35
Paste the clipboard in to your shell for realz
#!/bin/bash
pbpaste > /tmp/shellpaste.sh
echo "Execute this?"
cat /tmp/shellpaste.sh
echo
read -p "Press enter to run it..."
bash /tmp/shellpaste.sh
@drosenstark
drosenstark / Donut.swift
Last active April 6, 2019 11:59
(thanks to Paintcode) Draw a hollow circle in swift, or just a piece of one... angle starts at bottom and rotates around clockwise.
class func drawHollowCircle(circleColor: UIColor, rect: CGRect, startAngle: CGFloat = 1, endAngle: CGFloat = 360, outerRingProportion: CGFloat = 1, innerRingProportion: CGFloat = 0.5, drawShadow : Bool) {
//// Variable Declarations
let startAngleCalced: CGFloat = -startAngle + 270
let endAngleCalced: CGFloat = -endAngle + 270
let innerRingDiameter: CGFloat = rect.size.width * innerRingProportion
let innerRingOffset: CGFloat = 0.5 * (rect.size.width - innerRingDiameter)
let innerRingRect = CGRectMake(innerRingOffset, innerRingOffset, innerRingDiameter, innerRingDiameter)
let outerRingDiameter: CGFloat = rect.size.width * outerRingProportion
let outerRingOffset: CGFloat = 0.5 * (rect.size.width - outerRingDiameter)