Skip to content

Instantly share code, notes, and snippets.

Avatar

Jaden Geller JadenGeller

View GitHub Profile
@JadenGeller
JadenGeller / Interleave.swift
Created May 10, 2022
Interleave and Interpolate
View Interleave.swift
struct Interleave<Evens: Collection, Odds: Collection> {
var evens: Evens
var odds: Odds
init(_ evens: Evens, _ odds: Odds) {
precondition(evens.count == odds.count || evens.count == odds.count + 1)
(self.evens, self.odds) = (evens, odds)
}
}
@JadenGeller
JadenGeller / deep_union.ts
Created Feb 15, 2022
Deeply union a type
View deep_union.ts
export type DeepUnion<T, V> = T extends object
? { [P in keyof T]: DeepUnion<T[P], V> }
: T | V;
// type Example1 = DeepUnion<["hello", [1, 2, 3]], null>;
// = ["hello" | null, [1 | null, 2 | null, 3 | null]];
// type Example2: = DeepUnion<{ name: string, age: number }, undefined>;
// = DeepUnion<{ name: string | undefined, age: number | undefined }, undefined>;
@JadenGeller
JadenGeller / Result+Legacy.swift
Created Feb 13, 2022
Create a result from a legacy callback, e.g. `(LPLinkMetadata?, Error?) -> Void` Raw
View Result+Legacy.swift
extension Result {
init(success: Success?, failure: Failure?) {
switch (success, failure) {
case (let success?, nil):
self = .success(success)
case (nil, let failure?):
self = .failure(failure)
case (_?, _?):
preconditionFailure("Unexpected Result.init with both success and failure")
case (nil, nil):
@JadenGeller
JadenGeller / Continuation+Legacy.swift
Created Feb 13, 2022
Resume a continuation from a legacy callback, e.g. `(LPLinkMetadata?, Error?) -> Void`
View Continuation+Legacy.swift
extension CheckedContinuation {
func resume(returning value: T?, throwing error: E?) {
switch (value, error) {
case (let value?, nil):
resume(returning: value)
case (nil, let error?):
resume(throwing: error)
case (_?, _?):
preconditionFailure("Unexpected resume with both returning and throwing")
case (nil, nil):
@JadenGeller
JadenGeller / LPLinkMetadata+FetchingAsync.swift
Last active Feb 13, 2022
Async function to fetch LPLinkMetadata
View LPLinkMetadata+FetchingAsync.swift
import LinkPresentation
extension LPLinkMetadata {
static func fetching(for url: URL) async throws -> LPLinkMetadata {
let provider = LPMetadataProvider()
return try await withTaskCancellationHandler {
try await withCheckedThrowingContinuation { continuation in
provider.startFetchingMetadata(for: url) { metadata, error in
switch (metadata, error) {
case (nil, let error?):
@JadenGeller
JadenGeller / Future+Legacy.swift
Last active Feb 13, 2022
Create a future from a legacy callback, e.g. `(LPLinkMetadata?, Error?) -> Void`
View Future+Legacy.swift
import Combine
extension Future {
static func legacy(_ attemptToFulfill: @escaping (@escaping (Output?, Failure?) -> Void) -> Void) -> Future {
Future { completion in
attemptToFulfill { output, error in
switch (output, error) {
case (nil, let error?):
completion(.failure(error))
case (let output?, nil):
@JadenGeller
JadenGeller / SubscriberView.swift
Created Feb 13, 2022
Render SwiftUI content view as a function of publisher's latest output
View SubscriberView.swift
import Combine
import SwiftUI
struct SubscriberView<Publisher: Combine.Publisher, Content: View>: View where Publisher.Failure == Never {
let publisher: Publishers.Share<Publisher>
let content: (Publisher.Output?) -> Content
init(_ publisher: Publisher, @ViewBuilder content: @escaping (Publisher.Output?) -> Content) {
// Apply `share` in `init` to prevent recreating the subscription from scratch when `latest` is set.
self.publisher = publisher.share()
View ResultBuilder.swift
@resultBuilder
enum ResultBuilder<Expression> {
case block([Self])
static func buildBlock(_ components: Self...) -> Self {
.block(components)
}
case expression(Expression)
static func buildExpression(_ expression: Expression) -> Self {
.expression(expression)
@JadenGeller
JadenGeller / GroupedString.swift
Last active Dec 20, 2021
Parses outermost paired delimiters, gracefully handling mismatches
View GroupedString.swift
// #import "https://gist.githubusercontent.com/JadenGeller/7a4ae11be08a6798002775bbd07887e3/raw/7187bf2c72cd8ab29d64a0eea54a90e7c979dc41/Abort.swift"
extension String {
enum GroupedString {
enum Element {
case character(Character)
case group(Substring)
}
case balanced([Element])
View Abort.swift
struct Abort<Failure>: Error {
var failure: Failure
init(_ failureType: Failure.Type, _ failure: Failure) {
self.failure = failure
}
enum Recovery<Success> {
case success(Success)
case failure(Failure)