Skip to content

Instantly share code, notes, and snippets.

View chrisschreiner's full-sized avatar

Chris Schreiner chrisschreiner

  • Schpaencoder
  • Oslo, Norway, Europe
View GitHub Profile
// Option 1: if..elseif + optional unwrapping conditions
private func handleNotificationAction(id: String?, userInfo: [NSObject: AnyObject], responseInfo: [NSObject: AnyObject]?, completion: () -> Void) {
let taskId = userInfo["extra"]?["task_hash"] as? String
let projectId = userInfo["extra"]?["project_hash"] as? String
let comment = responseInfo?["UIUserNotificationActionResponseTypedTextKey"] as? String
if id == "comment", let task = taskId, comment = comment where !comment.isEmpty {
} else if id == "invitation_accept", let project = projectId {
@chrisschreiner
chrisschreiner / APOD.swift
Last active June 25, 2018 10:33
Astronomy Picture of the Day with ReactiveCocoa 4
// APOD.swift
// Test with ReactiveCocoa 4
//
// Created by Chris Patrick Schreiner on 17-11-2015.
import ReactiveCocoa
import Result
import XCPlayground
XCPlaygroundPage.currentPage.needsIndefiniteExecution = true
@mikeabdullah
mikeabdullah / MainViewController.m
Last active August 29, 2015 14:11
Encoding child view controllers in custom container
- (void)encodeRestorableStateWithCoder:(NSCoder *)coder {
[super encodeRestorableStateWithCoder:coder];
[coder encodeObject:self.contentViewController
forKey:@"contentViewController"];
}
@mikeabdullah
mikeabdullah / gist:bdd5e30ad856fa79ee87
Created November 21, 2014 19:10
Querying if root view controller is being dismissed
- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
UIViewController *root = self.presentingViewController.presentedViewController;
if (root.isBeingDismissed) {
[self.view endEditing:YES];
}
}
@rnapier
rnapier / FlatMapProof.swift
Created September 7, 2014 13:46
Proof: flatMap<Result> and continueWith are the the same function
// Claim: flatMap<Result> and continueWith are the the same function
//
// Begin with flatMap and its dependencies
//
func flatMap<T,U>(x: Result<T>, f: T -> Result<U>) -> Result<U> {
return flatten(map(x, f))
}
func map<T, U>(x: Result<T>, f: T -> U) -> Result<U> {
Note that like all analogies, this is not a perfect one. I find it helps me think about it though. Also, don't try to draw conclusions about performance from this analogy :)
-[NSUserDefaults setObject:forKey:] is like...
<edit file to add key and value>
git add file
git commit
dispatch_after(some time, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[[NSUserDefaults standardUserDefaults] synchronize]
});
@chrisschreiner
chrisschreiner / color-utils.clj
Created July 11, 2010 12:00
Color utilities for CSS
(import 'java.awt.Color)
(def *default-color-fraction* 0.2)
(def *default-blend-fraction* 0.5)
(defn- format-hex [c]
(str "#" (format "%02x%02x%02x"
(.getRed c)
(.getGreen c)
@michalmarczyk
michalmarczyk / merge-seqs.clj
Created June 26, 2010 19:48
merge seqs of Comparable objects (possibly removing duplicates)
(defn merge-seqs
"Merges sorted seqs of Comparable objects as in merge sort. Uses
left-to-right precedence order among the input seqs when duplicates
are present. Uses clojure.core/compare."
([xs ys]
(lazy-seq
(if (or (empty? xs) (empty? ys))
(concat xs ys)
(let [x (first xs)
y (first ys)]