Skip to content

Instantly share code, notes, and snippets.

@karwa
karwa / variadic-generics-comparable.swift
Created February 22, 2024 15:53
Varadic property comparison in Swift
public enum SortOrder {
case ascending
case descending
}
@inlinable @inline(__always)
public func isLessThan<Root, each Value: Comparable>(
_ leftRoot: Root,
_ rightRoot: Root,
comparing properties: repeat ((Root) -> each Value, SortOrder)
@karwa
karwa / nested-protocols.md
Created May 31, 2023 17:27
Nested protocols

Allow Protocols to be Nested in Non-Generic Contexts

@karwa
karwa / docc-bootstring.swift
Last active June 14, 2022 12:01
proof of concept bootstring encoding for docc
// Copyright The swift-url Contributors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
@karwa
karwa / AutoFormPOC.swift
Created January 29, 2022 22:49
POC form generation using private reflection APIs
import SwiftUI
struct ContentView: View {
@State var immutableForm = false
@State var data = Person(
name: "Johnny Appleseed", address: "The spaceship", dateOfBirth: Date(),
aSwitch: true, anImmutableString: "Hello, world!", anImmutableSwitch: false
)
var body: some View {
// Use the following Package.swift:
// -----------------------------------------------------------
// swift-tools-version:5.5
// The swift-tools-version declares the minimum version of Swift required to build this package.
//
//import PackageDescription
//
//let package = Package(
// name: "uniqueid-test",
// dependencies: [
@karwa
karwa / replacesubrange-diy.swift
Created October 13, 2020 18:42
A generic implementation of 'replaceSubrange' for contiguous buffers
// This file contains a generic implementation of 'replaceSubrange' for contiguous buffers,
// including those only accessible indirectly such as `ManagedBuffer` subclasses.
//
// It includes optimisations for in-place replacement as well as consuming the original storage when additional
// capacity is required. The implementation is adapted from the standard library's source code, where it forms
// the basis of Array's implementation of replaceSubrange.
/// An object which contains a unique, mutable buffer.
/// This protocol is a required level of indirection so that `replaceElements` can allocate, fill, and return objects which provide their buffers indirectly.
///
extension Collection {
public subscript(inplace_slice bounds: Range<Index>) -> Slice<Self> {
get { fatalError() }
_modify {
var slice = Slice(base: self, bounds: bounds)
yield &slice
}
}
}
@karwa
karwa / ASCII.swift
Created May 3, 2020 20:56
ascii prototype
public struct ASCII: Equatable, Comparable {
public var codePoint: UInt8
@inlinable public init(_ v: UInt8) { self.codePoint = v }
}
extension ASCII {
// Homogenous comparisons.
@inlinable public static func < (lhs: ASCII, rhs: ASCII) -> Bool {
lhs.codePoint < rhs.codePoint
}