Skip to content

Instantly share code, notes, and snippets.

View tkersey's full-sized avatar
👹

Tim Kersey tkersey

👹
View GitHub Profile
@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
@tkersey
tkersey / movie.md
Last active November 18, 2020 20:48 — forked from laser/movie.md

Movie Ticket Kata

What Are We Building?

Write a program that calculates purchase price for movie tickets using any language you like. It should not be a full-blown web app; it can be a simple class or collection of methods invokable by your test suite. We'll provide you with some requirements, test-cases, and even a sample interface - all you have to do is give us some software.

Base Admission Rate

The Base Admission Rate cover movies to be viewed on a regular weekday (see "Special Movie Day" below), in 2D, with a length of <= 120 minutes, viewed from the main seating area (there is also a balcony seating area, which is much fancier).

With scoped effects, handlers must be a part of the program

It is seductive to imagine that effect handlers in an algebraic effect system are not part of the program itself but metalanguage-level folds over the program tree. And in traditional free-like formulations, this is in fact the case. The Eff monad represents the program tree, which has only two cases:

data Eff effs a where
  Pure :: a -> Eff effs a
  Op :: Op effs a -> (a -> Eff effs b) -> Eff effs b

data Op effs a where
@tkersey
tkersey / libdispatch-efficiency-tips.md
Created November 2, 2020 02:17 — forked from tclementdev/libdispatch-efficiency-tips.md
Making efficient use of the libdispatch (GCD)

libdispatch efficiency tips

The libdispatch is one of the most misused API due to the way it was presented to us when it was introduced and for many years after that, and due to the confusing documentation and API. This page is a compilation of important things to know if you're going to use this library. Many references are available at the end of this document pointing to comments from Apple's very own libdispatch maintainer (Pierre Habouzit).

My take-aways are:

  • You should create very few, long-lived, well-defined queues. These queues should be seen as execution contexts in your program (gui, background work, ...) that benefit from executing in parallel. An important thing to note is that if these queues are all active at once, you will get as many threads running. In most apps, you probably do not need to create more than 3 or 4 queues.

  • Go serial first, and as you find performance bottle necks, measure why, and if concurrency helps, apply with care, always validating under system pressure. Reuse