Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clojure pmap returning different results than map

I am learning clojure, and implementing my standard test project, Tic Tac Toe 10. I have written the same AI in many languages before and have had problems scaling it beyond 6 moves ahead in the other languages also.

I got the AI algorithm basically working, but I'm trying to improve the speed with pmap. Since everything is immutable, I should be able to just drop in pmap in place of map and get the same results, but I'm not seeing that.

(defn get-spot
  [board player win-cond levels]
  (def avail (get-available board))
  (def final
    (apply merge
           (map #(array-map % (calc-score board player win-cond levels %)) avail)))
  final)

But pmap in that spot returns inconsistent results. Not sure where to start looking. I can post more of the code if it's needed.

like image 950
Brian Baritonehands Gregg Avatar asked Apr 17 '26 13:04

Brian Baritonehands Gregg


1 Answers

Replacing def with let everywhere solved the problem. My functions had lots of inconsistent side effects if I didn't use let.

(defn get-spot
  [board player win-cond levels]
  (let [avail (get-available board)
        final (apply merge
           (pmap #(array-map % (calc-score board player win-cond levels %)) avail))]
  final))

I've written a bunch of clojure code, and read through many tutorials. I don't know how I missed that really important detail.

like image 82
Brian Baritonehands Gregg Avatar answered Apr 19 '26 16:04

Brian Baritonehands Gregg