Skip to content

Instantly share code, notes, and snippets.

View bkase's full-sized avatar

Brandon Kase bkase

View GitHub Profile
@bkase
bkase / capture-hidden-type-variable-variant.md
Last active May 19, 2017 05:35
How to capture hidden type variables in a variant in Swift?

Optparse-applicative takes an interesting approach to commandline parsing. A parser is data that captures the structure of applicative and monadic combinators so that you can inspect that structure and interpret it later. Modeling this in Swift is hard.

I want to be able to model something like this:

indirect enum Parser<T> {
   case NilP(v: T?)
   case AltP(p1: Parser<T>, p2: Parser<T>)
   case BindP<I>(p: Parser<I>, f: (I) -> Parser<T>)
 /* ... */
@bkase
bkase / Effectful.swift
Created April 29, 2017 22:28
Effectful programming with callbacks
/*
* A callback-base implementation of effectful computation that defers interpretation
* until after computation is expressed (so you can inject different interpreters
* onto the same computation).
*
* The downside is the results aren't expressed as values. They are callback calls.
* You probably don't actually want to use this, but it seems interesting to think about.
*
* Inspired by "Programming with Algebraic Effects and Handlers" by
* Andrej Bauer and Matija Pretnar 2012
@bkase
bkase / BKOneOf.h
Last active August 2, 2017 11:53
Algebraic Data Types in Objective-C with Macros
//
// BKOneOf.h
// BKOneOf
//
#ifndef BKOneOf_h
#define BKOneOf_h
/*
* Interface and Implementation separate:
@bkase
bkase / lazycollection-indexing-behavior.md
Last active March 12, 2017 08:33
LazyCollection behavior when indexing directly via subscript

I received a great question over email that I want to answer publicly via gist:

At the end [of the Grokking Lazy Sequences and Collections talk], someone asked you if they can just access a lazy collection randomly and it will not run the transform (map for example) on every element in the collection up to the one they chose.

You said that is correct, but I think that it only applies if the Collection is a RandomAccessCollection? Otherwise, access is not random and is sequential instead, so the iterator’s next() method will be called on each element up to the one requested, and that transform will be executed on each one.

Actually, my response is correct! Confusingly, Collection does support subscript random access (with an expected O(1) runtime in fact) if you have an index. RandomAccessCollection conformance implies that you can compute the distance between indicies in O(1) time (aka moving an index is fast).

If yo

@bkase
bkase / Arity.swift
Last active June 4, 2016 05:41
Get function arity statically (up to 24 parameters)
func arity<A,R>(f: A -> R) -> Int {
return 1
}
func arity<A,B,R>(f: (A, B) -> R) -> Int {
return 2
}
func arity<A,B,C,R>(f: (A, B, C) -> R) -> Int {
return 3
}
func arity<A,B,C,D,R>(f: (A, B, C, D) -> R) -> Int {
@bkase
bkase / typeclassplay.scala
Created April 23, 2016 09:03
Playing with typeclasses in Scala
import simulacrum._
@typeclass trait CanTruthy[A] { self =>
@op("!!") def truthy(a: A): Boolean
}
object Main extends App {
import CanTruthy.ops._
implicit val truthyInt =
@bkase
bkase / prnsaaspfruicc.md
Created March 3, 2016 22:58
Production-Ready(?) Native Single-Atom-State Purely Functional Reactive Composable UI Components, or PRNSAASPFRUICC

Production-Ready(?) Native Single-Atom-State Purely Functional Reactive Composable UI Components, or PRNSAASPFRUICC

The native UI toolkits on Android and iOS are frustratingly imperative. Unfortunately, there has been little architecture evolution (unlike the web). However, nothing(ish) is stopping us from bolting something "nice" on top. By nice I mean single-atom-state purely functional reactive composable UI components. In this post, I'll explain what this title means, the inspiration behind the design of the framework, an example component, and dive a little into the framework's implementation.

Right now, there only exists a Kotlin (Android) implementation, but a Swift port will be relatively straightforward and will happen soon.

Let's break down the title:

  • "Production-Ready"
@bkase
bkase / apps_com.example.appname_BUCK
Last active February 24, 2020 02:00
Kotlin Buck build files
android_binary(
name = 'app',
manifest = 'AndroidManifest.xml',
keystore = ':debug_keystore',
use_split_dex = True,
primary_dex_patterns = [
'^com/tryroll/roll/AppShell^',
'^com/tryroll/roll/debug/AppShell^',
'^com/tryroll/roll/BuildConfig^',
@bkase
bkase / gist:8afc20abbbe5730b8b3a
Created December 10, 2014 22:53
Assertion code in swift std library
/// User code assertions.
///
/// User code assertions and fatal errors are only enabled in debug mode. In
/// release or fast mode these checks are disabled. This means they may have no
/// effect on program semantics, depending on the assert configuration.
/// Traditional C-style assert with an optional message.
///
/// When assertions are enabled and `condition` is false, stop program
/// execution in a debuggable state after printing a message. When
/// assertions are disabled in release and fast builds, `condition` is not even
@bkase
bkase / gist:847351601b8c3a3df99d
Created August 20, 2014 04:31
Hang in Xcode beta 6
import Foundation
public class A<T> {
var callbacks: [A<T> -> ()] = []
public init() {
}
}