Skip to content

Instantly share code, notes, and snippets.

View marcprux's full-sized avatar
🔆

Marc Prud'hommeaux marcprux

🔆
  • New England
  • 22:09 (UTC -04:00)
View GitHub Profile
@marcprux
marcprux / XCTAssertEqualOptional.swift
Created July 23, 2014 17:48
XCTAssertEqual that handles optional unwrapping
/// like XCTAssertEqual, but handles optional unwrapping
public func XCTAssertEqualOptional<T: Any where T: Equatable>(expression1: @auto_closure () -> T?, expression2: @auto_closure () -> T?, _ message: String? = nil, file: String = __FILE__, line: UInt = __LINE__) {
if let exp1 = expression1() {
if let exp2 = expression2() {
XCTAssertEqual(exp1, exp2, message ? message! : "", file: file, line: line)
} else {
XCTFail(message ? message! : "exp1 != nil, exp2 == nil", file: file, line: line)
}
} else if let exp2 = expression2() {
XCTFail(message ? message! : "exp1 == nil, exp2 != nil", file: file, line: line)
@marcprux
marcprux / nilter.swift
Created July 23, 2014 17:53
Create array of instances by unwrapping optionals
/// converts an array of optionals to an array of non-optionals by filtering out nil
public func nilter<T>(var array: Array<Optional<T>>) -> Array<T> {
return array.filter { $0.getLogicValue() }.map { $0! }
}
/// A calculation that holds a left term, a right term, and an operation
struct Calc<T> {
var lt: () -> T
var op: (T, T) -> T
var rt: () -> T
init(lt: () -> T, op: (T, T) -> T, rt: () -> T) {
self.lt = lt
self.op = op
self.rt = rt
@marcprux
marcprux / InversionOfExecution.swift
Last active August 29, 2015 14:16
Inversion of Execution in Swift
/// InversionOfExecution.swift – Marc Prud'hommeaux <[email protected]>
///
/// Demonstration of an *Inversion of Execution* (**IoE**) technique for how the same function
/// can be executed either synchronously with a return value or asynchronously with a
/// callback block depending on an optional trailing `iex` (inverted executor closure.
///
/// • When executed without a trailing closure, the result is provided as a return value:
///
/// `let result: Int = sum([1, 2])`
///
//
// Demonstration of using a channel to receive and dispatch IOHIDManager events using the ChannelZ framework.
//
// Created by Marc Prud'hommeaux on 3/8/15.
//
import Cocoa
import IOKit
import ChannelZ
@NSApplicationMain
@marcprux
marcprux / NSURL+Zip.swift
Created September 22, 2015 17:23
Categories on NSURL and NSData to create a zip archive without any external dependencies
public extension NSURL {
/// Creates a zip archive of the file/folder represented by this URL and returns a references to the zipped file
///
/// - parameter dest: the destination URL; if nil, the destination will be this URL with ".zip" appended
func zip(dest: NSURL? = nil) throws -> NSURL {
let destURL = dest ?? self.URLByAppendingPathExtension("zip")
let fm = NSFileManager.defaultManager()
var isDir: ObjCBool = false
@marcprux
marcprux / SHA1.swift
Created May 1, 2018 16:13
A single-file SHA1 hash function, modified from the CryptoSwift project
//
// SHA1.swift
// Glib
//
// Created by Marc Prud'hommeaux on 5/1/18.
// Copyright © 2018 Marc Prud'hommeaux. All rights reserved.
//
// A single-file conversion of the SHA1 code from: CryptoSwift
//
// Copyright (C) 2014-2017 Marcin Krzyżanowski <[email protected]>
@marcprux
marcprux / SFSymbols.swift
Created June 6, 2019 01:27
All the SFSymbol images as extensions of UIImage & NSImage
#if os(macOS)
import AppKit
public typealias UXImage = NSImage
#elseif os(iOS)
import UIKit
public typealias UXImage = UIImage
#endif
/// Extensions for system images included in SF Symbols
@available(macOS 10.15, iOS 13.0, *)
@marcprux
marcprux / ExpressibleByColorComponents.swift
Last active August 15, 2024 21:19
Color constants that can be declared in a single place and used for all of UIColor, NSColor, CIColor, CGColor, and SwiftUI.Color
/// A color instance that can be expressed by red, green, blue, and alpha components.
///
/// Conformed to by:
/// - `CoreGraphics.CGColor`
/// - `CoreImage.CIColor`
/// - `UIKit.UIColor`
/// - `AppKit.NSColor`
/// - `SwiftUI.Color`
public protocol ExpressibleByColorComponents {
/// Create an instance of this color from the given color components.
//
// Demonstration of how to use `anchorPreference` to mimic the floating selection
// behavior of `SegmentedPickerStyle`.
//
// Created by Marc Prud'hommeaux on 12/10/19.
//
import SwiftUI
public extension View {