Skip to content

Instantly share code, notes, and snippets.

View mklbtz's full-sized avatar

Michael Bates mklbtz

View GitHub Profile
View parse-dont-validate.swift
// From:
// Original:
/* Haskell
head :: [a] -> a
head (x:_) = x
func head<A>(_ array: [A]) -> A {
return array[0] // Crashes at runtime when array is empty.
mklbtz / Swift.sublime-build
Last active August 21, 2019 19:37
Use Xcode to run Swift CLI, file regex included
View Swift.sublime-build
"selector": "source.swift",
"shell_cmd": "/Applications/ \"${file}\"",
"file_regex": "^(.+\\.swift):([0-9]+):?([0-9]+)?:? (?:error|warning|note): (.*)$"
mklbtz /
Last active December 20, 2018 18:26
Things I already wish I had in Python
from __future__ import print_function
class NoneError(TypeError):
class maybe:
A two-state monad that represents the presents or absence of a value.
Think of it as a collection of zero or one values.
mklbtz / Collection+random.swift
Last active November 3, 2018 17:33
Implements "random" properties for collections and ranges
View Collection+random.swift
import Darwin
extension RandomAccessCollection {
// Fails if collection is larger than UInt32.max
var randomIndex: Index {
guard !isEmpty else { return endIndex }
let fullDistance = UInt32(distance(from: startIndex, to: endIndex))
let randomDistance = IndexDistance(arc4random_uniform(fullDistance))
return index(startIndex, offsetBy: randomDistance)
mklbtz /
Last active October 8, 2018 16:08
Stopwatch - A simple class for taking time hacks.
from datetime import datetime, timedelta
import sys
class Stopwatch:
def __init__(self):
self._hacks = []
def laps(self):
return len(self._hacks) - 1
mklbtz / displays.scpt
Created September 26, 2018 13:15
AppleScript that will open System Preferences and rotate displays to a preconfigured orientation
View displays.scpt
on launchDisplaysPane()
tell application "System Preferences"
set displayPane to pane ""
set displayTab to anchor named "displaysDisplayTab" of displayPane
reveal displayTab
delay 0.5
end tell
end launchDisplaysPane
mklbtz / SketchSystems.spec
Last active June 1, 2018 16:00
No Opportunities*
View SketchSystems.spec
No Opportunities*
parts uploaded -> Estimate Requested
Estimate Requested
Reviewing Parts*
redact prints -> Reviewing Parts
upload dummies -> Reviewing Parts
all parts clean -> Clean
mklbtz /
Last active May 22, 2018 05:14
Some Unix-y command line tools written in Swift

In the course of time, I found myself wanting to run RSpec automatically in response to file changes. I found a nice tool called fswatch to handle the file events, but I wanted a "smarter" pipeline. Following the Unix philosophy, I wrote a few tiny command line tools which I've provided the source to.

  • specname — maps a file in a rails project to its corresponding spec file.
  • debounce — makes sure multiple rapid saves don't trigger as many RSpec runs.
  • throttle — could be used to collect multiple file changes into one RSpec run, but I haven't figured that last part out yet.

The core functions are fully generic and have no dependencies on anything but Foundation, so they could be easily incorporated into a larger project if there's a use for them.

mklbtz / Final-Tagless Views.rb
Created May 21, 2018 22:15
Demonstrating the "final tagless" approach to the "Expression Problem" in Ruby
View Final-Tagless Views.rb
class View
def self.image(url:) { |img| img.url = url }
def self.button(title:) { |btn| btn.title = title }
def self.stack(subviews)
mklbtz /
Created March 24, 2018 19:13
SE Proposal: New literal for Data from contents of file

New literal for Data from contents of file

  • Proposal: SE-NNNN
  • Authors: Michael Bates
  • Review Manager: TBD
  • Status: Awaiting implementation


In Swift today, literals give us the ability to embed files, images, and colors directly into our programs. This proposes a new literal that will embed the contents of a file as Data. The proposed syntax is…