Skip to content

Instantly share code, notes, and snippets.

@CodaFi
CodaFi / AlgorithmW.swift
Last active March 31, 2024 23:25
A Swift Playground containing Martin Grabmüller's "Algorithm W Step-by-Step"
/// Playground - noun: a place where people can play
/// I am the very model of a modern Judgement General
//: # Algorithm W
//: In this playground we develop a complete implementation of the classic
//: algorithm W for Hindley-Milner polymorphic type inference in Swift.
//: ## Introduction
@CodaFi
CodaFi / Nat.swift
Last active January 6, 2024 07:23
An encoding of recursion schemes (à "Functional Programming with Bananas, Lenses, Envelopes and Barbed Wire" [Meijer, Fokkinga, Paterson] ~( http://www.eliza.ch/doc/meijer91functional.pdf )) in Swift.
final class Nat<F> : FunctorOf<F> {
let pred : Optional<F>
override init() {
self.pred = nil
}
init(pred : F) {
self.pred = pred
}

Motivation

Swift’s type system supports a number of different ways of taking a function or type and abstracting it. Usually, this is done by adding a generic parameter and an associated set of constraints. Similarly, a function that takes a particular type of argument can be abstracted to any number of those arguments by making it variadic with triple-dot (...) syntax. Today, both of these features are permitted separately: you can define a generic function that takes a variable number of arguments, such as

func debugPrint<T>(_ items: T...) 
  where T: CustomDebugStringConvertible
{   
  for (item: T) in items {
    stdout.write(item.debugDescription)
@CodaFi
CodaFi / Aviary.swift
Last active October 23, 2023 20:32
Fly my pretties
// Playground - noun: a place where people can play
// I wouldn't want a pair of birds that were... too demonstrative.
func idiot<A>(a : A) -> A {
return a
}
func kestrel<A, B>(a : A) -> B -> A {
return { _ in a }
@CodaFi
CodaFi / Free.swift
Created September 20, 2014 19:20
Free Monads in Swift
//
// Free.swift
// Swift_Extras
//
// Created by Robert Widmann on 9/19/14.
// Copyright (c) 2014 Robert Widmann. All rights reserved.
//
import Foundation
@CodaFi
CodaFi / Proposal.md
Last active October 12, 2023 00:03
@CodaFi
CodaFi / IP.swift
Last active June 3, 2023 05:32
An async sequence of kevents
#if canImport(Darwin)
import Darwin
#elseif canImport(Glibc)
import Glibc
#else
#error("unsupported platform")
#endif
/// Core utilities for interacting with IP-based abstractions.
public struct IP {
@CodaFi
CodaFi / aviary.m
Last active May 5, 2023 12:09
I know why the caged bird sings.
// cc aviary.m -o aviary -framework Foundation -fobjc-arc && ./aviary
#import <Foundation/Foundation.h>
// use it like lambda(…args…)(…return value…)
#define lambda(...) \
^ (__VA_ARGS__) _lambda_body
#define _lambda_body(...) \
{ return __VA_ARGS__; }

Deprecate @UIApplicationMain and @NSApplicationMain

Introduction

@UIApplicationMain and @NSApplicationMain used to be the standard way for iOS and macOS apps respectively to declare a synthesized platform-specific entrypoint for an app. These functions have since been obsoleted by SE-0281 and the @main attribute, and now represent a confusing bit of duplication in the language. This proposal seeks to deprecate these alternative entrypoint attributes in favor of @main in Swift 5.8, and makes their use in Swift 6 a hard error.

@CodaFi
CodaFi / alltheflags.md
Last active October 26, 2022 20:09
Every Option and Flag /swift (1.2) Accepts Ever

#Every Single Option Under The Sun

  • optimization level options
  • automatic crashing options
  • debug info options
  • swift internal options
  • swift debug/development internal options
  • linker-specific options
  • mode options