Skip to content

Instantly share code, notes, and snippets.

View rjchatfield's full-sized avatar

Smiley Rob rjchatfield

View GitHub Profile
@rjchatfield
rjchatfield / Promised.swift
Last active August 29, 2022 18:50
Promised values in Swift Concurrency
/// Wrapper around a lazily resolved using checked continuation.
///
/// Usage:
///
/// ```
/// let promiseA = Promised<String>()
/// async let a1 = promiseA.value
/// promiseA.resolve("AAA")
/// let a2 = await promiseA.value
/// print(await a1, a2)
/*
Inspired by https://forums.swift.org/t/running-an-async-task-with-a-timeout/49733
*/
/// - Throws: Throws `TimedOutError` if the timeout expires before `work` completes.
/// If `work` throws an error before the timeout expires, that error is propagated to the caller.
func withTimeout<R>(
_ maxDuration: TimeInterval,
returning returnType: R.Type = R.self,
work: @Sendable @escaping () async throws -> R
@rjchatfield
rjchatfield / Environment.swift
Last active September 14, 2020 23:14
Sourcery generation of EnvironmentCall
//sourcery: mockEnvironment
struct Environment {
var call1: () -> Void
var call2: () -> Foo
var call3: (Foo) -> Void
var call4: (Foo, Bar) -> Bazel
}
@rjchatfield
rjchatfield / ArrayBuilder.swift
Last active September 7, 2023 07:20
ArrayBuilder - Swift ~~FunctionBuilder~~ ResultBuilder
@resultBuilder
public struct ArrayBuilder<Element> {
public static func buildPartialBlock(first: Element) -> [Element] { [first] }
public static func buildPartialBlock(first: [Element]) -> [Element] { first }
public static func buildPartialBlock(accumulated: [Element], next: Element) -> [Element] { accumulated + [next] }
public static func buildPartialBlock(accumulated: [Element], next: [Element]) -> [Element] { accumulated + next }
// Empty Case
public static func buildBlock() -> [Element] { [] }
// If/Else
@rjchatfield
rjchatfield / 98.swift
Created April 24, 2020 12:52
98.swift
//
// 98.swift
//
// Created by Rob Chatfield on 24/4/20.
// Inspired by (98.css)[https://jdan.github.io/98.css]
// Copyright © 2020 Rob Chatfield. All rights reserved.
//
import SwiftUI
@rjchatfield
rjchatfield / LevenshteinDistance.swift
Last active April 1, 2020 03:54
Levenshtein Distance
// LevenshteinDistance.swift
//
// Created by Rob Chatfield on 1 Apr 2020
// Copyright 2020 Rob Chatfield. All rights reserved.
//
// This code is an updated Swift implementation based on "LevenshteinSwift" Created by Cory Alder.
// Which was based on the Objective-C "NSString+Levenshtein" Created by Mark Aufflick.
// Which was based loosely on the NSString(Levenshtein) code by Rick Bourner
//
// Improvements and suggestions welcome.
@rjchatfield
rjchatfield / 1_UITableViewState.swift
Last active February 28, 2020 06:32
UITableView DataSource/Delegate using State/Action/Reducer
import UIKit
extension UITableView {
struct State {
var sections: [Section]
var sectionIndexTitles: [String]?
struct Section {
var rows: [Row]
var titleForHeader: String?
@rjchatfield
rjchatfield / 1FlatMapFutures.swift
Last active February 16, 2020 12:01
Async fetcher
final class BoardShortFetcher {
let projectRepo: ProjectRepository
let projectGateway: ProjectGateway
let boardShortRepo: BoardShortRepository
let boardShortGateway: BoardShortGateway
init() { fatalError() }
// Only entry point
func retreiveBoardShort(boardFeatureLocator: BoardFeatureLocator) -> Future<BoardShortEntity, Error> {
@rjchatfield
rjchatfield / async_await_interopt.swift
Created January 27, 2020 07:33
Swift async/await interopt
/// Async code (one day) can be written in 3 ways:
/// 1. Callbacks
/// 2. Future
/// 3. async/await
///
/// Let's explore how they work together by defining
/// `myClosure`, `myFuture` & `myFunction`
/// each derived from the other two approaches.
@rjchatfield
rjchatfield / 1Playground.swift
Created January 21, 2020 10:33
Sudoku solver Pt.2: reference types
import PlaygroundSupport
import SwiftUI
PlaygroundPage.current.setLiveView(
GridResultView.all()
//GridResultView.single(.expert)
)
// Times: "copy+solve solve (val solve)"
Grid.easy // 6ms 0ms ( 8ms)