DEV Community

Discussion on: Don’t pay the for-loop tax

 
maxart2501 profile image
Massimo Artizzu

If that's still JavaScript (could be even C or Java), it could be refactored in a more expressive way:

const piece = chessboardRows.find(pieceAtColumn(column));
if (piece && isWhite(piece)) {
    GENERATE_MOVE;
}

function pieceAtColumn(column) {
    return function(row) {
        // something like row[column]?
    };
}
Enter fullscreen mode Exit fullscreen mode

Maybe it's more verbose, but also clearer.

Thread Thread
 
tbodt profile image
tbodt

This was C, I didn't have a find method on all the arrays.

Thread Thread
 
maxart2501 profile image
Massimo Artizzu

Ah, crud, you're out of luck then XD

I think the aim of this article is mainly JavaScript or any other language that support methods for functional programming.

I guess you're stuck with classic loops unless some kind of abstraction helps you with that (in Java <8 it's still a mess anyway).

Thread Thread
 
tbodt profile image
tbodt

I discovered that I pasted the code wrong, so I've edited the post to correct that. In the original version I could have written a function to find the index in the array, but I can't do that for the correct version.