Skip to content

Instantly share code, notes, and snippets.

View jtbandes's full-sized avatar

Jacob Bandes-Storch jtbandes

View GitHub Profile
#include <iostream>
using namespace std;
template<int N, char C, char... Cs> struct repeatImpl : repeatImpl<N-1, C, C, Cs...> {};
template<char C, char... Cs> struct repeatImpl<0, C, Cs...> {
static constexpr char cs[] = {Cs..., '\0'};
};
template<char C, char... Cs> constexpr char repeatImpl<0, C, Cs...>::cs[];
template<int N, char C> const char (& repeat())[N+1] {
return repeatImpl<N, C>::cs;
#include <iostream>
#include <regex>
template<typename StringT>
struct regex_searcher_t {
using IterT = decltype(std::begin(std::declval<const StringT&>()));
// using CharT = typename std::decay<decltype(*std::declval<IterT>())>::type;
using CharT = typename std::iterator_traits<IterT>::value_type;
func sequence<T>(_ initial: T, _ cond: (T) -> Bool, _ update: (inout T) -> ()) -> UnfoldSequence<T, T> {
return sequence(state: initial, next: { (state: inout T) -> T? in
guard cond(state) else { return nil }
update(&state)
return state
})
}
for x in sequence(4, { $0 < 10 }, { $0 = $0 + 1 }) {
print(x)
protocol P {
var dynamicTypeAsMember: P.Type { get }
}
struct T: P {}
extension P {
var dynamicTypeAsMember: P.Type {
return self.dynamicType
}
}
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)
}
@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
@jtbandes
jtbandes / decode-dyn-uti.swift
Last active June 14, 2025 04:55
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 / 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 / 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"]);