Skip to content

Instantly share code, notes, and snippets.

@karwa
karwa / codablebridge.swift
Created November 1, 2017 04:57
codable-nscoding bridge
import Foundation
/// This isn't safe to use before Swift gets ABI stability, because generic classes
/// could change their names. Also, be sure to register bridges with the Obj-C runtime
/// if using to decode during iOS state restoration.
///
class CodableBridge<Wrapped: Codable>: NSObject, NSSecureCoding {
let value: Wrapped
init(_ value: Wrapped) { self.value = value }
@karwa
karwa / virtualkeypath.swift
Created June 10, 2017 02:09
VirtualKeyPath
struct VirtualKeyPath<Root, Value> {
private let block: (Root) -> Value
init(block: @escaping (Root) -> Value) { self.block = block }
}
// Evaluation
extension VirtualKeyPath {
func evaluate(on: Root) -> Value { return block(on) }
extension Collection {
/// Repeats the collection _itself_ N times.
///
/// ```
/// > [1, 2, 3].repeated(3).forEach { print($0) }
/// [1, 2, 3]
/// [1, 2, 3]
/// [1, 2, 3]
@karwa
karwa / ConcurrentCollection.swift
Last active July 16, 2020 11:00
Concurrent collection wrapper
import Dispatch
// Import C11 for atomic_flag
// FIXME: SWIFT(canImport)
//#if canImport(Glibc)
// import Glibc.stdatomic
//#elseif canImport(Darwin)
import Darwin.C.stdatomic
//#endif
@karwa
karwa / Swift4.md
Last active January 7, 2017 05:51
Ideas for Swift 4

Specialising based on parameter values

The compiler would statically optimise a function with particular values, and generate an entry point which checks for the values and runs the optimised implementations.

@_specialize(value: 42)
// Some function on our hot path
func someFunction(value: Int) -> Result? {
  if value == 42 {
 // magic value. Do some special processing.
@karwa
karwa / SE-xxx.md
Last active June 30, 2016 00:33
Add integral rounding functions to FloatingPoint

Add integral rounding functions to FloatingPoint

Introduction, Motivation

The standard library lacks equivalents to the floor() and ceil() functions found in the standard libraries of most other languages. Currently, we need to import Darwin or Glibc in order to access the C standard library versions.

@karwa
karwa / make_sysroot.py
Last active April 19, 2023 08:32
Creates a linux sysroot with packages required for swift (requires dpkg-deb)
#!/usr/bin/env python
import argparse
import sys
import gzip
import urllib
import urlparse
import posixpath
import cStringIO
import os