Skip to content

Instantly share code, notes, and snippets.

View tkersey's full-sized avatar
👹

Tim Kersey tkersey

👹
View GitHub Profile
@rnapier
rnapier / MainActorRun.swift
Last active April 23, 2024 13:29
Regarding MainActor.run
// 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 {
@rnapier
rnapier / DefaultValueConcurrency.swift
Last active April 12, 2024 01:30
Somewhat surprising impact of non-Sendable default values
// 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
}
}
@OrionReed
OrionReed / dom3d.js
Last active April 23, 2024 14:46
3D DOM viewer, copy-paste this into your console to visualise the DOM topographically.
// 3D Dom viewer, copy-paste this into your console to visualise the DOM as a stack of solid blocks.
// You can also minify and save it as a bookmarklet (https://www.freecodecamp.org/news/what-are-bookmarklets/)
(() => {
const SHOW_SIDES = false; // color sides of DOM nodes?
const COLOR_SURFACE = true; // color tops of DOM nodes?
const COLOR_RANDOM = false; // randomise color?
const COLOR_HUE = 190; // hue in HSL (https://hslpicker.com)
const MAX_ROTATION = 180; // set to 360 to rotate all the way round
const THICKNESS = 20; // thickness of layers
const DISTANCE = 10000; // ¯\\_(ツ)_/¯
@airspeedswift
airspeedswift / list.swift
Created March 23, 2024 18:48
A Basic Noncopyable Singly Linked List in Swift
// To run this code on a Mac with Xcode installed:
// Download the latest toolchain from https://www.swift.org/download/#trunk-development-main and install it
// From a command line:
// export TOOLCHAINS=`plutil -extract CFBundleIdentifier raw -o - /Library/Developer/Toolchains/swift-latest.xctoolchain/Info.plist`
// xcrun swiftc -parse-as-library -enable-experimental-feature NoncopyableGenerics -enable-experimental-feature MoveOnlyPartialConsumption -Xfrontend -disable-round-trip-debug-types -enable-experimental-feature BorrowingSwitch linkedlist.swift
struct Box<Wrapped: ~Copyable>: ~Copyable {
private let pointer: UnsafeMutablePointer<Wrapped>
import SwiftUI
import AsyncAlgorithms
struct AsyncChanges<V>: ViewModifier where V : Equatable, V: Sendable {
typealias Element = (oldValue: V, newValue: V)
typealias Action = (AsyncStream<Element>) async -> Void
@State private var streamPair = AsyncStream<Element>.makeStream()
private let action: Action
private let value: V
@tkersey
tkersey / ReducedReplayAsyncStream.swift
Created March 14, 2024 06:27 — forked from ABridoux/ReducedReplayAsyncStream.swift
An AsyncSequence that allows to be consumed several times. Returning the current state as specified in a reduce function
struct ReducedReplayAsyncStream<Element> {
typealias Reduce = (_ partialResult: inout Element, _ nextResult: Element) -> Void
private let storage: _Storage
private var originalStream: AsyncStream<Element>
init(
bufferingPolicy limit: AsyncStream<Element>.Continuation.BufferingPolicy = .unbounded,
initialResult: Element,
@sjoerdvisscher
sjoerdvisscher / FeedbackMonad.hs
Last active February 22, 2024 13:36
From Lenses to Composable Continuations, and what lies between (Bob Atkey)
-- https://www.youtube.com/watch?v=YpklMn5yNA0
{-# LANGUAGE RankNTypes, QuantifiedConstraints #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE GADTs #-}
import Prelude (id, (.), ($), Functor(..), (<$>), fst)
import Data.Functor.Identity (Identity(..))
import Data.Void
import Data.Bifunctor (second)
class Feedback m where
@ole
ole / swift-has-feature.sh
Last active April 23, 2024 08:23
List Swift compiler upcoming and experimental feature flags. (Note: the second script below, swift-list-features.sh, is probably the more useful one of the two. Unfortunately, I can't reorder them.)
#!/bin/zsh
# Test if the Swift compiler knows about a particular language feature.
#
# Usage:
#
# swift-has-feature [--swift SWIFT_PATH] FEATURE
#
# The exit code signals success (= the compiler knows this feature) or failure.
#
@KhaosT
KhaosT / Keyboard and Trackpad for Apple Vision Pro.md
Last active March 20, 2024 20:31
Keyboard & Trackpad for Apple Vision Pro

Keyboard & Trackpad for Apple Vision Pro

Photo of a stack of keyboards

Over the last few months, in preparation for Apple Vision Pro, I started a quest to find a good portable keyboard + trackpad setup for the headset. After testing out a wide range of those from the market, here are my findings. Hope it helps people looking for the same.

The impression is based on testing out the keyboard/trackpad with iPadOS, as it shares the similar infrastructure for external input support as visionOS.

After testing on real hardware, visionOS disabled support for any non-Apple trackpad... So feel free to ignore everything about non-Apple trackpad here.

@tkersey
tkersey / iOSMemoryDeepDive.md
Created January 23, 2024 18:14
iOS Memory Deep Dive
  • vmmap --summary X.memgraph
  • vmmap X.memgraph | rg "MEMORY REGION NAME"
  • vmmap --verbose X.memgraph | rg "MEMORYREGION"
  • leaks --traceTree 0xSTARTINGMEMORYADDRESS
  • malloc_history X.memgraph --fullStacks 0xSTARTINGMEMORYADRESS
  • Other helpful commands
    • vmmap --pages X.memgraph
    • leaks X.memgraph
  • heap X.memgraph