This file contains hidden or 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
// known-good: Swift 2.2, Xcode 7.3 | |
struct Regex { | |
let matcher:NSRegularExpression | |
init?(pattern: String, options: NSRegularExpressionOptions = []) { | |
guard let m = try? NSRegularExpression(pattern: pattern, options: options) else { return nil } | |
self.matcher = m | |
} | |
func match(string: String, options: NSMatchingOptions = []) -> Bool { |
This file contains hidden or 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
//: Building thread-safe value types with GCD | |
/** | |
This playground walks through an example trying to create a thread-safe counter type, | |
and noting the difficult of doing this while maintaining value semantics. | |
An incrementer is not interesting in itself, but it is the simplest example illustrating problems which also appear for more useful types, like dictionaries or arrays. | |
//: Reference-type solution |
This file contains hidden or 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
import Darwin | |
/** | |
Returns a lazy sequence that returns a String for every line of `file`. | |
Example 1: | |
// print all lines from a file | |
let file = fopen(NSBundle.mainBundle().pathForResource("Cartfile", ofType: nil)!,"r") | |
for line in lineGenerator(file) { print(line,separator: "", terminator: "") } | |
fclose(file) |
This file contains hidden or 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
/** | |
Gets and sets to this dictionary-like type are atomic. | |
Does not fully implement correct value semantics for copy | |
operations, since the contained queue is a reference type | |
and will be shared among copies of this object | |
*/ | |
struct AtomicDictionary<KeyType:Hashable,ValueType> | |
{ |
This file contains hidden or 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
#!/bin/bash | |
# works on plain vanillla OS X 10.11.4 without installing pstree, | |
# which is of course the sane way to do this | |
if [ "$#" -ne 1 ]; then | |
echo "usage: $0 key" 1>&2 | |
echo "$0 finishes with an exit code of 0 if key was found in ancestral process; 1 if not; 2, if it is showing this usage message" 1>&2 | |
exit 2 | |
fi |
This file contains hidden or 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
protocol FunctionOperation : class { | |
associatedtype InputType | |
associatedtype OutputType | |
var input:InputType? { get set } | |
var output:OutputType? { get set } | |
func addDependency(op: NSOperation) | |
} | |
/** | |
Returns an _adapter block_ which passes the output from `f` into the input of `g`, and builds dependencies so f runs first, then the adapter, then g. |
This file contains hidden or 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
import Foundation | |
/** | |
NSURLSession extension to add synchronous data task request, throwing on | |
timeout. | |
Using a GCD semaphore internally. | |
Deeper wisdom from Quinn the Eskimo on why you may not want this: | |
<https://forums.developer.apple.com/thread/11519> |
This file contains hidden or 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
// known-good: Xcode 7.3, Swift 2.2 | |
import Foundation | |
public enum DataTaskResult { | |
case Success(data:NSData,response:NSURLResponse?) | |
case Error(error:NSError,response:NSURLResponse?) | |
} | |
extension NSURLSession { |
This file contains hidden or 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
// xcode 7.3 | |
import Foundation | |
/** | |
Struct representing values returned by `SecPKCS12Import` from the Security framework. | |
This is what Cocoa and CocoaTouch can tell you about a PKCS12 file. | |
*/ |
This file contains hidden or 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
#!/bin/sh | |
# sleeps for $1 seconds based on wall time, not CPU time | |
set -e | |
# known-good with: | |
# - GNU date (Ubuntu 14.04.3 LTS) | |
# - Darwin date (OS X 10.11.4) |