This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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() | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
OlderNewer