Skip to content

Instantly share code, notes, and snippets.

View seadiem's full-sized avatar

seadiem

  • Saints-Petersburg, Russia
View GitHub Profile
@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 {
@DougGregor
DougGregor / preventing-data-races.md
Created December 20, 2020 06:49
Preventing Data Races in the Swift Concurrency Model

Preventing Data Races in the Swift Concurrency Model

One of the goals of the concurrency effort is to prevent data races. This document describes the approach taken to preventing data races overall, by categorizing the sources of data races and describing how they are addressed with other proposals in the Swift Concurrency effort.

Data races

A data race occurs when two threads access the same memory concurrently and at least one of the accesses can change the value. Within the safe subset of Swift (e.g., ignoring the use of UnsafeMutablePointer and related types), the memory in question is always a stored property. There are several different categories of stored properties that need to be considered for data races:

  • Global and static stored properties:
@dabrahams
dabrahams / CollectionAdapter.swift
Last active July 17, 2020 09:33
Motivation for non-public conformances providing public API
/// A collection formed by adapting some `Base` collection, typically
/// displaying some altered form of the base collection's behavior.
///
/// Default implementations of all requirements are provided, forwarding their
/// implementations to the `Base` collection instance.
public protocol CollectionAdapter: Collection {
/// The type of the underlying collection being adapted.
associatedtype Base: Collection
/// The instance of the underlying collection being adapted.
@dabrahams
dabrahams / StaticArrayInsertRemove.swift
Last active September 7, 2022 06:20
Static Array With Functional Insert and Remove Operations
// The point of this prototype is to prove that we can generate efficient code
// for single-element insertion and deletion on a statically-sized array, with
// stable types for the end products i.e.,
//
// type(of: a.removing(at: i).inserting(x, at: j)) == type(of: a)
//
// and
//
// type(of: a.inserting(x, at: j).removing(at: i)) == type(of: a)
import Combine
import SwiftUI
@available(iOS 13.0, OSX 10.15, tvOS 13.0, watchOS 6.0, *)
@propertyWrapper
public struct Model<Value>: DynamicProperty {
final class _Box: ObservableObject {
let objectWillChange = ObservableObjectPublisher()
var value: Value {
@airspeedswift
airspeedswift / Bounded.swift
Created October 26, 2018 04:33
Bound a sequence with a start and end marker
struct BoundedSequence<Base: Sequence> {
let _base: Base
}
extension BoundedSequence {
struct Iterator {
enum State { case starting, iterating, ended }
var _state: State
var _iterator: Base.Iterator
}
@dabrahams
dabrahams / Algorithms.swift
Created June 8, 2018 20:27
Heap extensions to standalone Algorithms prototype
//===--- Algorithms.swift.gyb ---------------------------------*- swift -*-===//
//
// 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
//
@bellbind
bellbind / Shaders.metal
Last active October 26, 2021 16:02
[swift][osx] Metal programming with commandline (Xcode less)
//-*- mode: c++ -*-
// build:
// xcrun -sdk macosx metal -std=osx-metal1.1 Shaders.metal -o shaders.air
// xcrun -sdk macosx metal-ar rcs Shaders.metalar Shaders.air
// xcrun -sdk macosx metallib -o Shaders.metallib Shaders.metalar
#include <metal_stdlib>
kernel void square(const device float* input [[buffer(0)]],
device float* output [[buffer(1)]],
metal::uint id [[thread_position_in_grid]]) {
@randomsequence
randomsequence / cocoa-drawing.swift
Created July 14, 2015 15:25
Drawing images with CGContext and NSGraphicsContext in Swift
//: Playground - noun: a place where people can play
import Cocoa
let bounds = CGRectMake(0, 0, 100, 100);
func DrawImageInCGContext(#size: CGSize, #drawFunc: (context: CGContextRef) -> ()) -> NSImage {
let colorSpace = CGColorSpaceCreateDeviceRGB()
let bitmapInfo = CGBitmapInfo(CGImageAlphaInfo.PremultipliedLast.rawValue)
let context = CGBitmapContextCreate(
@ZevEisenberg
ZevEisenberg / resetAllSimulators.sh
Last active September 15, 2024 02:52
Reset all iOS simulators with this one weird trick
osascript -e 'tell application "iOS Simulator" to quit'
osascript -e 'tell application "Simulator" to quit'
xcrun simctl erase all