Skip to content

Instantly share code, notes, and snippets.

View bkase's full-sized avatar

Brandon Kase bkase

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / 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 / OlegSharing.swift
Created June 14, 2017 04:38
An exploration of Sharing (Oleg) focusing on Hashconsing and Sklansky
// An exploration of http://okmij.org/ftp/tagless-final/sharing/sharing.pdf
// Specifically focusing on Hashconsing and Sklansky
// See http://swift.sandbox.bluemix.net/#/repl/5940bd55698d8d064dec2b22 for this snippet in a playground
typealias NodeId = Int
enum Node: Equatable, Hashable {
case nconst(Int)
case nvar(String)
case nadd(NodeId, NodeId)
@bkase
bkase / Animation.swift
Created October 6, 2017 08:34
Rough Sketch Functional Animations
// SUPER ROUGH ideas about distributive property on Animations
// This is the "current relative time as input" rather than the "0 to 1 as input" (unit) version of the animation
// this has flaws, but simple to show (or not) the distributive property of sequence and parallel
infix operator <>: AdditionPrecedence
protocol Semigroup {
static func <>(lhs: Self, rhs: Self) -> Self
}
indirect enum FreeSemigroup<A>: Semigroup {
@bkase
bkase / example.re
Last active December 1, 2017 23:04
Reason React Native: Workaround to get jsThis from Reason-React
module Example {
type jsProps = {. x: int, y: string };
type state = ReasonReact.stateless;
type props = ReasonReact.noRetainedProps;
type action = ReasonReact.actionless;
let component = ReasonReact.statelessComponent("Example");
let make = (jsThis: Withjs.thisType) => (~x, ~y) => (_children) => {
@bkase
bkase / override-method-return.swift
Created January 5, 2018 05:56
Fun with extension-method-return-type overriding (port from Swift Sandbox)
protocol Base { }
extension Base {
var a: String? { return nil }
var b: Int? { return nil }
}
protocol A: Base {
var a: String { get }
}