This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// 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 } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum CountPredicate { | |
case greaterThan(_ n: Int) | |
case equalTo(_ n: Int) | |
} | |
extension Collection { | |
func count(is predicate: CountPredicate) -> Bool { | |
switch predicate { | |
case .equalTo(let val): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK: - Sequence. | |
extension Sequence { | |
public func split(maxLength: Int) -> [ArraySlice<Element>] { | |
return Array(self)._eagerSplit(maxLength: maxLength) | |
} | |
} | |
extension Collection { | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Collection { | |
public subscript(inplace_slice bounds: Range<Index>) -> Slice<Self> { | |
get { fatalError() } | |
_modify { | |
var slice = Slice(base: self, bounds: bounds) | |
yield &slice | |
} | |
} | |
} |