Skip to content

Instantly share code, notes, and snippets.

/// |                    World                 |
/// |------------------------------------------|
/// | Module A | Module B | Module C | Module D|
  1. World is a module
  2. World is aware of all modules.
  3. Modules aren't aware of World.
#!/usr/bin/swift
import AppKit
// MARK: - Helpers
@inline(__always) func error(_ message: String) {
print("💥 \(message)")
}
@inline(__always) func success(_ message: String) {
@JadenGeller
JadenGeller / Cut.swift
Last active January 22, 2017 20:47
Cut a sequence given a predicate taking in successive elements
extension Collection where SubSequence: Sequence, SubSequence.Iterator.Element == Iterator.Element {
func cut(atSuccession shouldCut: (Iterator.Element, Iterator.Element) throws -> Bool) rethrows -> [Self.SubSequence] {
var (fromIndex, toIndex) = (startIndex, startIndex)
var result: [SubSequence] = []
for (x, y) in zip(self, dropFirst()) {
defer { toIndex = index(after: toIndex) }
guard try shouldCut(x, y) else { continue }
result.append(self[fromIndex...toIndex])
fromIndex = index(after: toIndex)
}
@jspahrsummers
jspahrsummers / bad.m
Last active January 20, 2021 11:55
Synchronizing with multiple GCD queues
//
// DON'T do this, or else you risk a deadlock (e.g., by accidentally performing it in a different order somewhere)
//
dispatch_async(firstQueue, ^{
dispatch_sync(secondQueue, ^{
// code requiring both queues
});
});
@portellaa
portellaa / Default (OSX).sublime-keymap
Last active March 26, 2024 09:06 — forked from lmdemanuel/Default (OSX).sublime-keymap
Sublime Text 3 - User keybindings "a la Xcode"
[
{ "keys": ["super+0"], "command": "toggle_side_bar" },
{ "keys": ["super+shift+o"], "command": "show_overlay", "args": {"overlay": "goto", "show_files": true} },
{ "keys": ["super+shift+y"], "command": "show_panel", "args": {"panel": "console", "toggle": true} },
{
"keys": ["super+enter"],
"command": "set_layout",
"args":
{
"cols": [0.0, 1.0],
@steipete
steipete / NSData+PSPDFFoundation.m
Created July 16, 2015 08:08
After playing around with dispatch_io (https://gist.github.com/steipete/b22babbf3014e29c19f0), I ended up with this. Upside: Uses way less memory, controllable caching, similar performance, simpler code and supports priority donation implicitly since everything's sync.
static NSData *PSPDFCalculateSHA256FromFileURL(NSURL *fileURL, CC_LONG dataLength, NSError **error) {
NSCParameterAssert(fileURL);
NSData *shaData;
int fd = open(fileURL.path.UTF8String, O_RDONLY);
if (fd < 0) {
if (error) *error = [NSError pspdf_errorWithCode:PSPDFErrorCodeUnableToOpenPDF description:@"Failed to open file for calculating SHA256."];
return nil;
}
@steipete
steipete / NSData+PSPDFFoundation.m
Last active March 21, 2017 16:00
Haven't done much with dispatch_io yet so I'd appreciate a few more eyeballs on this. Am I closing things correctly in all error conditions? Are there other knobs I could change to make things even faster? (I know I've been lazy on the NSError's)
static NSData *PSPDFCalculateSHA256FromFileURL(NSURL *fileURL, CC_LONG dataLength, NSError **error) {
NSCParameterAssert(fileURL);
dispatch_queue_t shaQueue = dispatch_queue_create("com.pspdfkit.sha256-queue", DISPATCH_QUEUE_SERIAL);
__block dispatch_io_t readChannel;
void (^processIntError)(int intError) = ^(int intError) {
if (intError != 0) {
PSPDFLogWarning(@"Stream error: %d", intError);
if (error) *error = [NSError errorWithDomain:@"SHA256Error" code:100 userInfo:@{NSLocalizedDescriptionKey: @"failed to open file for calculating SHA256."}];
@cabeca
cabeca / simulator_populator
Created September 23, 2014 21:30
This script removes and recreates all simulators in Xcode 6.
#!/usr/bin/env ruby
device_types_output = `xcrun simctl list devicetypes`
device_types = device_types_output.scan /(.*) \((.*)\)/
runtimes_output = `xcrun simctl list runtimes`
runtimes = runtimes_output.scan /(.*) \(.*\) \((com.apple[^)]+)\)$/
devices_output = `xcrun simctl list devices`
devices = devices_output.scan /\s\s\s\s(.*) \(([^)]+)\) (.*)/
@staltz
staltz / introrx.md
Last active May 3, 2024 13:00
The introduction to Reactive Programming you've been missing
@gimenete
gimenete / gist:53704124583b5df3b407
Last active July 31, 2020 16:20
Animated rootViewController transition
// put this in your AppDelegate
- (void)changeRootViewController:(UIViewController*)viewController {
if (!self.window.rootViewController) {
self.window.rootViewController = viewController;
return;
}
UIView *snapShot = [self.window snapshotViewAfterScreenUpdates:YES];
[viewController.view addSubview:snapShot];
self.window.rootViewController = viewController;