i'm trying to model a game of poker.
My game state representation is in a map, for this question i'm only interested in the players.
{:players {:1 {:money 200} :2 {money 400}}}
Every function takes in a game state and some other parameters and return a new game state, for example:
(defn update-player-money
"Returns new game state where player has added amount to his money"
[game-state player amount]
(assoc-in game-state [:players player :money]
(+ (-> game-state :players player :money)
amount)))
Now i want a function that removes a certain amount of money from each player while passing down the new game state. To make it clear, something that for two players would do:
(update-player-money (update-player-money game-state :1 (- 20)) :2 (-20))
This is what i came up with:
(defn phase-1-blind
"Removes blind from all players."
[game-state blind-amount]
(letfn [(blind-helper [game-state player-list amount]
(if (seq player-list)
(blind-helper (update-player-money game-state
(first player-list)
(- amount))
(rest player-list)
amount)
game-state))]
(blind-helper game-state (keys (:players game-state)) blind-amount)))
This works but i was wondering if there was a more idiomatic or concise way to achieve the same effect.
update-in
? – Jochen Bedersdorferupdate-in
would certainly improve theupdate-player-money
function. Would there be a better alternative than writing the recursion by hand in the second function? – Marco Grassi:1
isn't idiomatic. Just use 1 – souenzzo