Preparation
Always make sure to pull/clone/download the latest 101simplejava repo.
See the instructions here:
https://github.com/101companies/101simplejava/blob/master/README.md
Starting point
Various simple OOP-like contributions could be picked as a starting point, e.g., Contribution:javaComposition.
Objective
Understand function objects and function combinators and apply them to list processing. That is, rather than using imperative for-loops in organizing queries and transformations over object graphs, use a combinator for list processing to apply simple actions or functions to individual objects.
Task
Revise the object model such that the involved collection types for lists of departments and employees are enhanced to be amenable to the application of actions and functions to elements of the collections. This may be achieved in different ways, but one option is that the enhanced collection types support two combinators: "map" (inspired by functional programming: http://101companies.org/wiki/Map_function) to apply an action (see lecture) to each element of the list; "fold" (inspired by functional programming: http://101companies.org/wiki/Fold_function) to apply a unary function (see lecture) to each element of the list. With this enhancement in place, the features Cut and Total are to be implemented so that combinators rather than for-loops are used.
Hint
Here is incomplete pseudo-code for the expected revision of the Cut feature:
public class Cut {
public static void cut(Company c) {
// An action for individual employees
Action<Employee> ae = new Action<Employee> {
void apply(Employee e) {
// cut the salary in half
...
}
};
// An action for individual departments
Action<Department> ad = new Action<Department> {
void apply(Department d) {
...
d.getEmployees().map(ae);
...
}
};
// process the list of departments of a company
...
}
}
Thus, map takes an action and applies it to all elements of the receiving list. Without giving pseudo-code here, in the re-implementation of Total, the invocation of fold may need to receive several arguments: the function for extracting values from elements, the binary operator and the initial value for aggregating the result. Other approaches are appreciated: possibly, collection types could be enhanced differently for use with combinators (rather than for-loops).
Testing
The test cases for the features Cut and Total should be maintained to work with the re-implemented features.