Sveriges mest populära poddar

Functional Design in Clojure

Episode 004: Atomic Curls

23 min • 23 november 2018

Christoph tries to make tic-tac-toe work over the Internet and discovers the power of the atom.

  • Let's get a web framework, build a UI, hook that up to an HTTP API, throw in some websockets for notifications!
  • "We'd end up with our first 12 hour podcast episode."
  • A "tracer bullet": get something functioning to see the important parts of the solution.
  • Can choose when to replace important parts by something more "production worthy".
  • "Let's just ditch all the complexity of having a UI and just use curl."
  • curl will print the text response. We have a terminal UI!
  • "We're extending the command line out to the web!"
  • "I just keep curling every day or every other day until it's my turn."
  • "It's your morning curls!"
  • How do we handle web requests? What is a route?
  • Routes: /new, /show, /play?row=0&col=1
  • "Super simple URL API. Who needs REST?"
  • Let's run this on port 1337 to hide it from the Internet.
  • One shared game state stored in an atom. It's the only game in town!
  • Use an atom instead of a database to cut complexity while problem solving.
  • The ! ("bang") in swap! and reset! indicates you're causing a side effect.
  • Handler's sole job: take web requests and alter the game state using the game model.
  • Function called by swap! should be pure. Don't throw exceptions!
  • Dilemma: How much do you put inside the function called by "swap!"?
  • For errors, the transaction function can:
    • Return unchanged reference. Use "triage" function to diagnose.
    • Have an "error" attribute in the state and set an "outcome" code like :success,:invalid-coordinate, etc.
  • Tracer bullet shows lots of ways to complex-ify this into "Tic-Tac-Toe, Enterprise Edition"

Clojure in this episode:

Förekommer på
00:00 -00:00