Doseq
Refactoring, refactoring, refactoring. That’s what my day consisted of. I think refactoring is one of my favorite parts of coding now. It sounds like such a boring task, but there is something satisfying about taking a large function and whittling it down and removing any duplications. It’s like a puzzle inside the puzzle you’ve already solved.
While refactoring my code for my proof to client that my get-best-move mini-max function is truly picking the best possible move, I came across the subject of today’s lesson. While I didn’t end up using it in my proof, it was fun playing around with and think it will come in handy in the future.
Doseq
(doseq seq-exprs & body)
Repeatedly executes body (presumably for side effects) with bindings and filtering as provided by “for”. Does not retain the head of the sequence. Returns nil.
The doseq function is the closest function I’ve found to Javascript’s .forEach array method. Both are used to produce
side effects (in my case I was using println, and I’ve often used it in javascript with console.log). I tried using it
to print the result and move-set of every possible game combination, but came to the realization that would be way too
much data for the client to sift through so opted for an “all tests pass” approach.
Doseq can be used by initializing variables inside of brackets (what you want to call the individual item of the sequence you feed in), and giving it a function you want performed each time. It’s similar the map function in the sense that it does it to every value in the sequence, but it differs in the fact that doseq returns nil after applying its function to every value.