Skip to content

Instantly share code, notes, and snippets.

@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
}
@karwa
karwa / string-binary.swift
Created February 22, 2020 13:58
String(binary:)
extension String {
/// Creates a string of ones and zeros representing the given value in binary.
///
/// Unlike `String.init<T: BinaryInteger>(_:radix:uppercase:)`, negative values
/// are represented as their true, two's-complement bitpattern.
///
@inlinable
public init<T: BinaryInteger>(binary value: T) {
let bitWidth = value.bitWidth
@karwa
karwa / collection-split.swift
Last active February 12, 2020 21:09
Split collections by length
// MARK: - Sequence.
extension Sequence {
public func split(maxLength: Int) -> [ArraySlice<Element>] {
return Array(self)._eagerSplit(maxLength: maxLength)
}
}
extension Collection {
#! /usr/bin/swift
//
// - This is just some AppKit boilerplate to launch a window.
//
import AppKit
@available(OSX 10.15, *)
class AppDelegate: NSObject, NSApplicationDelegate {
let window = NSWindow()
let windowDelegate = WindowDelegate()
@karwa
karwa / generic-base64.swift
Last active December 21, 2019 06:46
Generic base64 encode/decode
// This is a version of swift-corelibs-foundation's base64 encoder/decoder,
// adapted to encode/decode any Collection of UInt8s.
//
// This means you can directly encode/decode:
// Array<UInt8>, String.UTF8View, Unsafe{Mutable}BufferPointer<UInt8>, Unsafe{Raw}BufferPointer,
// SwiftNIO's ByteBuffer, Data, NSData and DispatchData (of course), and more.
//
// e.g.
// let encoded = "Hello, this is an encoded String! 🏆".utf8.base64EncodedString()
// print(encoded)
enum CountPredicate {
case greaterThan(_ n: Int)
case equalTo(_ n: Int)
}
extension Collection {
func count(is predicate: CountPredicate) -> Bool {
switch predicate {
case .equalTo(let val):
@karwa
karwa / unzip2collection.swift
Last active July 9, 2018 21:07
unzip collection
/// Provides a view of a collection of tuples as multiple collections.
public func unzip<C, TA, TB>(_ collection: C) -> Unzip2Collection<C, TA, TB> where C: Collection {
return Unzip2Collection(collection)
}
/// Provides a view of a collection of tuples as multiple collections.
public struct Unzip2Collection<C, TupleA, TupleB> where C: Collection, C.Iterator.Element == (TupleA, TupleB) {
private let _col: C
public init(_ c: C) { _col = c }
import Foundation
private extension DateComponents {
func scaled(by: Int) -> DateComponents {
let s: (Int?)->Int? = { $0.map { $0 * by } }
return DateComponents(calendar: calendar,
timeZone: timeZone,
era: s(era),
year: s(year), month: s(month), day: s(day),
hour: s(hour), minute: s(minute), second: s(second), nanosecond: s(nanosecond),
@karwa
karwa / IndexExpression2.swift
Last active January 3, 2018 23:10
Another idea for more convient slicing in Collections.
// Another idea for more convient slicing in Collections.
/// Lazily-resolved indexes, by their nature, are not really safe to use with a potentially-mutating collection because
/// their cached 'base' index might have been invalidated. If you want to use them in that situation, you have to ensure
/// they point to locations which will survive the mutation, then manually resolve them *before* mutating.
///
/// Or... you use the `Collection.easySlice()` method to do your slicing on a (hopefully) immutable copy
/// of the collection.
///
@karwa
karwa / IndexExpression.swift
Last active January 3, 2018 22:32
index expressions for collections
enum IndexType<C: Collection> {
case keypath(KeyPath<C, C.Index>)
case index(C.Index)
}
struct IndexExpression<C: Collection> {
let base: IndexType<C>
let distance: C.IndexDistance
func resolve(in collection: C) -> C.Index {