Block / Closures: Java, C#, Ruby
Java version
public EmployeeList managers(EmployeeList emps) {
return emps.findAll(
new EmployeeFinder() {
boolean find(Employee emp) { return e.isManager; }
}
);
}
// Supporting EmployeeList class and EmployeeFinder interface required
C# 2.0 finder (Less verbose than Java and no supporting classes)
public List Managers(List emps) {
return emps.FindAll(delegate(Employee e) { return e.IsManager; } );
}
Ruby finder
def managers(emps)
return emps.select {|e| e.isManager}
end
=======================================================================
C# 2.0
int total = 0;
order.EachItem(delegate(Item item) {
total += item.Price;
}
Java
final int[] total = { 0 };order.eachItem(new OrderBlock() {
public void handle(Item item) {
total[0] += item.getPrice();
}
});