Skip to content

Instantly share code, notes, and snippets.

Jacob Bandes-Storch jtbandes

Block or report user

Report or block jtbandes

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View analysis.py
# Inspired by: https://www.futilitycloset.com/2017/04/17/a-poets-arsenal/
# Word lists from the Opinion Lexicon: https://www.cs.uic.edu/~liub/FBS/sentiment-analysis.html
from itertools import product, imap, ifilter, chain
import multiprocessing
def load_words(path):
with open(path) as f:
words = ifilter(lambda line: line.rstrip() and not line.startswith(";"), f)
return map(lambda word: word.rstrip(), words)
View StringUtils.swift
extension String {
func lazyComponents(separatedBy sep: Character) -> AnySequence<String> {
return AnySequence<String> { [cv = self.characters] () -> AnyIterator<String> in
var current = cv.startIndex
var finished = false
return AnyIterator<String> {
if finished {
return nil
}
View repeat.cpp
#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;
View swift-crashers.md
View regex_searcher.cpp
#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;
View c-style-for.swift
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)
View dynamictype.swift
protocol P {
var dynamicTypeAsMember: P.Type { get }
}
struct T: P {}
extension P {
var dynamicTypeAsMember: P.Type {
return self.dynamicType
}
}
View NSTimer+Util.swift
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
Bool? "truth" table
View bool?.md
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 May 27, 2019
Dynamic UTI decoding
View decode-dyn-uti.swift
extension String
{
/// Creates a string by decoding a sequence of UTF-8 code units.
init?<S: Sequence>(utf8 input: S) where S.Iterator.Element == UTF8.CodeUnit {
var chars: [Character] = []
var decoder = UTF8()
var iter = input.makeIterator()
loop: while true {
switch decoder.decode(&iter) {
case .scalarValue(let scalar): chars.append(Character(scalar))
You can’t perform that action at this time.