Skip to content

Instantly share code, notes, and snippets.

@DougGregor
DougGregor / parallel_map.swift
Created December 24, 2020 01:10
Swift async/await implementation of a parallel map
extension Collection {
func parallelMap<T>(
parallelism requestedParallelism: Int? = nil,
_ transform: @escaping (Element) async throws -> T
) async throws -> [T] {
let defaultParallelism = 2
let parallelism = requestedParallelism ?? defaultParallelism
let n = self.count
if n == 0 {
@IanKeen
IanKeen / Example_Complex.swift
Last active September 10, 2024 11:53
PropertyWrapper: @transaction binding for SwiftUI to make changes to data supporting commit/rollback
struct User: Equatable {
var firstName: String
var lastName: String
}
@main
struct MyApp: App {
@State var value = User(firstName: "", lastName: "")
@State var showEdit = false
#import <Foundation/Foundation.h>
#import <CoreText/CoreText.h>
int main(int argc, const char * argv[]) {
@autoreleasepool {
CTFontRef font = CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, 12, NULL);
{
CTFontRef fallback = CTFontCreateForStringWithLanguage(font, CFSTR("\u4E2D"), CFRangeMake(0, 1), CFSTR("zh-Hans"));
CFCharacterSetRef covered = CTFontCopyCharacterSet(fallback);
bool isMember = CFCharacterSetIsCharacterMember(covered, 0x61);
////===--- EitherSequence.swift - A sequence type-erasing two sequences -----===//
////
//// This source file is part of the Swift.org open source project
////
//// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
//// Licensed under Apache License v2.0 with Runtime Library Exception
////
//// See https://swift.org/LICENSE.txt for license information
//// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
////
@greenchiu
greenchiu / Promise.swift
Last active March 26, 2020 13:49
Practice Promise
import Foundation
enum PromiseError: Error {
case invaild
}
public struct AsyncExecutor {
public static let main = AsyncExecutor()
public static let background = AsyncExecutor(qos: .default)
private let queue: DispatchQueue
@greenchiu
greenchiu / Finding_smallest_common_element_in_two_arrays.swift
Last active December 4, 2019 07:47
Finding smallest common element in two arrays
func Solution(_ nums1:[Int], _ nums2: [Int]) -> Int? {
let set: Set<Int> = Set(nums1)
var result: Int?
for element in nums2 {
if set.contains(element) {
if let currentMin = result {
result = min(element, currentMin)
continue
}
result = element
@stephanecopin
stephanecopin / RFC2368EmailSupport.swift
Last active November 7, 2023 16:32
A small set of struct/extensions to easily handle `mailto:` links in Swift 5.1.
import Foundation
import MessageUI
struct EmailParameters {
/// Guaranteed to be non-empty
let toEmails: [String]
let ccEmails: [String]
let bccEmails: [String]
let subject: String?
let body: String?
@pofat
pofat / closure.swift
Created September 29, 2019 08:16
Discuss how closure capturing and capture list works
import Foundation
// struct for printing out instance address
struct MemoryAddress<T>: CustomStringConvertible {
let intValue: Int
var description: String {
let length = 2 + 2 * MemoryLayout<UnsafeRawPointer>.size
return String(format: "%0\(length)p", intValue)
}
@simme
simme / debounce-throttle.swift
Created December 20, 2016 10:04
Swift 3 debounce & throttle
//
// debounce-throttle.swift
//
// Created by Simon Ljungberg on 19/12/16.
// License: MIT
//
import Foundation
extension TimeInterval {
@billy3321
billy3321 / Markdown Cheatsheet 中文版.md
Last active April 15, 2025 15:49
Markdown Cheatsheet 中文版