Solitaire - assert and refactoring


#1

Hi clojurists

I have 2 questions about the serie of the solitaire game.

When card.clj is written, the function make uses assert to make some checks.

(defn make [suit value]
  (assert (contains? values value))
  (assert (contains? suits suit))
  [suit value])

Why not using :post and :pre?

(defn make [suit value]
  {:pre [(contains? suits suit)
         (contains? values value)]}
  [suit value])

Secondly, I would like to understand why an indirection has been added in the test? For example, instead of calling transfer-fn, call apply-move to redirect to transfer (via :move/transfer). github diff

Thanks a lot


#2

Hi @Lud,

Thanks for the questions! I love discussion.

No reason except that I didn’t want to explain them. Not everyone is aware of that syntax.

This one is a little harder to answer, and I’m a little concerned because I thought I addressed this. One path we explored was representing the move itself as data. It’s sometimes a useful indirection. For instance, it lets you do stuff like check if a move is valid. Having just the function, the only thing you can do with it is apply it.

Rock on!
Eric


#3
  1. Fair enough

  2. I looked at the video 9 (validating moves) today. Indeed it make more sense now. I come from the C++ world and a regular motto from this community is “you don’t pay for what you don’t use”. That’s why I asked this question.

Thanks @ericnormand