Skip to content

Instantly share code, notes, and snippets.

View beccadax's full-sized avatar

Becca Royal-Gordon beccadax

View GitHub Profile
@beccadax
beccadax / Nodes.swift
Last active September 28, 2016 06:22
Code that doesn't work because existentials don't conform to their own protocols.
protocol MyNode {
var total: Int { get }
}
protocol MyParentNode: MyNode {
// Each MyParentNode-conforming type can only have certain types of child nodes
// (basically, type A can have B, C, or D; B can have C or D; C can have D; and D is not a MyParentNode).
// I want to model these limitations in the types of children permitted.
associatedtype Child: MyNode
var children: [Child] { get set }
// Evaluates the given closure when two `Optional` instances are not `nil`,
// passing the unwrapped values as parameters. (Thanks, Mike Ash, Tim Vermeulen)
// Following the example of IEEE754 with NAN, any comparison involving
// .None is false
//
// See also: http://stackoverflow.com/questions/1565164/what-is-the-rationale-for-all-comparisons-returning-false-for-ieee754-nan-values
/// Returns nil if either parameter is nil, or a tuple of the parameters
/// otherwise.
func all<T, U>(_ first: T?, _ second: U?) -> (T, U)? {
@beccadax
beccadax / GenericHierarchicalTurnstyleState.swift
Last active July 28, 2016 14:38
HierarchicalTurnstyleState with Functioning/Broken implemented with a generic type
// Untested; consider this a sketch
typealias HierarchicalTurnstyleState = StatusState<FunctioningTurnstyleState>
enum StatusState<MachineState: StateType>: StateType {
case Functioning(MachineState)
case Broken(oldState: MachineState)
typealias Event = StatusEvent<MachineState.Event>
typealias Command = MachineState.Command
@beccadax
beccadax / Example.swift
Created July 26, 2016 12:28
Alternate design for RelativeRange
let array = Array(1...10)
array[.startIndex ..< 3]
array[.startIndex + 2 ..< .endIndex - 1]
@beccadax
beccadax / Example.swift
Created July 26, 2016 10:22
Experimenting with the $-based relative range syntax
let array = Array(1...10)
array[$ ..< 3]
array[$ + 2 ..< $ - 1]
@beccadax
beccadax / Contents.swift
Created July 9, 2016 01:31
Non-stdlib initial prototype of IncompleteRange design
//: Playground - noun: a place where people can play
func withSampleData(f: (inout [Int]) -> Void) {
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9]
f(&array)
}
// IncompleteRange, nil lowerBound
withSampleData { array in
array[..<6]
@beccadax
beccadax / UnsafeMutableRawPointerChanges.swift
Last active July 3, 2016 07:15
Bikeshedding of SE-0107.
struct UnsafeMutableRawPointer : Strideable, Hashable, _Pointer {
// Anything I don't list, I'm okay with. SE-0107 names are commented
// out above my alternatives.
// I don't like the use of `cast` for these. Yes, "cast" is its own
// past participle, but it's not clear that these operations are
// nonmutating. I prefer `casting` because it's clearer about that.
//
// func cast<T>(to: UnsafeMutablePointer<T>.Type) -> UnsafeMutablePointer<T>
// func cast<T>(to: UnsafePointer<T>.Type) -> UnsafePointer<T>

Rationalizing Sequence end-operation names

Introduction

Sequence and Collection offer many special operations which access or

The Sequence and Collection protocols offer a wide variety of APIs which are defined to operate on, or from, one end of the sequence:

Fixed Size Return Return Index (1) Subsequence without Remove (2) Pop (2) Equate (1) (3)
First 1 C.first - S.dropFirst() C.removeFirst() C.popFirst() -
Last 1 C.last - S.dropLast() C.removeLast() C.popLast() -
First (n: Int) S.prefix(_:) - S.dropFirst(_:) C.removeFirst(_:) - S.starts(with:)
Last (n: Int) S.suffix(_:) - S.dropLast(_:) C.removeLast(_:) - -
**Matching A

Tuple-Based Compound Optional Binding

Introduction

This proposal enhances optional binding with a new, tuple-based syntax for