Skip to content

Instantly share code, notes, and snippets.

View danielctull's full-sized avatar

Daniel Tull danielctull

View GitHub Profile
@danielctull
danielctull / Day10.swift
Created December 10, 2021 10:34
Solution for Advent of Code 2021 Day 10.
import Advent
import Algorithms
import Foundation
public enum Day10: Day {
public static let title = "Syntax Scoring"
public static func part1(_ input: Input) throws -> Int {
@danielctull
danielctull / Tracker+SwiftUI.swift
Last active October 18, 2021 11:19
Exploring the tracking options in view level code.
import SwiftUI
// MARK: - SwiftUI environment definition (CoreTracking)
private struct EmptyTracker: Tracker {
func track(_ event: Event) {}
}
public struct TrackerEnvironmentKey: EnvironmentKey {
@danielctull
danielctull / Either.swift
Last active March 2, 2021 10:37
Trying to see what casting options exist with an Either type.
public enum Either<Left, Right> {
case left(Left)
case right(Right)
}
extension Either {
public func `as`(_ type: Left.Type) -> Left? {
guard case let .left(left) = self else { return nil }
return left
@danielctull
danielctull / Really.swift
Last active June 1, 2022 08:58
I think I found the perfect improvement for force unwrapping in swift.
// EXAMPLE
let string: String? = "Hello World" // String?
let unwrappedString = string.really.srsly.itsfine.honestly.aaarggh // String
print(unwrappedString)
// IMPLEMENTATION
extension Optional {
@danielctull
danielctull / IntersperseIndex.swift
Created October 13, 2020 08:57
Another idea for the intersperse index.
extension Intersperse: Collection where Base: Collection {
public struct Index: Comparable {
enum Representation {
case end
case element(Base.Index)
case separator(Base.Index)
}
let representation: Representation
@danielctull
danielctull / BindingInvmap.swift
Last active August 13, 2020 19:38
Implementing an invmap function on SwiftUI's Binding type.
import SwiftUI
extension Binding {
func invmap<NewValue>(
to: @escaping (Value) -> NewValue,
from: @escaping (NewValue) -> Value
) -> Binding<NewValue> {
Binding<NewValue>(
@danielctull
danielctull / Multimap.swift
Created April 19, 2020 09:10
A multimap implementation.
struct Multimap<Key: Hashable, Value: Hashable> {
fileprivate struct _Element: Hashable {
let key: Key
let value: Value
}
private var elements: Set<_Element> = []
@danielctull
danielctull / NestedPropertyWrappers.swift
Created January 10, 2020 11:49
Trying to created nested property wrappers in Swift by conforming the property wrappers themselves to StringProtocol.
@propertyWrapper
struct B {
var wrappedValue: Int
}
@propertyWrapper
struct Clamp {
var wrappedValue: Int
}
@danielctull
danielctull / UnwrapView.swift
Last active November 5, 2019 00:43
A view which takes a value and shows one view or another depending on whether the value is nil or not.
import SwiftUI
public struct UnwrapView<Some: View, None: View>: View {
private let some: () -> Some?
private let none: () -> None?
public init<Value>(value: Value?,
some: @escaping (Value) -> Some,
@danielctull
danielctull / UserDefaultsKey.swift
Created October 7, 2019 14:09
UserDefaults Key with a Property Wrapper
import Foundation
// MARK:- Usage
extension UserDefaults.Key where Value == Bool {
static let shouldDoSomething = Self(name: "shouldDoSomething", default: false)
}
struct View {