It’s Wednesday my dudes. You know what that means? IPM day. I turned in my tic-tac-toe game, but it was noticed that I didn’t have a proper way to handle when users input non-valid moves. If you put in a move that was not an available move on the board it would skip your turn and still let the computer make the next move. My video submission and speech on Java were both fine but Alex couldn’t sign off on my tic-tac-toe game with those bugs. He weighed his options on what to do, and said if I could hotfix it within 2 hours he’d sign off on it.

I got it fixed with 30 minutes to spare! It did take me a little bit longer than I expected, because I hadn’t accounted for there being an error if the user submitted a move that wasn’t a number. My program was automatically trying to turn the value into a number as soon as the user played it, which lead to a critical error. I decided the best path was to create a separate function that checked if the users input was valid. Instead of converting the users input to an integer and then comparing it to my list of moves (all integers), I decided to take my list of moves, and convert them to string, and check if the users input matched anything in the list.

But enough about my day, lets talk about my favorite function I learned about today.

zipmap

The zipmap function takes two ordered collections and creates a map of key-value pairs with the items of each list that share an index.

Example: (zipmap [:a :b :c :d] ‘(1 2 3 4)) => {:a 1 :b 2 :c 3 :d 4}

Things to watch out for:

  • If the two collections aren't the same size, it will only pair up to the highest index of the smallest collection.
  • If the keys are not all unique you may lose data, it will overwrite the first duplicate key with the value of the index of the last matching key.

While this may not be the most exciting function in the world, I feel like it’s just one of the tid bits that makes me enjoy Clojure even more.