Skip to content

Instantly share code, notes, and snippets.

@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
@dduan
dduan / Makefile
Created Dec 29, 2017
"Hello, World!" In WebAssembly
View Makefile
compile:
wat2wasm main.wat -o main.wasm
serve:
python -m SimpleHTTPServer
@dduan
dduan / ADT.swift
Created Dec 18, 2017
Representing Algebraic Data Type?
View ADT.swift
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 Oct 29, 2017
Enum setter error reporting seems too simplistic?
View bug.swift
enum E {
final class P { var v = 0 }
case a(P)
var p: P {
switch self {
case .a(let p): return p
}
}
View remove-type-inference-for-stored-proprety.md

Remove type inference for stored properties

Introduction

When a type's stored property is declared with a default value, user may choose to let Swift's compiler to automatically deduct type for this property instead of writing it explicitly. We propose this feature be removed from Swift.

@dduan
dduan / runCommand.swift
Last active Apr 20, 2019
How to fork()+execv() in Swift
View runCommand.swift
import Foundation
func withCStrings(_ strings: [String], scoped: ([UnsafeMutablePointer<CChar>?]) throws -> Void) rethrows {
let cStrings = strings.map { strdup($0) }
try scoped(cStrings + [nil])
cStrings.forEach { free($0) }
}
enum RunCommandError: Error {
case WaitPIDError
@dduan
dduan / withCStrings.swift
Last active Aug 27, 2018
A function that turns a Swift 3 [String] to an array of C strings (char *[]).
View withCStrings.swift
func withCStrings(_ strings: [String], scoped: ([UnsafeMutablePointer<CChar>?]) throws -> Void) rethrows {
let cStrings = strings.map { strdup($0) }
try scoped(cStrings + [nil])
cStrings.forEach { free($0) }
}
@dduan
dduan / XCTest+Eventually.swift
Last active Jul 14, 2020
A simple "eventually" method to aide asynchronous testing with XCTest
View XCTest+Eventually.swift
import XCTest
extension XCTestCase {
/// Simple helper for asynchronous testing.
/// Usage in XCTestCase method:
/// func testSomething() {
/// doAsyncThings()
/// eventually {
/// /* XCTAssert goes here... */
You can’t perform that action at this time.