Skip to content

Instantly share code, notes, and snippets.

View marcprux's full-sized avatar
🔆

Marc Prud'hommeaux marcprux

🔆
  • New England
  • 03:21 (UTC -04:00)
View GitHub Profile
@marcprux
marcprux / AlignmentGuides.swift
Created February 28, 2020 17:57
Example of how to use `alignmentGuide` to align components along two dimensions
//
// ContentView.swift
// AlignDemo
//
// Created by Marc Prud'hommeaux on 2/28/20.
// Copyright © 2020 Glimpse I/O. All rights reserved.
//
import SwiftUI
//
// 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 {
@marcprux
marcprux / ExpressibleByColorComponents.swift
Last active November 17, 2019 20:58
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.
@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 / 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 <marcin@krzyzanowskim.com>
@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
//
// 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 / InversionOfExecution.swift
Last active August 29, 2015 14:16
Inversion of Execution in Swift
/// InversionOfExecution.swift – Marc Prud'hommeaux <marc@glimpse.io>
///
/// 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])`
///
/// 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 / 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! }
}