Sveriges mest populära poddar

Functional Design in Clojure

Episode 003: Tic-Tac-REPL

23 min • 16 november 2018

Nate tries to turn the tic-tac-toe "game engine" into a real application he can play with a friend.

  • Let's play the game!
  • How do you keep track of the game state as it changes?
  • Bucket brigade the reference to the future loop using recur
  • The game loop: read input, evaluate, print out new board, loop.
  • "It has occurred to me that we are basically writing a REPL."
  • "We have the tic-tac-REPL"
  • How do you get input from the user? How to you make sure it's right?
  • "It keeps harassing the non-compliment user until they type the right thing in"
  • Input loop: read, validate, loop on error, return on success
  • Keep the logic pure! Separate out the parsing and validation functions.
  • "Much better to tuck it away in a function!"
  • Sequence the pure parts with a minimalist function that does the I/O.
  • "I don't like having ovens present because they're hard to put in my test cases."
  • Unit test those pure parts. (No one likes to be mocked.)
  • I/O is a side effect!
  • "Every time I redefine one of those things I feel like I'm reaching down into the bowels of Clojure and doing something moderately illegal."
  • Using keywords as error codes is nifty
  • "You don't have any different kinds of nil. You just have one. It's the nuh-uh."
  • Can use a tuple with the first element always being a keyword and the second being data for the "details"

Clojure in this episode:

  • read-line
  • string/split
  • swap! and reset!
  • loop and recur
  • let vs loop
  • keywords
  • nil punning
Förekommer på
00:00 -00:00