Skip to content

Instantly share code, notes, and snippets.

@DandyLyons
DandyLyons / UpcomingFeature.swift
Created November 19, 2024 00:24
Swift `UpcomingFeature` enum
// Please feel free to add this gist to your own `Package.swift` file to enable quick type-safe Swift language feature flagging.
extension SwiftSetting {
/// Feature Flag Strings
///
/// Found at https://www.swift.org/migration/documentation/swift-6-concurrency-migration-guide/sourcecompatibility/
/// Please update this list as new feature flags are added.
enum UpcomingFeature: String {
/// SE-0418: https://github.com/swiftlang/swift-evolution/blob/main/proposals/0418-inferring-sendable-for-methods.md
case inferringSendableFromCapture = "InferSendableFromCaptures"
@DandyLyons
DandyLyons / README.md
Last active December 12, 2024 22:06
Selective Equality Checking in Swift

Selective Equality Checking in Swift

This gist vends a new protocol called SelectiveEquatable which contains a single method, isEqual(to:by:). This method allows you to check the equality of two values by selectively picking which properties you would like to compare.

SelectiveEquatable is also available as a Swift Package!

Background

To see my thought process of how and why I created this, check out my blog post: Selective Equality Checking in Swift.

@DandyLyons
DandyLyons / hasSameElements(as:).swift
Created October 30, 2024 00:22
Unordered Equality Checking in Swift
extension Sequence where Element: Hashable {
/// Count the number of occurrences of each value in a sequence
public func countFrequency() -> [Element: Int] {
var result = [Element: Int]()
for element in self {
result[element, default: 0] += 1
}
return result
}
@DandyLyons
DandyLyons / IceCreamShop.swift
Created October 8, 2024 19:43
Typed throws in Swift (multiple types)
import Foundation
struct IceCreamShop {
enum Error: Swift.Error {
case notEnoughMoney
case flavorNotSoldHere
case flavorError(IceCreamFlavor.Error)
}
private(set) var availableFlavors: [String: IceCreamFlavor]
@DandyLyons
DandyLyons / OptionalTextField.swift
Created August 27, 2024 17:25
OptionalTextField.swift
//
// OptionalTextField.swift
//
//
// Created by Daniel Lyons on 2024-08-12.
//
import Foundation
import SwiftUI
@DandyLyons
DandyLyons / ProgrammaticScrollView.swift
Created July 28, 2024 02:53
ProgrammaticScrollView
import SwiftUI
import Foundation
struct ProgrammaticScrollView<Content: View>: View {
@Binding private var scrollID: Int?
let content: () -> Content
init(scrollID: Binding<Int?>, @ViewBuilder content: @escaping () -> Content) {
self._scrollID = scrollID
self.content = content
}
@DandyLyons
DandyLyons / TreeBasedPush.swift
Created July 11, 2024 19:29
Tree-Based Push onto NavigationStack using TCA
import SwiftUI
@main
struct TreeBasedPushApp: App {
var body: some Scene {
WindowGroup {
TreeBasedPush_V()
}
}
}
@DandyLyons
DandyLyons / gist:f21d3ac89e5dcdb4b4bc109d36f5b83d
Created April 16, 2024 02:45
2024-04-15 FoodOrderBot Routes
import Fluent
import OpenAI
import Vapor
// THIS IS THE INTERFACE FOR THE OPENAI API THAT WE'LL USE LATER
//let chatQuery = ChatQuery(
// messages: <#T##[Self.ChatCompletionMessageParam]#>,
// model: .gpt4_turbo_preview,
// frequencyPenalty: <#T##Double?#>,
// logitBias: <#T##[String : Int]?#>,
@DandyLyons
DandyLyons / SharedObservable
Last active April 1, 2024 17:08
This demonstrates that Shared in TCA will automatically add observability to value types such as structs, but not to reference types such as classes.
//
// SharedObservable.swift
// SpeechExample
//
// Created by Daniel Lyons on 2024-04-01.
//
import ComposableArchitecture
import Foundation
import SwiftUI
@DandyLyons
DandyLyons / _ReducerPrinter + filtering
Last active March 29, 2024 17:50
Filter ._printChanges() so that it only prints what you want
import ComposableArchitecture
extension _ReducerPrinter {
public static func filtered(actions includingActions: @escaping (Action) -> Bool) -> Self {
Self { receivedAction, oldState, newState in
if includingActions(receivedAction) {
// repeated from _ReducerPrinter.customDump
// TODO: Return a custom dump so that you don't need to repeat code
var target = ""
target.write("received action:\n")