Skip to content

Instantly share code, notes, and snippets.

@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
@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 / 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 / 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
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 / 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) }
@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 / IndexExpression.swift
Last active January 3, 2018 22:32
index expressions for collections
enum IndexType<C: Collection> {
case keypath(KeyPath<C, C.Index>)
case index(C.Index)
}
struct IndexExpression<C: Collection> {
let base: IndexType<C>
let distance: C.IndexDistance
func resolve(in collection: C) -> C.Index {
@karwa
karwa / IndexExpression2.swift
Last active January 3, 2018 23:10
Another idea for more convient slicing in Collections.
// Another idea for more convient slicing in Collections.
/// Lazily-resolved indexes, by their nature, are not really safe to use with a potentially-mutating collection because
/// their cached 'base' index might have been invalidated. If you want to use them in that situation, you have to ensure
/// they point to locations which will survive the mutation, then manually resolve them *before* mutating.
///
/// Or... you use the `Collection.easySlice()` method to do your slicing on a (hopefully) immutable copy
/// of the collection.
///