Skip to content

Instantly share code, notes, and snippets.

// 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 {
//: 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
@algal
algal / SwiftLineReader.swift
Last active February 8, 2023 16:14
Read a file one line at a time in Swift
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)
/**
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>
{
@algal
algal / has-ancestrol-process.bash
Created April 26, 2016 16:56
Check if the current process has a given ancestor by name
#!/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
@algal
algal / OperationsComposers.swift
Created April 20, 2016 21:05
Experiment with generic NSOperation composition
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.
@algal
algal / NSURLSession+sendSynchronousRequest.swift
Last active April 18, 2016 17:30
NSURLSession extension to add synchronous data task request.
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>
@algal
algal / NSURLSession+DataTaskResult.swift
Created April 6, 2016 21:46
Type-wrapper for dataTaskWithRequest
// 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 {
@algal
algal / PKCS12.swift
Last active February 24, 2025 20:47
Reading PKCS12 with Swift in Foundation
// 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.
*/
@algal
algal / wallsleep.sh
Last active September 13, 2016 21:56
wallsleep -- like sleep, but uses wall time not process time
#!/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)