Skip to content

Instantly share code, notes, and snippets.

View alexito4's full-sized avatar

Alejandro Martínez alexito4

View GitHub Profile
@jspahrsummers
jspahrsummers / gist:00d1ad24afbe26cd7abe
Last active August 29, 2015 14:07
Scenarios to consider for FRP APIs
  Hot Cold Notes
Behaviors Always Property values over time
Events Maybe Maybe This is a design decision, but picking “hot” requires introducing some concept of buffering
Promises Sorta Sorta Promises don't do work until started, but only do it once, and share/buffer their result
@mattbischoff
mattbischoff / LCKIndexedFetchedResultsController.h
Last active August 29, 2015 14:10
LCKIndexedFetchedResultsController is a NSFetchedResultsController subclass that attempts to solve the problem of sorting a fetched collection into correctly alphabetized sections for every locale. It does things like making sure that the `#` character shows up at the bottom instead of the top of the section index titles.
//
// LCKIndexedFetchedResultsController.h
// Quotebook
//
// Created by Andrew Harrison on 7/26/14.
// Copyright (c) 2014 Lickability. All rights reserved.
//
@import CoreData;
//: Playground - noun: a place where people can play
// Got most of it from:
// https://developer.apple.com/sample-code/wwdc/2015/downloads/Crustacean.zip
// This is in the Protocol-Oriented Programming in Swift talk
// Pay special attention to the Heterogeneous Equality section, on page 2.
//
// Most comments directly copy/pasted from Crustacean Playground
protocol Account {
@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;
}
@irace
irace / Optional+Empty.swift
Last active June 16, 2016 16:17
`UITextView` should really just have this by default.
protocol TextContaining {
var isEmpty: Bool { get }
}
extension String: TextContaining {
}
extension Optional where Wrapped: TextContaining {
var isEmpty: Bool {
switch self {
@janodev
janodev / MySingleton.h
Last active July 28, 2016 03:19
Singleton pattern with __atribute__ unavailable to prevent the accidental misuse of a singleton. Note that the user is still able to create more instances using runtime functions.
#import <Foundation/Foundation.h>
@interface MySingleton : NSObject
+(instancetype) sharedInstance;
// clue for improper use (produces compile time error)
+(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));
-(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
+(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));
@dabrahams
dabrahams / pie.swift
Last active December 27, 2016 18:07
Make defining a collection as easy as pie
//===----------------------------------------------------------------------===//
//===--- Defining Collections, easy as Pie --------------------------------===//
//===----------------------------------------------------------------------===//
struct Digits : PieCollection {
let startState: Optional = 0
func iterate(from state: Int) -> (nextState: Int?, element: Int) {
return (nextState: state+1 < 10 ? state+1 : nil, element: state)
}
}
@irace
irace / CenteringView.swift
Last active November 29, 2017 19:31
I’m building a complex new app entirely with programmatic Auto Layout. It only supports iOS 9 so that means `UIStackView` and `NSLayoutAnchor` exclusively. These two classes have been very handy thus far, in the spirit of composition over inheritance.
final class CenteringView: UIView {
// MARK: - Initialization
init(contentView: UIView) {
super.init(frame: .zero)
addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activateConstraints([
import Foundation
import JavaScriptCore
/// Used to lookup our Bundle.
private class MomentBundleClass: NSObject {}
/// A wrapper around a moment.js object.
public struct Moment {
@chriseidhof
chriseidhof / TypedExpr.swift
Last active February 3, 2018 23:01
Typed Expressions in Swift
// Variables just contain an integer. We can have a maximum of `Int.max` variables in our program. ¯\_(ツ)_/¯
private struct Var {
static var freshVarIx = 0
let ix: Int
init() {
Var.freshVarIx+=1
ix = Var.freshVarIx
}
}