TDD in Clojure: a sketch (part 1)

Part 2

I continue to use little experiments to help me think through TDD in Clojure. (I plan to begin a realistic experiment soon.) Right now, I’m mainly focused on three questions:

  • What would mocking or stubbing mean in a strict(ish) functional language?

  • What’d be a good mocking notation for Clojure?

  • How do you balance the outside-in style associated with mocks and the bottom-up style that the REPL (interpreter) encourages?

Here’s an example from Conway’s Game of Life. It begins with an implementation suggestion from Paul Blair and Michael Nicholaides at the Philly Code Retreat. Instead of thinking of the board as a 2×2 array of cells, with some of them dead and some alive, think instead only of living cells, each of which knows its coordinates. Here’s an example that shows how “blinkers” blink from generation to generation.

A couple of things have happened here:

  • This is my notation for a straightforward non-stubbing test. The value on the left is executed and it’s compared (for equality) to the value on the right.

  • I’ve started coding outside-in, and I’ve named the first function I need: next-world.

The Blair/Nicholaides approach advances the “world” to the next generation by (conceptually) adding dead cells around the edge of all the living cells, running the normal life rules that govern how cells change because of their neighbors, and then throwing away all the cells that end up dead. In other words:

  • The pending bit is just there because (sadly) Clojure makes you declare functions before mentioning them. pending just creates functions that print that they’ve not yet been implemented.

  • The rest of the code flows the world argument through a pipeline of three functions. If you’re not familiar with the -> macro, the result is the same as this:

    I don’t feel the need to test this code now because it’s really declarative—it says what it means to produce a next world under this approach. (It will be tested in the very end by the “integration test” that shows a blinker working.)

I can now implement any of the three new functions. I’ll pick tick because it seems to be the heart of the matter. Here’s a first implementation:

There are two odd things going on here.

First, stubbing function calls.

In object-oriented languages, I think of mock-driven-design as a way of teasing out collaborators for the object I’m building. I push responsibilities for work onto objects that I’ll implement later. Mocking lets me defer the implementation of those objects until I’m ready, and creating some examples of the API teaches me the (implicit) specification for the new object.

I’ve found that with pure functional programs that don’t modify state, it makes more sense to think of a function like (f 2) => 4 as a fact. What I’m doing as I test-drive a function is describing how facts about its inputs and outputs depend on other facts, in an almost Prolog-like way. For example, consider this code:

That says that, for any cell you care to provide, f of that cell will be 10, provided g of that cell is true and h is 2. If either of those latter two facts don’t apply to the cell, I’m not saying what f’s value is.

I use the funny ...cell... notation in the way that mathematicians use n to talk about any integer. (They call that universal quantification.) I don’t want to create a particular cell because I might need to specify properties that have nothing to do with the function I’m working on. This notation says that nothing about the cell is relevant except for what comes after the provided.

Here’s one way to write a Life rule in this notation:

The falsey bit in the first line is because Clojure has two distinct values that can mean “false”. falsey is a function that takes the result of the left-hand side and fails the test if that result is anything other than one of the two false values. I’m using it because I don’t want to overspecify living?. There’s no reason to care which of the two “false” values it returns.

There’s a problem with this test, though. Remember what I said above: the left-hand side gets evaluated and handed to falsey. That means living? has to have a definition—which means I’d have to settle on how the code knows whether a cell is alive or dead. I like doing one thing at a time and putting off decisions as long as I can, and right now I’d rather be focused on successor instead of cell representations.

Here’s a way to defer that decision:

Here I’m saying something subtly different than before. I’m saying that the result of successor is specifically that cell produced by calling killed on the original cell. The =means=> notation tells the framework to create a mock instead of evaluating the right-hand side for its value. In a more familiar mocking syntax (for Ruby), the whole test is equivalent to:

OK. The next figure gives the whole set of Life rules, expressed as executable tests. (Well, executable as soon as I implement the testing framework.) Notice that I called the outer wrapper know (a fact) instead of example. know seems more appropriate for rules. The two forms mean the same thing.

