Skip to content

Instantly share code, notes, and snippets.

Avatar

Nate Cook natecook1000

View GitHub Profile
View integerWithBytes.swift
// integerWithBytes.swift
// as seen in http://natecook.com/blog/2015/03/a-sanatorium-for-swift-generics/
//
// (c) 2015 Nate Cook, licensed under the MIT license
protocol BitshiftOperationsType {
func <<(lhs: Self, rhs: Self) -> Self
func >>(lhs: Self, rhs: Self) -> Self
func <<=(inout lhs: Self, rhs: Self)
func >>=(inout lhs: Self, rhs: Self)
@natecook1000
natecook1000 / operatorCharacters.swift
Last active February 23, 2023 12:58
Allowed characters for Swift operators
View operatorCharacters.swift
import Foundation
extension UnicodeScalar : ForwardIndexType {
public func successor() -> UnicodeScalar {
return UnicodeScalar(value + 1)
}
}
var operatorHeads: [UnicodeScalar] = Array("=-+!*%<>&|^~?".unicodeScalars)
operatorHeads += Array("\u{00A1}" ... "\u{00A7}")
View isNullOrEmpty.swift
protocol StringType {
var isEmpty: Bool { get }
}
extension String : StringType { }
extension Optional where Wrapped: StringType {
var isNullOrEmpty: Bool {
return self?.isEmpty ?? true
}
@natecook1000
natecook1000 / NSCalendar+Swift.swift
Created March 18, 2015 03:08
Swift-friendly NSCalendar methods
View NSCalendar+Swift.swift
// NSCalendar+Swift.swift
// A set of Swift-idiomatic methods for NSCalendar
//
// (c) 2015 Nate Cook, licensed under the MIT license
extension NSCalendar {
/// Returns the hour, minute, second, and nanoseconds of a given date.
func getTimeFromDate(date: NSDate) -> (hour: Int, minute: Int, second: Int, nanosecond: Int) {
var (hour, minute, second, nanosecond) = (0, 0, 0, 0)
getHour(&hour, minute: &minute, second: &second, nanosecond: &nanosecond, fromDate: date)
@natecook1000
natecook1000 / CalculatorView.swift
Last active June 6, 2022 01:00
An IBInspectable Calculator Construction Set
View CalculatorView.swift
// CalculatorView.swift
// as seen in http://nshipster.com/ibinspectable-ibdesignable/
//
// (c) 2015 Nate Cook, licensed under the MIT license
/// The alignment for drawing an String inside a bounding rectangle.
enum NCStringAlignment {
case LeftTop
case CenterTop
case RightTop
View OptionSet+Collection.swift
/// This is a useful index that can store a comparable element or the end of
/// a collection. Similar to https://github.com/apple/swift/pull/15193
enum IndexWithEnd<T : Comparable> : Comparable {
case element(T)
case end
static func < (lhs: IndexWithEnd, rhs: IndexWithEnd) -> Bool {
switch (lhs, rhs) {
case (.element(let l), .element(let r)):
return l < r
View Transposition.swift
struct Transposition<Base: Collection>: RandomAccessCollection
where Base.Element: RandomAccessCollection
{
typealias Index = Int
typealias Indices = Range<Int>
var base: Base
struct Transposed: Collection {
typealias Index = Base.Index
View Swift Unicode Properties.md

The following table lists the Unicode properties as described here, along with the corresponding Unicode.Scalar.Properties property and type, if implemented.

Unicode Property Unicode.Scalar.Properties Type
General
Name name String?
Name_Alias nameAlias String?
Block
Age age `Unicode.Versi
View PostHocDispatch.swift
// Based on https://gist.github.com/dabrahams/852dfdb0b628e68567b4d97499f196f9
struct Dispatch<Model> {
func apply<A, R>(_ a: A, _ f: (Model) -> R) -> R {
f(a as! Model)
}
}
protocol RandomAccessCollectionDispatch {
func fastCount<C: Collection>(_ x: C) -> Int?
}
@natecook1000
natecook1000 / openRanges.swift
Last active May 27, 2021 19:37
Open-ended range operators for Swift
View openRanges.swift
// Open-ended range operators
//
// 100... is equivalent to 100...Int.max
// ...-100 is equivalent to Int.min...-100
// ..<3 is equivalent to Int.min..<3
import Swift
/// Conforming types provide static `max` and `min` constants.
protocol MinMaxType {