Skip to content

Instantly share code, notes, and snippets.

@milseman
milseman / magic_caret.swift
Created April 17, 2019 19:28
Magic Caret
prefix operator ^
public prefix func ^<T>(_ t: T) -> T? { return .some(t) }
//
// Numbers
//
@milseman
milseman / offset_indexing.swift
Last active April 30, 2019 00:13
Offset Indexing
precedencegroup RelativeOffsetPrecedence {
higherThan: RangeFormationPrecedence
}
infix operator ++ : RelativeOffsetPrecedence
infix operator -- : RelativeOffsetPrecedence
extension Collection {
internal func _clampedIndex(_ idx: Index, offsetBy offset: Int) -> Index {
let limit = offset < 0 ? startIndex : endIndex
@milseman
milseman / offset_indexing_arrow_operator.swift
Created April 30, 2019 20:41
Offset Indexing, with arrow operators instead
precedencegroup RelativeOffsetPrecedence {
higherThan: RangeFormationPrecedence
}
infix operator --> : RelativeOffsetPrecedence
infix operator <-- : RelativeOffsetPrecedence
prefix operator -->
postfix operator <--
extension Collection {
@milseman
milseman / offset_indexing_xwu.swift
Created May 2, 2019 22:45
Offset Indexing, using OffsetRange
// TODO: doc
public struct OffsetBound {
internal enum Kind {
case fromStart(Int)
case fromEnd(Int)
}
internal var kind: Kind
init(fromStart: Int) {
self.kind = .fromStart(fromStart)
@milseman
milseman / offset_indexing_phantom_xwu.swift
Last active May 3, 2019 00:21
Offset Indexing, using phantom-typed OffsetRange
// TODO: doc
public struct OffsetBound<Bound> {
internal enum Kind {
case fromStart(Int)
case fromEnd(Int)
}
internal var kind: Kind
public init(fromStart: Int) {
self.kind = .fromStart(fromStart)
@milseman
milseman / offset_indexing_no_range.swift
Created May 3, 2019 21:28
Offset Indexing, only OffsetBound
// TODO: doc
public struct OffsetBound {
internal enum Kind {
case fromStart(Int)
case fromEnd(Int)
}
internal var kind: Kind
public init(fromStart: Int) {
self.kind = .fromStart(fromStart)
@milseman
milseman / offset_no_range_phantom.swift
Created May 3, 2019 22:25
Broken: Offset Indexing, Existing ranges, Phantom typed
// TODO: doc
public struct OffsetBound<Bound> {
internal enum Kind {
case fromStart(Int)
case fromEnd(Int)
}
internal var kind: Kind
public init(fromStart: Int) {
self.kind = .fromStart(fromStart)
@milseman
milseman / offset_no_range_explicit_end.swift
Created May 6, 2019 19:25
Offset Indexing, no range, explicit end
// TODO: doc
public struct OffsetBound {
internal enum Kind {
case fromStart(Int)
case fromEnd(Int)
}
internal var kind: Kind
internal init(fromStart: Int) {
self.kind = .fromStart(fromStart)
@milseman
milseman / offset_no_range_no_label.swift
Last active May 6, 2019 21:00
Offset indexing, no range, no label
// TODO: doc
public struct OffsetBound {
internal enum Kind {
case fromStart(Int)
case fromEnd(Int)
}
internal var kind: Kind
internal init(fromStart: Int) {
self.kind = .fromStart(fromStart)
@milseman
milseman / offset_bound_gist.swift
Last active September 27, 2021 16:55
OffsetBound
// The below will be a customization point on Collection. For the purposes of
// this gist, we'll fake/approximate it with static overloads
extension Collection {
/// Returns an index `distance` positions prior to `i` if it exists.
///
/// Other methods such as `index(_:offetBy:)` must not be passed a negative
/// offset if the collection is bidirectional. This method will perform a
/// negative offset even if the collection is not bidirectional, by using a
/// less efficient means. `BidirectionalCollection` customizes this with a