Skip to content

Instantly share code, notes, and snippets.

View PimCoumans's full-sized avatar
thinking about making coffee

Pim PimCoumans

thinking about making coffee
View GitHub Profile
@ollieatkinson
ollieatkinson / Tuple.swift
Last active April 22, 2024 15:13
Implementation of `Tuple` type using Swift's new parameter packs
public struct Tuple<each T> {
public private(set) var value: (repeat each T)
public init(_ value: repeat each T) { self.value = (repeat each value) }
}
extension Tuple {
public func map<each U>(
_ transform: (repeat each T) throws -> (repeat each U)
) rethrows -> (repeat each U) {
@moyerr
moyerr / AnyAsyncSequence.swift
Created May 17, 2023 17:54
A type-erased AsyncSequence
struct AnyAsyncSequence<Element>: AsyncSequence {
struct AnyAsyncIterator: AsyncIteratorProtocol {
private let _next: () async throws -> Element?
init<Base: AsyncSequence>(_ base: Base) where Base.Element == Element {
var baseIterator = base.makeAsyncIterator()
self._next = { try await baseIterator.next() }
}
func next() async throws -> Element? {
extension Result {
public func `catch`(_ handler: () throws -> Success) -> Result<Success, Error> {
flatMapError { _ in
.init { try handler() }
}
}
public func `catch`(_ handler: (Failure) throws -> Success) -> Result<Success, Error> {
flatMapError { error in
.init { try handler(error) }
import UIKit
import PlaygroundSupport
import CoreGraphics
extension Numeric {
/// Linear interpolation
/// - Parameters:
/// - a: a value to interpolate from
/// - b: a value to interpolate to
/// - delta: interpolation delta value (0 - 1)
@AliSoftware
AliSoftware / Bindings.swift
Last active July 9, 2024 22:02
Re-implementation of @binding and @State (from SwiftUI) myself to better understand it
/*:
This is a concept re-implementation of the @Binding and @State property wrappers from SwiftUI
The only purpose of this code is to implement those wrappers myself
just to understand how they work internally and why they are needed,
⚠️ This is not supposed to be a reference implementation nor cover all
subtleties of the real Binding and State types.
The only purpose of this playground is to show how re-implementing
them myself has helped me understand the whole thing better
@adamgraham
adamgraham / UIColor+CIELCh.swift
Last active December 3, 2023 10:20
An extension of the iOS class UIColor to provide conversion to and from CIELCh° colors.
/// An extension to provide conversion to and from CIELCh° colors.
extension UIColor {
/// The CIELCh° components of a color - lightness (L), chroma (C), and hue (h).
struct CIELCh: Hashable {
/// The lightness component of the color, in the range [0, 100] (darkest to brightest).
var L: CGFloat
/// The chroma component of the color.
var C: CGFloat
@douglashill
douglashill / KeyboardTableView.swift
Last active March 30, 2023 22:01
A UITableView that allows navigation and selection using a hardware keyboard.
// Douglas Hill, December 2018
// Made for https://douglashill.co/reading-app/
// Find the latest version of this file at https://github.com/douglashill/KeyboardKit
import UIKit
/// A table view that allows navigation and selection using a hardware keyboard.
/// Only supports a single section.
class KeyboardTableView: UITableView {
// These properties may be set or overridden to provide discoverability titles for key commands.
@lattner
lattner / TaskConcurrencyManifesto.md
Last active July 15, 2024 01:20
Swift Concurrency Manifesto
@endavid
endavid / FontAtlas.swift
Created May 27, 2017 18:03
Signed Distance Field in Metal/Swift
public class FontAtlas: NSObject, NSSecureCoding {
public static var supportsSecureCoding: Bool { get { return true } }
static let atlasSize: Int = 2048 // 4096 runs out of mem...
var glyphs : [GlyphDescriptor] = []
let parentFont: UIFont
var fontPointSize: CGFloat
let textureSize: Int
var textureData: [UInt8] = []
@Koze
Koze / PhotosScreenshot.m
Last active November 30, 2022 22:12
Getting All Screenshots with Photos.framework
NSMutableArray *mArray = [NSMutableArray array];
// fetch all image assets
PHFetchOptions *fetchOptions = [[PHFetchOptions alloc] init];
fetchOptions.predicate = [NSPredicate predicateWithFormat:@"mediaType == %d", PHAssetMediaTypeImage];
PHFetchResult *result = [PHAsset fetchAssetsWithOptions:fetchOptions];
[result enumerateObjectsUsingBlock:^(PHAsset * __nonnull asset, NSUInteger idx, BOOL * __nonnull stop) {
// filter with subtype for screenshot
if (asset.mediaSubtypes & PHAssetMediaSubtypePhotoScreenshot) {
[mArray addObject:asset];