Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
SlaunchaMan / random.swift
Created August 25, 2020 19:45
Random numbers adding up to a sum
import Foundation
func randomNumbers(_ count: Int, totalling sum: Int) -> [Int] {
let randomOffsets = (0 ..< count - 1).map { _ in Int.random(in: 1 ..< sum) }
let range = ([0] + randomOffsets + [sum]).sorted()
var values: [Int] = []
for i in 0 ... count - 1 {
import Foundation
let cast = ["🚶‍♂️🚶🏻‍♂️🚶🏼‍♂️🚶🏽‍♂️🚶🏾‍♂️🚶🏿‍♂️🚶‍♀️🚶🏻‍♀️🚶🏼‍♀️🚶🏽‍♀️🚶🏾‍♀️🚶🏿‍♀️"]
var string = "Virtual #WWDC20 queue:\n"
while string.count <= 280 {
string.append(cast.randomElement()!)
}
@SlaunchaMan
SlaunchaMan / SwiftUIStackView.swift
Created June 16, 2020 14:03
A SwiftUI-like function builder approach for UIStackView.
//: A UIKit based Playground for presenting user interface
import UIKit
import PlaygroundSupport
@_functionBuilder
struct UIViewBuilder {
static func buildBlock(_ views: UIView...) -> [UIView] {
return views
}
@SlaunchaMan
SlaunchaMan / DynamicSubclassGenerator.swift
Created January 14, 2020 03:05
Generate dynamic subclasses of Objective-C objects in Swift
import Foundation
import ObjectiveC
private var subclassCounts: [String: Int] = [:]
func GenerateDynamicSubclass<T: NSObject>(of class: T.Type) -> T.Type! {
let oldClassName = NSStringFromClass(`class`) as String
var subclassCount = subclassCounts[oldClassName] ?? 0
let className = "\(oldClassName)_DynamicSubclass_\(subclassCount)"
@SlaunchaMan
SlaunchaMan / Locale+DistanceUnit.swift
Last active October 3, 2019 19:25
Determining which Locales use miles for distance formatting.
import Foundation
import MapKit
extension Locale {
var usesMilesForDistanceFormatting: Bool {
guard usesMetricSystem else { return true }
let miles = Measurement(value: 42, unit: UnitLength.miles)
let kilometers = miles.converted(to: .kilometers)
@SlaunchaMan
SlaunchaMan / FetchPublisher.swift
Last active June 21, 2019 05:11
FetchPublisher
//
// FetchPublisher.swift
// [REDACTED]
//
// Created by Jeff Kelley on 6/20/19.
// Copyright © 2019 Jeff Kelley. All rights reserved.
//
import CoreData
import Combine
@SlaunchaMan
SlaunchaMan / no_u.swift
Last active February 8, 2019 01:16
I wanted a version of zip(_:_:) that padded the end of the shorter sequence with Optionals.
import Foundation
func paddedZip<T, U, Sequence1, Sequence2>(
_ sequence1: Sequence1,
_ sequence2: Sequence2
) -> Zip2Sequence<[T?], [U?]> where
Sequence1 : Sequence, Sequence1.Element == T,
Sequence2 : Sequence, Sequence2.Element == U {
var array1: [T?] = []
var array2: [U?] = []
@SlaunchaMan
SlaunchaMan / GracefulDecoding.swift
Created October 24, 2018 19:57
Decoding items when one of them may not decode successfully.
extension KeyedDecodingContainer {
/// An item that cannot be decoded. Used in `compactDecode(arrayOf:forKey:)`
/// to represent items that could not be decoded.
private struct UndecodableItem: Decodable {}
/// Attempts to decode an array of type `T`. If an individual item in the
/// array fails to decode, these errors are not passed on to the caller;
/// instead, they are not included in the final array.
///
@SlaunchaMan
SlaunchaMan / runGYB.bash
Last active July 17, 2018 13:57
Bash script to run Gyb on the given input files or file lists.
GYB_PATH="${SRCROOT}/Vendor/gyb/gyb"
function gyb {
file=$1
if [ ${file: -4} == ".gyb" ]; then
"${GYB_PATH}" --line-directive '' -o "${file%.gyb}" "$file";
fi
}
if [ $SCRIPT_INPUT_FILE_COUNT -ne 0 ]; then
@SlaunchaMan
SlaunchaMan / AdvancedDatesAndTimesInSwift.swift
Created August 15, 2017 21:49
Sample code for my Advanced Dates and Times in Swift talk at 360iDev 2017.
//: Advanced Dates and Times in Swift
import Foundation
#if os(macOS)
import PlaygroundSupport
import UserNotifications
#endif
// Date Intervals