查找下面的命令行式風格的代碼:這種命令式編碼風格的等效Scala代碼是什麼?
private boolean placePieceOnBoard(Results results,
ChessLayout chessLayout,
List<ChessPiece> piecesToPlace,
int pieceIndex,
int startOffset) {
if(pieceIndex == piecesToPlace.size()) {
results.addLayout(chessLayout);
return true;
}
else {
ChessPiece chessPiece = piecesToPlace.get(pieceIndex);
int offset = startOffset;
while(offset < chessLayout.getBoardLength()) {
int placedOffset = chessLayout.placePieceInNextAvailablePosition(chessPiece, offset);
if(placedOffset == ChessLayout.NULL_OFFSET)
break;
else {
logger.debug("piece {} ({}) moved", pieceIndex, chessPiece);
placePieceOnBoard(results, chessLayout.clone(), piecesToPlace, pieceIndex + 1, placedOffset + 1);
chessLayout.removeChessPiece(chessPiece);
offset = placedOffset + 1;
}
}
return false;
}
}
在這裏,我想知道如何作出一個功能性的方式在循環中的偏移量的變化轉換,如何從循環等
結果與ChessLayout看怎麼樣? –
請將您的代碼片段首先轉換爲Scala,這是您至少可以做到的!在函數式編程中,您不會中斷,您可以讓函數終止/返回。該策略必須創建一個將偏移量作爲參數並返回新偏移量值的函數。 – jmelanson