There is a old Java code (without lambda expressions):
public List<CheckerPosition> getAttackedCheckersForPoint(CheckerPosition from, boolean isSecondPlayerOwner, boolean isQueen, VectorDirection ignoredDirection){
List<VectorDirection> allDirections = VectorDirection.generateAllDirections();
List<CheckerPosition> result = new ArrayList<CheckerPosition>();
for (VectorDirection direction : allDirections){
if (!direction.equals(ignoredDirection)){
Checker firstCheckerOnWay = findFirstCheckerOnWay(new CheckerBaseVector(from, direction), !isQueen);
if ((firstCheckerOnWay != null) && (firstCheckerOnWay.isSecondPlayerOwner() != isSecondPlayerOwner) && isCheckerBlocked(firstCheckerOnWay.getPosition(), direction)){
result.add(firstCheckerOnWay.getPosition());
}
}
}
return result;
}
I'm trying to rewrite this code to Java 8 Stream API style:
allDirections.stream()
.filter(d -> !d.equals(ignoredDirection))
.map(d -> findFirstCheckerOnWay(new CheckerBaseVector(from, d), !isQueen)) // In this operation I map VectorDirection element (d) to Checker variable type.
.filter(c -> (c != null) && (c.isSecondPlayerOwner() != isSecondPlayerOwner) && isCheckerBlocked(c.getPosition(), d)); // But in this operation I need to access d variable...
PROBLEM: The function isCheckerBlocked() (which uses in last filter() operation) takes variable of VectorDirection type (variable d). But after calling map() function I lose access to this variable. How I can save access to d variable after calling map() function?
Thank you for attention.
directiononce you've mapped it to a new type. Maybe you can create a new object that encapsulatesVectorDirectionandCheckerand you can map 'direction' to that type.