Skip to content

Instantly share code, notes, and snippets.

@JadenGeller
JadenGeller / $Parser.swift
Last active September 12, 2024 04:50
parsing custom attributed text format
import SwiftUI
struct AttributedStringBuilder {
@Stack var attributes: RichTextAttributes = .init()
var string: AttributedString = ""
mutating func text(_ span: String) {
string += AttributedString(span, attributes: .init(attributes))
}
@JadenGeller
JadenGeller / Stack.swift
Created September 6, 2024 20:49
push/pop state property wrapper
@propertyWrapper
struct Stack<Value> {
private var history: [Value]
init(wrappedValue initialValue: Value) {
self.history = [initialValue]
}
var wrappedValue: Value {
get { history[history.endIndex - 1] }

Markdown Extensions

Component Directives

  • Component Directive: A code block that defines a custom component.
  • Info String: Starts with a $COMPONENT-NAME, which is @ followed by an $IDENTIFIER (e.g., @component-name).
  • Front Matter: Enclosed within --- delimiters, specifies component attributes.
  • Nesting: Use more backticks for the parent block than the child, respecting CommonMark.
  • Content: Valid markdown, which may nest component directives.

Examples

@JadenGeller
JadenGeller / bfs.py
Last active July 3, 2024 02:51
itertools bfs
from itertools import zip_longest, chain
from dataclasses import dataclass, field
@dataclass
class Tree:
value: any
children: list = field(default_factory=list)
def levels(self):
yield [self]
struct RangeSliceCollection<Base: Collection, Ranges: Collection>: Collection where Ranges.Element == Range<Base.Index> {
var base: Base
var ranges: Ranges
var startIndex: Ranges.Index { ranges.startIndex }
var endIndex: Ranges.Index { ranges.endIndex }
func index(after i: Ranges.Index) -> Ranges.Index {
ranges.index(after: i)
}
extension Task where Success == Never, Failure == Never {
static let perpetual = Task {
await withCheckedContinuation { (_: CheckedContinuation<Void, Never>) in }
preconditionFailure()
}
}
@JadenGeller
JadenGeller / Versioned.swift
Last active September 6, 2024 20:49
useful property wrapper for prefix sums
@propertyWrapper
struct Versioned<Value>: RandomAccessCollection {
private var history: [Value]
var startIndex: Int { 0 }
var endIndex: Int { allValues.endIndex }
subscript(position: Int) -> Value {
get { allValues[position] }
set { allValues[position] = newValue }
@JadenGeller
JadenGeller / ToolbarAccessory.swift
Created May 7, 2024 02:19
Extend macOS toolbar downward in SwiftUI
import SwiftUI
extension View {
func toolbarAccessory(@ViewBuilder content: () -> some View) -> some View {
safeAreaInset(edge: .top) {
VStack(spacing: 0) {
content()
Divider()
}
.background(Material.bar, ignoresSafeAreaEdges: [.top])
func apportion(quota: Int, _ population: [Int]) -> [Int] {
var portions = population.map({ $0 / quota })
let errors = population.map({ $0 % quota })
let adjustments = (errors.reduce(0, +) + quota / 2) / quota
errors
.enumerated()
.sorted(by: { $0.element > $1.element })
.prefix(adjustments)
.forEach {
func lowestCommonAncestor<ID: Hashable>(of ids: (ID, ID), parent: (ID) -> ID?) -> ID? {
var ancestor: (ID?, ID?) = ids
var visited: (Set, Set) = ([ids.0], [ids.1])
while ancestor.0 != nil || ancestor.1 != nil {
if let ancestor = ancestor.0 {
guard !visited.1.contains(ancestor) else { return ancestor }
}
if let ancestor = ancestor.1 {
guard !visited.0.contains(ancestor) else { return ancestor }