Skip to content

Instantly share code, notes, and snippets.

Rob Napier rnapier

Block or report user

Report or block rnapier

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 resource-leak-or-not-mac-os-x.cpp
// Example for http://stackoverflow.com/questions/21699184/resource-leak-or-not-mac-os-x
#import <CoreServices/CoreServices.h>
#import <SystemConfiguration/SCDynamicStore.h>
#import <SystemConfiguration/SCDynamicStoreCopySpecific.h>
#include <iostream>
#include <thread>
void Execute()
{
View underscore.m
// With dot syntax (and standard Xcode indentation, just hit "Cmd-I")
NSArray *tweets = Underscore.array(results)
.filter(Underscore.isDictionary)
.reject(^BOOL (NSDictionary *tweet) {
return [tweet[@"iso_language_code"] isEqualToString:@"en"];
})
.map(^NSString *(NSDictionary *tweet) {
NSString *name = tweet[@"from_user_name"];
NSString *text = tweet[@"text"];
View dispatch-sync-safe.swift
func dispatch_sync<R> (queue: dispatch_queue_t, block: Void -> R ) -> R {
var result: R!
dispatch_sync(queue) {
result = block()
}
return result
}
func result() -> String {
return
@rnapier
rnapier / gist:478465d1b15e95b98b42
Last active Aug 29, 2015
Spooky action at a distance when you don't use self
View gist:478465d1b15e95b98b42
// Try this in a commandline app vs a playground. The behaviors are different.
class MyClass {
func doSomething() {
refresh() // I wonder which refresh we'll get?
}
}
// Not only does this change the behavior of doSomething()... (see below)
// extension MyClass {
View gist:411a710ebb725677b25d
func ==<A:Equatable, B:Equatable>(lhs:(A,B), rhs:(A,B)) -> Bool {
return lhs.0 == rhs.0
&& lhs.1 == rhs.1
}
func ==<A:Equatable, B:Equatable, C:Equatable>(lhs:(A,B,C), rhs:(A,B,C)) -> Bool {
return lhs.0 == rhs.0
&& lhs.1 == rhs.1
&& lhs.2 == rhs.2
}
@rnapier
rnapier / gist:0d24ca74e4b4c5bfb3b4
Last active Aug 29, 2015
withExtendedLifetimes (arbitrary number of parameters)
View gist:0d24ca74e4b4c5bfb3b4
// You call this as:
// withExtendedLifetimes([x,y,z]) { ... }
// I haven't been able to replace the array with variadic. The function
// will be absorbed into the list (even if it's curried).
func withExtendedLifetimes(args: [Any], f: () -> ()) {
var rest = args
let last:Any = rest.removeLast()
if rest.count > 0 {
Swift.withExtendedLifetime(last) { () -> () in withExtendedLifetimes(rest, f) }
}
@rnapier
rnapier / gist:e757aed05afc1811a6cc
Last active Aug 29, 2015
Tail-recursive reverse
View gist:e757aed05afc1811a6cc
func reverseR<T>(result: [T], curList: Slice<T>) -> [T] {
switch curList.first {
case .None: return result
case .Some(let cur): return reverseR([cur] + result, dropFirst(curList))
}
}
func reverseTailRecursive<T>(ls: [T]) -> [T] {
return reverseR([], Slice(ls))
}
@rnapier
rnapier / gist:8eda179689d9d61c2bfb
Last active Aug 29, 2015
And autoclosure saves(?) the day for generic recursive enums
View gist:8eda179689d9d61c2bfb
// Creating a generic recursive data structure with autoclosure. (READ ALL NOTES; THIS MAY NOT DO WHAT YOU WANT.)
// Closures are reference types, so the size is known (? I think ?)
// Note that this is just because of unimplemented features in the compiler in Beta5
// There's no reason to think this is a long-term requirement.
// IMPORTANT: the closure will run every time you access this value, so if that has
// side effects, this won't work. It's only possible on pure value types.
// But the fact that this works as expected is actually kind of incredible.
// Think about what is required for it to work out the type for NestedList.Elem("first").
@rnapier
rnapier / gist:2c2bccc40b24fb9d54fc
Last active Aug 29, 2015
Version 2 of Functional Wish Fulfillment
View gist:2c2bccc40b24fb9d54fc
//
// Version 2 of pagesFromData from Functional Wish Fulfillment
// http://robnapier.net/functional-wish-fulfillment
//
import Foundation
func pagesFromData(data: NSData) -> Result<[Page]> {
// 1. Parse the NSData into a JSON object
@rnapier
rnapier / gist:0f5611d0bf89a9645713
Last active Aug 29, 2015
Version 3 of Functional Wish Fulfillment
View gist:0f5611d0bf89a9645713
//
// Version 3 of pagesFromData from Functional Wish Fulfillment
// http://robnapier.net/functional-wish-fulfillment
//
import Foundation
func pagesFromData(data: NSData) -> Result<[Page]> {
return continueWith(asJSON(data)) {
continueWith(asJSONArray($0)) {
You can’t perform that action at this time.