Skip to content

Instantly share code, notes, and snippets.

@dduan
dduan / RemoveDuplicates.swift
Created July 30, 2019 19:00
Combine framework: remove duplicates by key paths.
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 December 28, 2018 23:21
Find first index of occurrence of a collection in a collection.
/// 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
{
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 September 18, 2018 05:24
Generate commit for each staged file, such that each commit is a `--fixup` to the commit said file was last changed.
#!/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 July 28, 2018 21:00
A Generic S-Expression Parser
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 February 8, 2018 08:52
A hand-made recursive descend JSON parser for fun.
enum State {
enum String {
case normal
case escape
case unicode(Int)
}
}
final class Context {
let contents: [UnicodeScalar]
@dduan
dduan / quiet.sh
Last active January 7, 2018 06:34
Run any command and suppress its output to stdin and stderr, unless it returns an error code.
#!/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
@dduan
dduan / Makefile
Created December 29, 2017 04:13
"Hello, World!" In WebAssembly
compile:
wat2wasm main.wat -o main.wasm
serve:
python -m SimpleHTTPServer
@dduan
dduan / ADT.swift
Created December 18, 2017 19:27
Representing Algebraic Data Type?
struct Decl {
struct Option {
enum Segment {
case one(String, [String])
indirect case map(Segment, Segment)
}
typealias Field = (name: String?, type: Segment)
let name: String
let fields: [Field]
}
@dduan
dduan / bug.swift
Created October 29, 2017 05:21
Enum setter error reporting seems too simplistic?
enum E {
final class P { var v = 0 }
case a(P)
var p: P {
switch self {
case .a(let p): return p
}
}