How do I convert this JavaScript code to Clojure?
I am trying to draw a (x,y) world where the cells are on or off according to the fill property. In the example below I am trying to print the rows then columns but my next step is to move the fill property around (up, down, left, right). So, I don't want an answer which wouldn't work if I were not printing the data structure.
My goal is to understand how to think about this problem in a functional way. It was easy for me to solve this problem with mutable state in JavaScript. While working on the solution, I was able to easily reason about how to write the code in JavaScript but when I came to do the same in Clojure, I was at a loss. So, I feel like a good answer to this question would be to help me understand how to think about this kind of problem in a functional way.
I have watched many talks and read a bunch of articles about Clojure and functional programming but when it becomes to writing the code, it's difficult for me to get started.
var world = [
[{ x: 1, y: 4, fill:0 }, { x: 2, y: 4, fill:0 }, { x: 3, y: 4, fill:0 }, { x: 4, y: 4, fill:0 }],
[{ x: 1, y: 3, fill:0 }, { x: 2, y: 3, fill:0 }, { x: 3, y: 3, fill:0 }, { x: 4, y: 3, fill:0 }],
[{ x: 1, y: 2, fill:0 }, { x: 2, y: 2, fill:0 }, { x: 3, y: 2, fill:0 }, { x: 4, y: 2, fill:0 }],
[{ x: 1, y: 1, fill:0 }, { x: 2, y: 1, fill:0 }, { x: 3, y: 1, fill:1 }, { x: 4, y: 1, fill:0 }]
];
function printworld(world) {
var out = "";
for(var row=0; row<world.length; row++) {
for(var col=0; col<world[row].length; col++) {
out += "["+world[row][col].fill+"]";
}
out += "\n"
}
console.log(out);
}
printworld(world);
The output looks like:
[0][0][0][0]
[0][0][0][0]
[0][0][0][0]
[0][0][1][0]
EDIT: After spending time working through the problems at 4clojure.com, I realized that I was trying to bite off a larger problem (ex. a Chess game in Clojure) than I was ready to solve. Creating the most basic functions at 4clojure.com has been difficult but it's building a steady working knowledge on how to approach a solution using the functional method.