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:
qsort [] = [] | |
qsort (x:xs) = qsort less ++ [x] ++ qsort more | |
where less = filter (<x) xs | |
more = filter (>=x) xs |
# 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) |
I hereby claim:
To claim this, I am signing this object:
// | |
// CollectionViewDataSource.swift | |
// Khan Academy | |
// | |
// Created by Andy Matuschak on 10/14/14. | |
// Copyright (c) 2014 Khan Academy. All rights reserved. | |
// | |
import UIKit |
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) | |
} |
// | |
// 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 | |
} |
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,