Skip to content

Instantly share code, notes, and snippets.

View tkersey's full-sized avatar
👹

Tim Kersey tkersey

👹
View GitHub Profile
@tkersey
tkersey / CoW.swift
Created July 8, 2022 02:12
Reducing stack costs of structs by using Copy on Write (CoW)
@dynamicMemberLookup
struct CoW<T> {
init(_ value: T) {
_storage = Storage(value)
}
public subscript<V>(dynamicMember keyPath: WritableKeyPath<T, V>) -> V {
get { value[keyPath: keyPath] }
set { value[keyPath: keyPath] = newValue }
}
var value: T {
@tkersey
tkersey / Moore.hs
Created March 6, 2022 23:31 — forked from ekmett/Moore.hs
Moore for Less
{-# LANGUAGE GADTs #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE UndecidableInstances #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
import Control.Applicative
import Control.Comonad
import Data.Bifunctor
@tkersey
tkersey / alltheflags.md
Created November 29, 2021 19:05 — forked from CodaFi/alltheflags.md
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
@tkersey
tkersey / Continuations.swift
Created November 29, 2021 18:46 — forked from CodaFi/Continuations.swift
A Swift Continuation Monad
// Playground - noun: a place where people can play
public func id<A>(x : A) -> A {
return x
}
public func error<A>(x : String) -> A {
assert(false, x)
}
@tkersey
tkersey / Y.swift
Created November 29, 2021 18:19 — forked from CodaFi/Y.swift
The Y Combinator in Swift
public func unsafeCoerce<A, B>(_ x : A) -> B {
return unsafeBitCast(x, to: B.self)
}
func Y<A>(_ f : @escaping (A) -> A) -> A {
return { (x : @escaping (A) -> A) in
return f((x(unsafeCoerce(x))))
}({ (x : A) -> A in
let fn : (A) -> A = unsafeCoerce(x)
return f(fn(x))

Copy and paste the swift code below into a playground to experiment.

This is a very close emulation of Functor and Monad typeclasses in swift. As of Swift 1.2 and Xcode 6.3, this is no longer very fragile.

Unfortunately, the compiler cannot verify the types when passing a function to (>>=). We have to wrap the function in a closure and call it with an explicit argument to compile.

optionalDoubles >>= squareRoot // doesn't compile
optionalDoubles >>= { squareRoot($0) } // compiles
@tkersey
tkersey / 1-Functor-and-Monad.md
Created November 22, 2021 22:06 — forked from mbrandonw/1-Functor-and-Monad.md
Swift Functor and Monad

Copy and paste the swift code below into a playground to experiment.

This is a very close emulation of Functor and Monad typeclasses in swift. However, it is very fragile (i.e. easy to crash the compiler).

For example, instance methods of fmap will run fine, but attempting to use a globally defined fmap that acts on Functor types will cause a crash. Similarly for bind. Unfortunately this means we cannot define the nice infix operator versions of these functions.

@tkersey
tkersey / gist:7bf5329bc307a2e3587a3a143fcae600
Created November 22, 2021 22:04 — forked from mbrandonw/gist:ba93c363f67291c2b5ec
First attempt at Functor protocol in Swift
protocol Functor {
func fmap <A, B> (Self<A>, A -> B) -> Self<B>
// ^ ERROR: Cannot specialize non-generic type '`Self`'
}
enum Maybe<A> : Functor {
case Nothing
case Just(A)
func fmap<B>(x: Maybe<A>, f: A -> B) -> Maybe<B> {
@tkersey
tkersey / Yoneda.swift
Created November 22, 2021 20:42 — forked from CodaFi/Yoneda.swift
//
// Yoneda.swift
// Basis
//
// Created by Robert Widmann on 12/11/14.
// Copyright (c) 2014 Robert Widmann. All rights reserved.
//
import Basis

[fit] Algebra to

[fit] (Co)monads


$$Cᴮᴬ = (Cᴮ)ᴬ$$


$$Cᴮᴬ = (Cᴮ)ᴬ$$

$$Pair&lt;A,B&gt; → C ≅ A → B → C$$