Skip to content

Instantly share code, notes, and snippets.

041f94dcd6f7e66679da2ead9c76e5aaa24f0d17ec559ab354806133340f877095226f2d4bc36a07c4cf1f1e9269bb3bb55970234a0af292c869446ce0f6e8e886;nsirlconnection
@klundberg
klundberg / optional-conditional-collection.swift
Created February 21, 2018 06:39
Conditional conformance in swift 4.1 for optional for some common protocols
extension Optional: Collection where Wrapped: Collection {
public typealias Element = Wrapped.Element
public typealias Index = Wrapped.Index
public func index(after i: Wrapped.Index) -> Wrapped.Index {
switch self {
case let value?:
return value.index(after: i)
case nil:
fatalError() // TODO: ???
@klundberg
klundberg / optional-collection.swift
Last active February 24, 2018 23:13
Potential Swift 4.1 implementation of conditional conformace to Collection for Optional<Collection>
public struct OptionalIndex<InnerIndex: Comparable> {
let inner: InnerIndex?
init(_ base: InnerIndex) {
inner = base
}
init() {
inner = nil
}
@klundberg
klundberg / machine.js
Created November 14, 2019 23:26
Generated by XState Viz: https://xstate.js.org/viz
// Available variables:
// - Machine
// - interpret
// - assign
// - send
// - sendParent
// - spawn
// - raise
// - actions
@klundberg
klundberg / Binding+initially.swift
Created June 28, 2023 06:41
A mutable variant of Binding.constant to help with swiftui previews
extension Binding {
/// like Binding.constant but lets you update the value and stores that updated value via a captured enclosing reference.
public static func initially(_ value: Value) -> Binding<Value> {
let box = Box(value)
return .init(
get: { box.value },
set: { box.value = $0 }
)
}