Skip to content

Instantly share code, notes, and snippets.

@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 / collection_searchers.swift
Last active July 6, 2019 01:21
WIP: Collection Consumers and Searchers
///
/// Collection Consumers
///
protocol CollectionConsumer {
associatedtype Element
func consumeFront<C: Collection>(_ c: C) -> C.Index? where C.Element == Element
}
@milseman
milseman / string_formatters.swift
Last active September 11, 2019 18:23
String Formatters Through Interpolation
struct StringFormatters {
var text = "Hello, World!"
}
/*
This is the master template for fprintf format string functionality.
For development purposes only
```
@milseman
milseman / offset_bound_proposal.md
Last active October 6, 2019 06:16
Offset-Based Access to Indices, Elements, and Slices

Changes in v2

  • Removed .start and .end, now all OffsetBounds are built from .first and .last.
    • Simplifies usage and learnability. .last + 1 representing the end is mostly only relevant to documentation.
    • Better delineates OffsetBound abstraction and terminology from indices.
  • Added RangeReplaceableCollection convenience overloads, as well as subscript setters.
    • .insert(at:), .remove(at:), etc.,
@milseman
milseman / eat_seek_peek.swift
Created February 28, 2018 20:26
Playground for Self-sliced Collections: eat/seek/peek
// Eat/seek/peek
extension Collection where SubSequence == Self, Element: Equatable {
mutating func eat() -> Element {
defer { self = self.dropFirst() }
return peek()
}
mutating func eat(_ n: Int) -> SubSequence {
let (pre, rest) = self.seek(n)
defer { self = rest }