This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Test to check sample distribution. Written by Codex. | |
/// Prompt: | |
/// Write unit tests for NormalizedGaussian with the goal of checking that it generates random values with a guassian normal distribution. | |
import XCTest | |
@testable import FinLib | |
final class NormalizedGaussianTests: XCTestCase { | |
private let sampleCount = 200_000 | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public actor TaskBag { | |
private var tasks: [UUID: Task<Void, Never>] = [:] | |
func add(_ task: Task<Void, Never>) { | |
let id = UUID() | |
tasks[id] = Task { [weak self] in | |
await task.value | |
await self?.remove(id: id) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import lldb | |
# put this in ~/.lldb/stack_usage/stack_usage.py. | |
# Load in .lldbinit with `command script import "~/.lldb/stack_usage/stack_usage.py"` | |
# At a breakpoint, you can then type `stack_usage` to get the current stats. | |
@lldb.command("stack_usage") | |
def _stack_usage(debugger, command, result, internal_dict): | |
"""Show current stack usage information""" | |
target = debugger.GetSelectedTarget() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! env python3 | |
import os | |
import re | |
import subprocess | |
import argparse | |
parser = argparse.ArgumentParser( | |
description="Process .o files to output stack allocation info." | |
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Testing | |
import Combine | |
// From https://stackoverflow.com/questions/78892734/getting-task-isolated-value-of-type-async-passed-as-a-strongly-trans/78899940#78899940 | |
public final class AsyncFuture<Output, Failure: Error>: Publisher, Sendable { | |
public typealias Promise = @Sendable (Result<Output, Failure>) -> Void | |
private let work: @Sendable (@escaping Promise) async -> Void | |
public init(_ work: @Sendable @escaping (@escaping Promise) async -> Void) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Built with Swift 6 (Xcode 16b1) | |
actor Accumulator { | |
static let shared = Accumulator() | |
var values: [String] = [] | |
// A couple of approaches, neither of which works. | |
func add(value: String) { values.append(value) } | |
nonisolated func addSync(value: String) { Task { await add(value: value) } } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Version of Swift 6 Mutex, with minimal API (just withLock), portable to at least iOS 15, probably much earlier. | |
// I cannot yet promise it's actually correct. | |
@frozen | |
public struct Mutex<Value> { | |
private let buffer: ManagedBuffer<Value, os_unfair_lock> | |
public init(_ initialValue: Value) { | |
buffer = ManagedBuffer<Value, os_unfair_lock>.create(minimumCapacity: 1) { _ in initialValue } | |
buffer.withUnsafeMutablePointerToElements { lockPtr in |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// In regards to https://mastodon.social/@mattiem/112285978801305971 | |
// MainActor class with synchronous methods | |
@MainActor final class M { | |
func methodA() {} | |
func methodB() {} | |
} | |
// Actor that relies on M. | |
actor A { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/// Some exploration into how selector-based notification interact with actors. | |
/// | |
/// Or in the words of Brent Simmons (@[email protected]), | |
/// "Selector-based Notification Observers Are Actually Good" | |
/// Overall, I'm reasonably convinced, in that it avoids the headaches of `deinit` in actors. | |
/// However, Combine-based observation is also good at this, so I don't yet have a strong opinion | |
/// about old-school selectors vs Combine beyond my usual nervousness around Combine. | |
/// Whether "I'd like to reduce Combine" is more or less powerful than "I'd like to reduce @objc" | |
/// is yet to be seen. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// With Strict Concurrency | |
class C {} | |
struct S { | |
func f() { | |
Task { // Surprisingly need `@MainActor in` here to make this correct | |
await g() // Warning: Passing argument of non-sendable type 'C' into main actor-isolated context may introduce data races | |
} | |
} |
NewerOlder