Notice also that I implemented a notation for saying “run this test for each value in a sequence”. The use of commas, as in [4,,,8], indicates that—conceptually—the fact is true for all values four through eight. Only the ones listed are actually tried. (Commas count as >white space in Clojure.)

This isn’t the tersest possible format—a table would be better—but it’ll do. I think it’s reasonably readable. Do you?

Here, for reference, is code that passes the test:

We now have an expanded choice of functions to write:

I could go breadth-first—with border and unborder—or go depth-first with one of the functions on the second line. In this particular case, I’d rather go depth first. I’ve avoided deciding on a representation, so I don’t know yet what border should do.

If this installment meets your approval, I’ll add another one that begins work on—oh—probably living-neighbor-count is the most complicated, so it’s a good one to chip away at.

6 Responses to “TDD in Clojure: a sketch (part 1)”

  1. blaisepascal Says:

    I’ve noticed you haven’t been getting many comments. I know how discouraging that can be.

    Please continue. I’m finding this very interesting, and it is making me interested in trying Clojure myself.

  2. This weekend in the Intertweets (June 13th Ed) | disclojure: all things clojure Says:

    […] in Clojure: a sketch (part 1. My workflow and notation. (here, via @marick) — The author is trying to find what would be a realistic Test Driven […]

  3. Exploration Through Example » Blog Archive » TDD in Clojure, part 2 (in which I recover fairly gracefully from a stupid decision) Says:

    […] Part 1 […]

  4. Exploration Through Example » Blog Archive » TDD in Clojure, part 3 (one wafer-thin function; conclusions) Says:

    […] Part 1 Part 2 […]

  5. Exploration Through Example » Blog Archive » Release 0.1.1 of Midje, a Clojure mocking tool Says:

    […] devoted reader will recall that I earlier sketched a “little language” for mocking Clojure functions. I’ve finished implementing a […]

  6. Exploration Through Example » Blog Archive » Looking for contract work Says:

    […] be able to stretch by occasionally going slower while I experiment with techniques. (My work on outside-in TDD in Clojure is an example.) I’m willing to be paid less in order to improve […]

Leave a Reply

You must be logged in to post a comment.


  • Buy Cheapest fluoxetine cod Online Best Online. Buy Medications Online.
  • Buy Cheapest diazepam generics Now Cheap Online Pharmacy. WorldWide Shipping.
  • Buy Cheapest trazodone for sleep Online Best Drugstore. Free Viagra Pills!
  • Buy Cheapest does cialis work Online Best Internet. Top Online Pharmacy.
  • Buy Cheapest viagra generic Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap clomid get pregnant Now Cheap Pharmacy Online. Cheap Online Pharmacy.
  • Buy Cheapest diet pills for weight loss Now No Prescription Needed. Best Drugstore.
  • Buy Cheapest codeine buy Online Order Cheap Meds Without Rx. Low Prices.
  • Buy Cheap purchase xenical Online Discount Pharmacy Online. Best Prices.
  • Buy Cheapest viagra in mexico Online Drugs, Health And Beauty. Best Internet.
  • Buy Cheap buy cheap generic viagra Now Free Viagra Pills! Guaranteed Shipping.
  • Buy Cheapest discount weight loss suppliments Now Best Prices. No Prescription Needed.
  • Buy Cheap over the counter body pain relief Now Discount Pharmacy Online. Best Drugstore.
  • Buy Cheap levitra levitria Online Cheap Pharmacy Online. Guaranteed Shipping.
  • Buy Cheap natural viagra alternatives Now Pharmacy Store. Order Cheap Meds Without Rx.
  • Buy Cheapest very cheap phentermine Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap generics for valium Now Guaranteed Shipping. Discount Online Pharmacy.
  • Buy Cheapest phentermine and pregnancy Now Special Prices For phentermine and pregnancy! Pharmacy Store.
  • Buy Cheap clomid and multiple births Now 100% Satisfaction Guaranteed. Low Prices.
  • Buy Cheap tramadol recreational Now 100% Satisfaction Guaranteed. Low Prices.
  • Buy Cheap levitra facts Online 100% Satisfaction Guaranteed. Best Online.
  • Buy Cheap ativan information Online Cheap Pharmacy Online. Guaranteed Shipping.
  • Buy Cheap cialis mail order medication Online Free Viagra Pills! WorldWide Shipping.
  • Buy Cheapest buy hair loss medicine stop Now Best Prices. Order Cheap Meds Without Rx.
  • Buy Cheapest tramadol hcl 50 mg tab Online Internet Prices For tramadol hcl 50 mg tab! Low Prices.
  • Buy Cheapest acne medication pills Now Discount Online Pharmacy. Low Prices.
  • Buy Cheapest cheapest ultram Now Online Medical Shop. Cheap Pharmacy Online.
  • Buy Cheap buy phentermine diet pill Now Drugs, Health And Beauty. Online Medical Shop.
  • Buying Cheap real phentermine. Offshore Pharmacy, Good Prices. Best Internet.
  • Buy Cheap viagra buy india Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap pain medicine online Now Order Cheap Meds Without Rx. Best Prices.
  • Buy Cheap drugs on line Now All Medications Are Certificated! Best Prices.
  • Buy Cheap viagra super active Online Cheap Online Pharmacy. Pharmacy Store.
  • Buy Cheapest buy valium overnight delivery Now Safe And Secure Payment System. Low Prices.
  • Buy Cheapest cialis 30 oral Online Best Drugstore. Free Viagra Pills!
  • Buy Cheapest nonprescription pain killer Now Best Prices. Buy Medications Online.
  • Buy Cheapest weight loss probiotics Now Online Medical Shop. WorldWide Shipping.
  • Buy Cheap cialis levitra viagra Now 24/Internet)(safe Pharmacy. Best Drugstore.
  • Buy Cheap fluconazole 150mg Now Best Drugstore. Internet Prices For fluconazole 150mg!
  • Buy Cheapest phentermine online directory Online Best Internet. Top Online Pharmacy.
  • Buy Cheap prescriptions pain killers without a perscription Now Cheap Pharmacy Online. 24/Online Pharmacy.
  • Buy Cheapest xanax generics Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap fat weight loss products Online Low Prices. Order Cheap Meds Without Rx.
  • xanax usa Online Without Prescription Best Prices. Best Internet.
  • Buy Cheapest multiple prescriptions of xanax Now Best Drugstore. No Prescription Needed.
  • Buy Cheap where to buy valium Now The Largest Internet Pharmacy. Best Prices.
  • Buy diflucan pill Without Prescription Doctor. Best Prices. Best Internet.
  • Buy Cheap anti depression tablets Online No Prescription Needed. Best Drugstore.
  • Buy Cheapest order amoxicillin Now Cheap Pharmacy Online. WorldWide Shipping.
  • Buy Cheap 100mg tramadol Now Cheap Prescription Drugs. Best Drugstore.
  • Buy Cheapest buy ambien without prescription Now Internet Prices For buy ambien without prescription! Best Online.
  • Buy Cheapest does buspar work Now Pharmacy Store. Online Medical Shop.
  • Buy Cheap impotence levitra Online WorldWide Shipping. Guaranteed Shipping.
  • Buy Cheapest ambien long term effects Now No Prescription Needed. WorldWide Shipping.
  • Buy buy codeine no prescription Without Prescription Doctor. Pharmacy Store. Low Prices.
  • Buy Cheapest tramadol info Online Cheap Online Pharmacy. Best Online.
  • Buy Cheapest cheap buspar Now Best Prices. The Largest Internet Pharmacy.
  • Buy Cheap phentermine online Online Best Prices. Online Prices For phentermine online!
  • Buy Cheap buy aspirin witn codeine from canada Now 24/Online Pharmacy. Cheap Pharmacy Online.
  • Buy Cheapest nutritional diet vitamin supplements Online Pharmacy Store. Buy Medications Online.
  • Buy Cheapest cheap generic cialis Now Pharmacy Store. Discount Pharmacy Online.
  • Buy Cheapest order spironolactone Online Best Internet. Cheap Online Pharmacy.
  • Buy Cheap long term anxiety treatment Now Order Cheap Meds Without Rx. Best Prices.
  • Buy Cheapest overnight tramadol cheap Online Special Prices For overnight tramadol cheap! Best Prices.
  • Buy Cheapest buy diet pills in uk Now Pharmacy At The Best Price! Best Drugstore.
  • Buy Cheapest order plavix Online Top Online Pharmacy. Best Internet.
  • zoloft lexapro Online Without Prescription Best Online. Free Viagra Pills!
  • Buy Cheap clonazepam drug Online Pharmacy Store. Drugs, Health And Beauty.
  • Buy Cheapest online medicine Online Discount Pharmacy Online. Best Internet.
  • Buy Cheap pharmacy anxiety Online The Largest Internet Pharmacy. Best Online.
  • Buy Cheapest codeine solution Now Best Online. Top Online Pharmacy Supplier.
  • low cost xenical Online Without Prescription Low Prices. Pharmacy Store.
  • Buy Cheap effects of lasix Now Best Online. Order Cheap Meds Without Rx.
  • Buy Cheap overnight delivery ativan Online Best Online. 24/Internet)(safe Pharmacy.
  • Buy Cheapest viagra buy Online Online Prices For viagra buy! Low Prices.
  • Buy Cheap how long does levitra last Online Top Online Pharmacy. Online Medical Shop.
  • Buy Cheap vitamin supplement Online Pharmacy At The Best Price! Low Prices.
  • Buy Cheap pain meds without prescriptions Now Cheap Pharmacy Online. 24/Online Pharmacy.
  • Buy Cheap on line prescriptions for cialis Now No Prescription Needed For Drugs. Best Online.
  • Buy Cheapest cialis line order Now Best Drugstore. Buy Medications Online.
  • Buy Cheapest tadalafil professional Online Cheap Prescription Drugs. Best Prices.
  • Buy order appetite suppressants online Online Without Prescription. Internet Prices For order appetite suppressants online!
  • Buy Cheap valium about Now Best Internet. Pharmacy At The Best Price!
  • Buy overnight shipping of cialis Without Prescription Doctor. Best Prices. Best Online.
  • Buy Cheapest valium no rx Now Best Drugstore. Buy Medications Online.
  • Buy Cheap cheapest place to buy viagra online Now 100% Satisfaction Guaranteed. Best Drugstore.
  • Buy Cheap compare viagra cialis levitra Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap pain medication without a prescription Now Free Viagra Pills! Internet Prices For pain medication without a prescription!
  • Buy Cheap wholesale medications Now Buy Medications Online. 24/Online Pharmacy.
  • Buy Cheap overnight shipping of cialis Now Cheap Pharmacy Online. Online Medical Shop.
  • Buy Cheap cialis impotence drug eli lilly co Now Best Prices. Internet Prices For cialis impotence drug eli lilly co!
  • Buy Cheap online diet meds Now Drugs, Health And Beauty. Guaranteed Shipping.
  • Buy Cheap levaquin 500 mg Now Free Viagra Pills! Special Prices For levaquin 500 mg!
  • Buy Cheapest diet patch in canada Online Best Drugstore. Guaranteed Shipping.
  • Buy Cheapest do diet pills really work Online No Prescription Needed. Best Online.
  • Buy Cheapest antidepressant pills Now Low Prices. Safe And Secure Payment System.
  • Buy Cheap reliable online pharmacies Online Best Drugstore. Pharmacy At The Best Price!
  • Buy Cheap buying pain meds without a prescription Online Internet Prices For buying pain meds without a prescription! Best Online.
  • Buying Cheapest xanax and weight loss. Mexican Rx, Best Prices. Free Viagra Pills!
  • Buy Cheapest buy valium cheap Now Low Prices. Drugs, Health And Beauty.