Skip to content

Instantly share code, notes, and snippets.

Loving Life

Ben Leggiero BenLeggiero

Loving Life
View GitHub Profile
View SO-Answer-42079976.swift
import Foundation
public extension Array where Element == UInt8 {
// MARK: - Constants
/// This is used by the `toHexString()` method
private static let uppercaseHexCharacters : [Character] =
BenLeggiero / LICENSE.txt
Last active Aug 22, 2020
Make UIColor Codable
View LICENSE.txt
This section is meant to explain the purpose of this License in Layman's
terms, and should thusly never be seen as legally binding. This disclaimer does
not apply to further sections of this License.
1. This is no one's product and is in the public domain
2. You're not responsible for any bad things that might happen because someone
used this product
BenLeggiero / runAnimationGroup shim.swift
Created May 7, 2019
NSAnimationContext.runAnimationGroup in OS X 10.11 and older
View runAnimationGroup shim.swift
// By Ben Leggiero on 2019-05-07
// License is BH-1-PS:
import AppKit
public extension NSAnimationContext {
BenLeggiero / CompilationException
Last active Dec 24, 2018
Kotlin Compiler Crashes
View CompilationException
<Exception from standalone Kotlin compiler>
Kotlin: [Internal Error] org.jetbrains.kotlin.codegen.CompilationException: Back-end (JVM) Internal error: wrong code generated
org.jetbrains.kotlin.codegen.CompilationException Back-end (JVM) Internal error: Couldn't transform method node:
reposition (Lorg/bh/tools/base/math/geometry/FractionRect;Lorg/bh/tools/base/math/geometry/FractionRect;)Lorg/bh/tools/base/math/geometry/FractionRect;:
@Lorg/jetbrains/annotations/NotNull;() // invisible
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 0
@Lorg/jetbrains/annotations/NotNull;() // invisible, parameter 1
LDC "rect"
BenLeggiero / Array.joinedWithComma.swift
Last active Sep 11, 2018
Slight tweaks to Cortis Clark's joinedWithComma function
View Array.joinedWithComma.swift
extension Array where Iterator.Element == String {
/// Originally by Cortis Clark on StackOverflow, modified by Ben Leggiero for an example
/// - SeeAlso:
func joinedWithComma(useOxfordComma: Bool = true, maxItemCount: UInt8? = nil) -> String {
let result: String
if let maxItemCount = maxItemCount, count > maxItemCount {
result = self[0 ..< maxItemCount].joined(separator: ", ") + ", etc."
} else if count >= 2 {
let lastIndex = count - 1
let extraComma = (useOxfordComma && count > 2) ? "," : ""
BenLeggiero / Elevate Permissions.swift
Created Aug 8, 2018
Elevates permissions in non-sandboxed Swift apps
View Elevate Permissions.swift
import Foundation
private func gainPermissions() {
var authorizationRef: AuthorizationRef? = nil
var authItem = AuthorizationItem(name: kSMRightBlessPrivilegedHelper, valueLength: 0, value: nil, flags: 0)
var authRights = AuthorizationRights(count: 1, items: &authItem)
let flags: AuthorizationFlags = [.interactionAllowed, .preAuthorize, .extendRights]
var environment = AuthorizationEnvironment()
BenLeggiero / QuickSorter.swift
Last active Mar 6, 2018
General-use Quicksort in Swift
View QuickSorter.swift
/// Uses quicksort with the Lomuto partition scheme.
/// By Ben Leggiero, written on 2018-03-05. Copyright BH-0-PD
struct QuickSorter {
/// Performs a quicksort with Lomuto partition using the given array and returns the result
func quicksorting<C: Comparable>(_ unsorted: [C]) -> [C] {
var arrayCopy = unsorted
BenLeggiero / What is a Monad?.swift
Created Nov 27, 2017
This file is an illustration of the concepts described in the Computerphile video "What is a Monad?", but in Swift rather than the video's Haskell.
View What is a Monad?.swift
/// This file is an illustration of the concepts described in the Computerphile video "What is a Monad?", but in Swift rather than the video's Haskell.
import Cocoa
// MARK: - Stuff we need in order to make our code look like the code in the video
BenLeggiero / NSWindow+Fade.swift
Last active Jan 13, 2021 — forked from indragiek/NSWindow+Fade.h
NSWindow+Fade - Animator proxy based NSWindow fading
View NSWindow+Fade.swift
// NSWindow+Fade.swift
// BH Bezel Notification
// Created by Ben Leggiero on 2017-11-09.
// Translated to Swift 4 from the original (ObjC):
// Copyright © 2017 Ben Leggiero. All rights reserved.
import Foundation