Skip to content

Instantly share code, notes, and snippets.

View andymatuschak's full-sized avatar

Andy Matuschak andymatuschak

View GitHub Profile
qsort [] = []
qsort (x:xs) = qsort less ++ [x] ++ qsort more
where less = filter (<x) xs
more = filter (>=x) xs
@andymatuschak
andymatuschak / gist:475014
Created July 14, 2010 04:10
Ruby expression values and a lesson on newlines
# In Ruby, every expression (even ifs) has a value!
print 'What is your name? '
name = gets.chomp
name_response = if name == 'Suzanne'
then 'Oh man, ' + name + ' you are awesome'
else 'You, ' + name + ', are not nearly as cool as Suzanne.'
end
print name_response
# Suppose I want to the user to enter names one at a time, until the user enters a blank line,
# then tell each name that they're awesome.
# Yeah, I don't have really a better way of doing that, and that bothers me. I'd do:
a = []
until (str = gets.chomp?).empty?
a << str
end
import Data.List (sort)
import Data.Array (Array, array, (!))
deckSize = 52
eachColor = 26
-- Some convenience types:
type History = (Int, Int) -- # of red cards seen, # of black cards seen
data Choice = Stop | Continue deriving (Show, Eq)
data Decision = Decision { choice :: Choice, expectedWin :: Double } deriving (Show, Eq)

Keybase proof

I hereby claim:

  • I am andymatuschak on github.
  • I am andymatuschak (https://keybase.io/andymatuschak) on keybase.
  • I have a public key whose fingerprint is 289E CB4D BCC9 5D3A 71BD 6795 14CB 9F07 992C D348

To claim this, I am signing this object:

@andymatuschak
andymatuschak / CollectionViewDataSource.swift
Last active February 12, 2021 09:44
Type-safe value-oriented collection view data source
//
// CollectionViewDataSource.swift
// Khan Academy
//
// Created by Andy Matuschak on 10/14/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
@andymatuschak
andymatuschak / gist:2b311461caf740f5726f
Created December 28, 2014 18:17
A pragmatic and intentionally non-abstract solution to JSON decoding / initialization that doesn't require learning about five new operators.
struct User {
let id: Int
let name: String
let email: String?
}
extension User: JSONDecodable {
static func create(id: Int, name: String, email: String?) -> User {
return User(id: id, name: name, email: email)
}
@andymatuschak
andymatuschak / MultiDirectionAdjudicatingScrollView.swift
Created January 26, 2015 19:31
Source for the Khan Academy app's unusual scrolling interactions
//
// MultiDirectionAdjudicatingScrollView.swift
// Khan Academy
//
// Created by Andy Matuschak on 12/16/14.
// Copyright (c) 2014 Khan Academy. All rights reserved.
//
import UIKit
import UIKit.UIGestureRecognizerSubclass
// One note before we start: if the inhabitants of Value are a closed set,
// then making Value an enum is going to be a clearer model than using a
// protocol like this. I'm assuming you need to be able to retroactively add
// new members of Value.
// Can't use Equatable directly because of its Self requirement.
protocol HeteroEquatable {
func isEqualTo(value: HeteroEquatable) -> Bool
}
@andymatuschak
andymatuschak / States-v3.md
Last active May 1, 2024 12:32
A composable pattern for pure state machines with effects (draft v3)

A composable pattern for pure state machines with effects

State machines are everywhere in interactive systems, but they're rarely defined clearly and explicitly. Given some big blob of code including implicit state machines, which transitions are possible and under what conditions? What effects take place on what transitions?

There are existing design patterns for state machines, but all the patterns I've seen complect side effects with the structure of the state machine itself. Instances of these patterns are difficult to test without mocking, and they end up with more dependencies. Worse, the classic patterns compose poorly: hierarchical state machines are typically not straightforward extensions. The functional programming world has solutions, but they don't transpose neatly enough to be broadly usable in mainstream languages.

Here I present a composable pattern for pure state machiness with effects,