Skip to content

Instantly share code, notes, and snippets.

View dan-zheng's full-sized avatar
💭

Dan Zheng dan-zheng

💭
View GitHub Profile
@DougGregor
DougGregor / dynamic_member_lookup_environment.swift
Created May 2, 2018 16:59
Using Swift 4.2's @dynamicMemberLookup to expose environment variables
import Darwin
@dynamicMemberLookup
struct Environment {
subscript(dynamicMember name: String) -> String? {
get {
guard let value = getenv(name) else { return nil }
return String(validatingUTF8: value)
}
@regexident
regexident / GenericsManifesto.md
Created November 14, 2017 09:16
Draft for extended section on generic protocols in Swift Raw

Generic protocols

One of the most commonly requested features is the ability to parameterize protocols themselves. For example, a protocol that indicates that the Self type can be constructed from some specified type T or converted into T:

// T -> Self:
protocol ConstructibleFrom<T> {
  init(from value: T)
}
@Azoy
Azoy / Random.swift
Last active November 13, 2017 11:01
Swift Random Unification Design
public protocol RandomSource {
static var shared: RandomSource { get }
func next<T : FixedWidthInteger>(_ type: T.Type) -> T
func nextUniform<T : BinaryFloatingPoint>(_ type: T.Type) -> T
}
// Utilizes /dev/urandom
public class DevRandom: RandomSource {
public static let shared = DevRandom()
@lattner
lattner / TaskConcurrencyManifesto.md
Last active June 20, 2026 07:56
Swift Concurrency Manifesto
@rxwei
rxwei / staged-y-combinator.swift
Last active July 23, 2017 10:10
Staged Y combinator with NNKit
/// Y combinator
func fix<D, R>(
_ f: @escaping (Rep<(D) -> R>) -> Rep<(D) -> R>) -> Rep<(D) -> R> {
return lambda { d in f(fix(f))[d] }
}
let fac: Rep<(Int) -> Int> = fix { f in
lambda { (n: Rep<Int>) in
.if(n == 0, then: ^1, else: n * f[n - 1])
}
@CTMacUser
CTMacUser / NNNN-fixed-size-arrays.md
Last active April 28, 2021 17:37
Swift proposal for fixed-size array types
@bittlingmayer
bittlingmayer / fasttext_similarity.py
Created June 10, 2017 12:50
Similarity for two files output by fastText print-word-vectors or print-sentence-vectors
"""
Takes two files produced by fastText's print-word-vectors or print-sentence-vectors and compares the vectors by similarity.
(See https://github.com/facebookresearch/fastText.)
This can be useful for benchmarking output or even generating benchmark data.
For example:
@joshbuchea
joshbuchea / semantic-commit-messages.md
Last active August 17, 2026 23:02
Semantic Commit Messages

Semantic Commit Messages

See how a minor change to your commit message style can make you a better programmer.

Format: <type>(<scope>): <subject>

<scope> is optional

Example

@threepointone
threepointone / glam-for-css-folks.md
Last active January 5, 2026 04:03
why css purists will love glam

I made a little styling lib called glam

(some features are in development)

one

let's start off with the simplest use case. we'll make an 'index.html' page, and assume we've setup our js bundler to output bundle.js

@rxwei
rxwei / GADT.swift
Last active November 28, 2024 19:15
GADT in Swift
/// A type equality guarantee is capable of safely casting one value to a
/// different type. It can only be created when `S` and `T` are statically known
/// to be equal.
struct TypeEqualityGuarantee<S, T> {
private init() {}
/// Safely cast a value to the other type.
func cast(_ value: T) -> S {
return value as! S
}