Skip to content

Instantly share code, notes, and snippets.

View jtbandes's full-sized avatar
🗾
Traveling

Jacob Bandes-Storch jtbandes

🗾
Traveling
View GitHub Profile
//import Foundation
//class P: NSObject {}
protocol P {}
class C: P {}
extension SequenceType
{
func mapAs<T>(type: T.Type) -> [T] {
return map { $0 as! T }
@jtbandes
jtbandes / wtf.cpp
Last active August 29, 2015 14:27
a vtable gotcha
class Base1 {
public:
virtual void Foo() = 0;
};
class Base2 {
public:
virtual void Bar() = 0;
};
@jtbandes
jtbandes / neat.cpp
Last active August 29, 2015 14:27
fun things made possible by C++
// Suppose we want to convert argv/argc to a std::vector<std::string> for easier use.
// There are surprisingly many (and surprisingly concise) options:
// First pass:
vector<string> args;
for (int i = 1; i < argc; i++) {
args.push_back(string(argv[i]));
}
// Minor improvement using emplace, which forwards the char* param directly to a string constructor:
// Would you rather use this:
NSLayoutConstraint(item: label, attribute: .Leading, relatedBy: .Equal, toItem: button, attribute: .Trailing, multiplier: 1, constant: 0).active = true
// or this?
label.constrain(.Leading, .Equal, to: button, .Trailing, plus: 20)

Values of macros from TargetConditionals.h.

Xcode 7 / iOS 9.1, tvOS 9.0, watchOS 2.0, OS X 10.11 SDKs

Macro 🖥 📱 📱sim ⌚️ ⌚️sim 📺 📺sim
TARGET_OS_MAC 1 1 1 1 1 1 1
TARGET_OS_IPHONE 0 1 1 1 1 1 1
TARGET_OS_IOS 0 1 1 0 0 0 0
TARGET_OS_WATCH 0 0 0 1 1 0 0
@jtbandes
jtbandes / background.js
Created November 22, 2015 21:29
Be Productive — a barebones Chrome extension
var urlsToBlock = [
// matches manifest.json
"*://*.reddit.com/*"
];
function handleRequest(details) {
return {cancel: true};
}
chrome.webRequest.onBeforeRequest.addListener(handleRequest, {"urls": urlsToBlock}, ["blocking"]);
@jtbandes
jtbandes / scary.hs
Last active December 30, 2015 09:18
import Control.Arrow ( (&&&) )
import qualified Control.Arrow ( first, second )
import Data.Maybe ( fromMaybe )
-- this also exists in Data.Graph.Inductive.Query.Monad
(><) :: (a -> c) -> (b -> d) -> (a, b) -> (c, d)
(><) f g = Control.Arrow.first f . Control.Arrow.second g
first :: [a] -> Maybe a
first [] = Nothing
@jtbandes
jtbandes / decode-dyn-uti.swift
Last active March 11, 2025 10:13
Dynamic UTI decoding
/// Decodes a dynamically-generated Uniform Type Identifier for inspection purposes. (**NOT FOR PRODUCTION USE!**)
/// Many, many thanks to http://alastairs-place.net/blog/2012/06/06/utis-are-better-than-you-think-and-heres-why/
///
/// <https://developer.apple.com/library/ios/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_conc/understand_utis_conc.html#//apple_ref/doc/uid/TP40001319-CH202-BCGCDHIJ>
func decodeDynUTI(_ uti: String) -> String?
{
let vec = Array("abcdefghkmnpqrstuvwxyz0123456789")
let encoded = Array(uti).suffix(from: 5)
var result: [UInt8] = []
@jtbandes
jtbandes / bool?.md
Last active May 7, 2017 21:19
Bool? "truth" table
x: Bool? false x==nil x==false x!=true x==true, x??false x!=false, x??true x!=nil true
true? false false false false true true true true
false? false false true true false false true true
nil false true false true false true false true
extension NSTimer
{
private class Trampoline
{
typealias Handler = @convention(block) (NSTimer) -> Void
@objc static func handleTimer(timer: NSTimer) {
let handler = unsafeBitCast(timer.userInfo, Handler.self)
handler(timer)
}