Skip to content

Instantly share code, notes, and snippets.

View rnapier's full-sized avatar

Rob Napier rnapier

View GitHub Profile
// 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()
{
// 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"];
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 August 29, 2015 14:04
Spooky action at a distance when you don't use self
// 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 {
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 August 29, 2015 14:04
withExtendedLifetimes (arbitrary number of parameters)
// 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 August 29, 2015 14:04
Tail-recursive reverse
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 August 29, 2015 14:04
And autoclosure saves(?) the day for generic recursive enums
// 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 August 29, 2015 14:05
Version 2 of Functional Wish Fulfillment
//
// 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:9e8f92a1ce6be5c3d295
Created August 18, 2014 18:28
Version 1 of Functional Wish Fulfillment
//
// Version 1 of pagesFromData from Functional Wish Fulfillment
// http://robnapier.net/functional-wish-fulfillment
//
import Foundation
func pagesFromData(data: NSData) -> PageListResult {
// 1. Parse the NSData into a JSON object