Skip to content

Instantly share code, notes, and snippets.

@dduan
dduan / EscapingClosurePropertyWrapper.swift
Created Jul 28, 2020
This property wrapper guarantees that a closure does nothing if its enclosing object is deinitialized.
View EscapingClosurePropertyWrapper.swift
@propertyWrapper
struct Escaping {
typealias Closure = () -> Void
var store: Closure
var wrappedValue: Closure {
get { self.store }
set { self.store = newValue }
}
@dduan
dduan / SpeakBubble.swift
Last active Jun 20, 2020
Twitter's voice tweet UI has an interesting animation on iOS. This is an attempt to recreate that animation with SwiftUI. Looks like this https://youtu.be/I6XZzIgWYAQ
View SpeakBubble.swift
import SwiftUI
struct ChaoticPhoto: View {
let image: Image
let radius: CGFloat
@Binding var activated: Bool
@State var scale: CGFloat = 1
var body: some View {
image
.resizable()
@dduan
dduan / drstring.sh
Created Oct 15, 2019
Run DrString anywhere docker is available!
View drstring.sh
#!/bin/bash
temp_file=$(mktemp)
echo 'FROM swift@sha256:c4d53af406c5dc48bd43c0d313f3ed80924eee4bf78907ce4ad6eb8f5513f376' >> temp_file
echo 'RUN git clone https://github.com/dduan/DrString.git; cd DrString; make build; cp .build/release/drstring /bin/drstring' >> temp_file
echo 'RUN rm -rf /data' >> temp_file
echo 'ADD . /data' >> temp_file
echo 'WORKDIR /data' >> temp_file
echo 'ENTRYPOINT ["drstring"]' >> temp_file
@dduan
dduan / RemoveDuplicates.swift
Created Jul 30, 2019
Combine framework: remove duplicates by key paths.
View RemoveDuplicates.swift
import Combine
@available(OSX 10.15, iOS 13.0, tvOS 13.0, watchOS 6.0, *)
extension Publisher {
func removeDuplicates<Property>(by keyPath: KeyPath<Output, Property>)
-> Publishers.RemoveDuplicates<Self> where Property: Equatable
{
return self.removeDuplicates {
$0[keyPath: keyPath] == $1[keyPath: keyPath]
}
@dduan
dduan / BidirectionalCollection+firstIndex.swift
Last active Dec 28, 2018
Find first index of occurrence of a collection in a collection.
View BidirectionalCollection+firstIndex.swift
/// Usage: myString.firstIndex(of: otherString)
extension BidirectionalCollection where Element: Equatable {
func firstIndex(of other: Self) -> Index? {
guard
let start = other.first.flatMap(self.firstIndex(of:)),
self[start...].count >= other.count,
case let end = self.index(start, offsetBy: other.count),
zip(self[start ..< end], other).allSatisfy(==)
else
{
View Repro.swift
import Dispatch
class Runner {
var observers = [String: () -> Void]()
let queue = DispatchQueue(label: "foo-queue")
func run() {
let observers = self.observers
for _ in 0..<20000 {
let c: () -> Void = { [weak self] in
@dduan
dduan / fixup-each-staged-file.py
Last active Sep 18, 2018
Generate commit for each staged file, such that each commit is a `--fixup` to the commit said file was last changed.
View fixup-each-staged-file.py
#!/usr/bin/env python
"""
Generate commit for each staged file, such that each commit is a `--fixup` to
the commit said file was last changed.
NOTE: this command will unstage all files. It also does not disninguish staged
and unstaged potion of the same file.
USAGE: stage files you want to commit, run this command. Interactive rebase with
autosquash: `git rebase -i --autosquash BASE`
@dduan
dduan / SExpression.swift
Created Jul 28, 2018
A Generic S-Expression Parser
View SExpression.swift
indirect enum SExpression {
struct Token {
var index: Int
var content: String
}
enum Atom {
case string(Token)
case symbol(Token)
}
@dduan
dduan / JSON.swift
Last active Feb 8, 2018
A hand-made recursive descend JSON parser for fun.
View JSON.swift
enum State {
enum String {
case normal
case escape
case unicode(Int)
}
}
final class Context {
let contents: [UnicodeScalar]
@dduan
dduan / quiet.sh
Last active Jan 7, 2018
Run any command and suppress its output to stdin and stderr, unless it returns an error code.
View quiet.sh
#!/usr/bin/env bash
# Run any command and suppress its output to stdin and stderr, unless it returns an error code.
OUTPUT=$(${@:1} 2>&1)
if [ $? -eq 0 ]; then
exit
fi
OLDLFS=$LFS
You can’t perform that action at this time.