Comment by bccdee
3 months ago
I think the important thing is that the code look pure from a testing perspective.
Say you've got a function that accesses a key-value store. Ideally, you can factor out the i/o so that you do all your reads up front and all your writes at the end, leaving a pure function in the middle. But if the code is too tangled up in its side effects for that, the next best thing is to create a fake KV store and then wrap the function like this:
def doTest(input, initialState):
kv = makeFake(initialState)
result = doThing(kv, input)
return result, kv.dumpContents()
doThing isn't a pure function, but doTest is. Now you can write tests like this:
result, outputState = doTest(input, initialState)
assert (result, outputState) == expected
I guess you could call that "imperative core, functional shell," lol.
No comments yet
Contribute on Hacker News ↗