Skip to content

Instantly share code, notes, and snippets.

View kristopherjohnson's full-sized avatar
💭
Huh?

Kristopher Johnson kristopherjohnson

💭
Huh?
View GitHub Profile
@kristopherjohnson
kristopherjohnson / IsNotNilOrEmptyValueTransformer.swift
Created February 8, 2016 22:21
Value transformer that returns true for strings that are non-empty
import Foundation
/// Given a `String?`, return an `NSNumber` Boolean value that is true if the value is non-nil and not empty.
///
/// This is useful for binding the `enabled` property of a button to a string value that is bound to a text field.
final class IsNotNilOrEmptyValueTransformer: NSValueTransformer {
override class func transformedValueClass() -> AnyClass {
return NSNumber.self
@kristopherjohnson
kristopherjohnson / blink.fs
Last active January 23, 2016 16:00
GForth interface for the wiringPi library
@kristopherjohnson
kristopherjohnson / Makefile
Last active January 23, 2016 10:16
Raspberry Pi/WiringPi simple example
LDLIBS=-lwiringPi
blink: blink.c
run: blink
sudo ./blink
.PHONY: run
clean:
$(RM) blink
@kristopherjohnson
kristopherjohnson / testTaskOutputPipe.swift
Created January 12, 2016 23:00
Demonstrate launching an `NSTask` and asynchronously reading its output.
import Cocoa
/// Demonstrate launching an `NSTask` and asynchronously reading its output.
///
/// Credit: <http://stackoverflow.com/a/23938137/1175>
func testTaskOutputPipe() {
let task = NSTask()
task.launchPath = "/bin/ls"
task.arguments = ["-l", "/Applications"]
@kristopherjohnson
kristopherjohnson / primeFactorsOf.swift
Last active January 2, 2016 18:16
Find prime factors of a number
import Foundation
/// Given a positive integer, find its prime factors.
///
/// - parameter number: The number to be factored.
///
/// - returns: Array of factors.
///
/// - TODO: Use a less-naive algorithm. Construct the result lazily.
@kristopherjohnson
kristopherjohnson / String_fromPathComponents.swift
Last active December 30, 2015 21:31
Swift String extension for generating filesystem paths from components
import Foundation
extension String {
/// Returns a new string made by appending to the receiver a filesystem path component.
///
/// - parameter pathComponent: The path component to be appended to the receiver.
///
/// - returns: A new `String` made by appending `pathComponent` to the receiver, preceded if necessary by a path separator.
@kristopherjohnson
kristopherjohnson / matchingFilesForPattern.swift
Created December 22, 2015 22:13
Swift wrapper for glob(3)
import Foundation
/// Given a wildcard pattern, return matching paths.
///
/// - parameter pattern: pathname pattern to be expanded. See docs for `glob(3)`.
/// - returns: array of pathnames that match the pattern
func matchingFilesForPattern(pattern: String) -> [String] {
var result = Array<String>()
var g = glob_t()
@kristopherjohnson
kristopherjohnson / turnstile.swift
Last active March 23, 2016 10:00
Swift implementation of Robert C. Martin's turnstile state-machine example
// Implementation of Robert C. Martin's turnstile state-machine example
/// Defines the "events" that can be invoked on a turnstile-like object.
protocol TurnstileEventHandler {
func onCoin()
func onPass()
}
/// Defines the "actions" that a turnstile can take due to an event or state transition.
protocol TurnstileActionDelegate: class {
@kristopherjohnson
kristopherjohnson / get-info-dictionary.swift
Last active December 17, 2015 20:12
Swift script to retrieve the info.plist dictionary for an executable
#!/usr/bin/swift
// Gets the info.plist data for executables specified on command line
import Foundation
if Process.argc < 2 {
print("usage: get-info-dictionary EXECUTABLE...")
}
else {
@kristopherjohnson
kristopherjohnson / MyInitiallyPositionedWindowController.swift
Created December 8, 2015 16:48
Snippet to programmatically set initial position of an Cocoa window
import Cocoa
class MyInitiallyPositionedWindowController: NSWindowController {
override func windowDidLoad() {
super.windowDidLoad()
if let window = window, screen = window.screen {
let screenRect = screen.visibleFrame
let offsetFromLeft = CGFloat(200)