I think the only argument for using a a for-loop is to break out of the loop. I think pretty much everything else can be done using the functional approach.
Indeed, but as a reminder there are methods that allow you to "break out" soon looping an array with FP. Namely, find and some are probably the most expressive. (Alas, find isn't supported in IE, not even 11, but OTOH is easily polyfill-able.)
While I agree that breaking out of a loop is a point for the loops, breaking out of a for rubs me the wrong way. I believe you should use a while (or do-while) loops and specify the condition appropriately. Breaks hamper readability imho and make the code even harder to reason about or even formally verify.
I've written plenty of loops that would look a lot worse if I had to write them without break statements. For example, this code to look for possible forward moves for a white rook on a chessboard:
The first check could obviously be moved into the loop condition, but I can't think of a good way to move the second check into the loop condition too.
Edit: Language is C.
Edit 2: GENERATE_MOVE call was in the wrong place, moved it outside of the second if.
If that's still JavaScript (could be even C or Java), it could be refactored in a more expressive way:
constpiece=chessboardRows.find(pieceAtColumn(column));if(piece&&isWhite(piece)){GENERATE_MOVE;}functionpieceAtColumn(column){returnfunction(row){// something like row[column]?};}
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.
Oops, I pasted the code wrong. What I wanted to do was generate every move from the current position of the rook up to a piece, and including the piece if the piece is a black piece. See updated post.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I think the only argument for using a a for-loop is to break out of the loop. I think pretty much everything else can be done using the functional approach.
Indeed, but as a reminder there are methods that allow you to "break out" soon looping an array with FP. Namely,
find
andsome
are probably the most expressive. (Alas,find
isn't supported in IE, not even 11, but OTOH is easily polyfill-able.)While I agree that breaking out of a loop is a point for the loops, breaking out of a for rubs me the wrong way. I believe you should use a while (or do-while) loops and specify the condition appropriately. Breaks hamper readability imho and make the code even harder to reason about or even formally verify.
I've written plenty of loops that would look a lot worse if I had to write them without break statements. For example, this code to look for possible forward moves for a white rook on a chessboard:
The first check could obviously be moved into the loop condition, but I can't think of a good way to move the second check into the loop condition too.
Edit: Language is C.
Edit 2: GENERATE_MOVE call was in the wrong place, moved it outside of the second if.
If that's still JavaScript (could be even C or Java), it could be refactored in a more expressive way:
Maybe it's more verbose, but also clearer.
This was C, I didn't have a find method on all the arrays.
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).
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.
I agree the loop would look worse. However, in this case all you are doing is finding the first piece and if it's white calling a method, right?
So you could do something like
where the
findFirstPiece
would find the firstrow
value that is either a white or a black piece, returning -1 if no such value was found.Oops, I pasted the code wrong. What I wanted to do was generate every move from the current position of the rook up to a piece, and including the piece if the piece is a black piece. See updated post.