Skip to content

Instantly share code, notes, and snippets.

View DivineDominion's full-sized avatar

Christian Tietze DivineDominion

View GitHub Profile
@jstn
jstn / RandomNumbers.swift
Last active May 5, 2023 03:26
generate random numbers for 64-bit types while mitigating modulo bias
/*
`arc4random_uniform` is very useful but limited to `UInt32`.
This defines a generic version of `arc4random` for any type
expressible by an integer literal, and extends some numeric
types with a `random` method that mitigates for modulo bias
in the same manner as `arc4random`.
`lower` is inclusive and `upper` is exclusive, thus:
@candostdagdeviren
candostdagdeviren / pre-commit
Last active April 22, 2023 09:32
Git Pre-Commit hook with SwiftLInt
#!/bin/bash
#Path to swiftlint
SWIFT_LINT=/usr/local/bin/swiftlint
#if $SWIFT_LINT >/dev/null 2>&1; then
if [[ -e "${SWIFT_LINT}" ]]; then
count=0
for file_path in $(git ls-files -m --exclude-from=.gitignore | grep ".swift$"); do
export SCRIPT_INPUT_FILE_$count=$file_path
@justinhj
justinhj / gist:eb2d354d06631076566f
Created May 12, 2014 02:01
Sending a notification from emacs
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Terminal notifier
;; requires 'sudo gem install terminal-notifier'
;; stolen from erc-notifier
(defvar terminal-notifier-command (executable-find "terminal-notifier") "The path to terminal-notifier.")
; (terminal-notifier-notify "Emacs notification" "Something amusing happened")
(defun terminal-notifier-notify (title message)
@steipete
steipete / PSPDFThreadSafeMutableDictionary.m
Last active December 10, 2022 09:37
Simple implementation of a thread safe mutable dictionary. In most cases, you want NSCache instead, but it can be useful in situations where you want to manually control what is evicted from the cache in low memory situations.**Warning:** I only use this for setting/getting keys. Enumeration is not thread safe here and will still throw exception…
//
// PSPDFThreadSafeMutableDictionary.m
//
// Copyright (c) 2013 Peter Steinberger, PSPDFKit GmbH. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
@russbishop
russbishop / TypeErasure.swift
Last active May 18, 2022 01:20
Type erasure with multiple adopting types
// Paste me into a playground!
import Cocoa
//: # Basic Setup
protocol FancyProtocol {
associatedtype Thing
func holdPinkyUp(x: Thing)
}
@IanKeen
IanKeen / Decodable+Random.swift
Last active March 29, 2022 13:06
Custom Decoder that can be used to create Decodable instances that are populated with random values
import Foundation
extension Decodable {
public static func randomInstance() throws -> Self {
let decoder = RandomDecoder()
return try Self(from: decoder)
}
}
private class RandomDecoder: Decoder {
@DivineDominion
DivineDominion / URL+resolvedAgainstBase.swift
Last active January 31, 2022 10:37
Given two file path URLs, determine the shortest relative path to get from one to the other, e.g. `../../folder/file.txt`
// Copyright © 2021 Christian Tietze. All rights reserved. Distributed under the MIT License.
import Foundation
extension URL {
/// Produces a relative path to get from `baseURL` to the receiver for use in e.g. labels.
///
/// When there's no common ancestor, e.g. `/tmp/` and `/var/`, then this returns an absolute path. The only exception to this rule is when `baseURL` itself is the root `/`, since tor that base _all_ paths are relative.
///
/// - Returns: Shortest relative path to get from `baseURL` to receiver, e.g. `"../../subdirectory/file.txt"`, and `"."` if both are identical. Absolute path (or absolute URL for non-`file://` URLs) if there's nothing in common.
@klundberg
klundberg / json-codable.swift
Last active September 2, 2021 22:35
encode/decode arbitrary JSON data with swift 4's Codable
enum JSONValue: Codable, Equatable {
struct JSONValueDecodingError: Error {
let message: String
}
case boolean(Bool)
case number(Double)
case string(String)
case array([JSONValue?])
case object([String: JSONValue?])
@AliSoftware
AliSoftware / SwiftCopying.swift
Last active August 18, 2021 19:41
TypeSafe copy()/mutableCopy() (NSCopying/NSMutableCopying) in Swift
import Foundation
//: Swift type-safe protocol versions of (Mutable)Copying
protocol SwiftCopying {
associatedtype NonMutableType = Self
func clone() -> NonMutableType
}
extension SwiftCopying where Self: NSCopying {
func clone() -> NonMutableType {
return self.copy() as! NonMutableType
@preble
preble / Completion.swift
Created January 20, 2016 23:32
A cancellable completion for Swift.
final class Completion<R> {
private let closure: (R) -> Void
private var cancelled = false
/// `closure` is called upon completion, if not cancelled.
init(closure: (R) -> Void) {
self.closure = closure
}