Accumulate
Develop an "Accumulate" function, that takes a collection (List of elements) and a function to perform as parameters. The Accumulate function should apply the given function to each element of the collection, accumulating the results in a new collection, which will be returned.
- For instance, assume we have a collection of numbers: 1, 2, 3, 4, 5
- Suppose the function to be applied is: square a number (x => x * x)
- The result should then be a collection holding: 1 (1 squared), 4 (2 squared), 9 (3 squared), 16 (4 squared), 25 (5 squared).
Please pay attention to the following requirements and edge cases:
-
Empty Collection: Your accumulate function must handle the case where the input collection is empty. In this situation, return an empty collection.
-
Null Collection: Similarly, the function should be able to handle a null collection. In such a case, return null.
-
Different Data Types: Your function must handle collections of various data types (Integers, Strings, etc.), not just integers.
-
Null Function: Your function should handle cases where the provided Function is null. Rather than applying a transformation, the original collection should be returned as is in this case.
-
Invariant Order: The order of elements in the output collection should correspond to the order of the elements in the input collection, i.e., the i-th element in the output collection is the result of applying the function to the i-th element in the input collection.
Please go through the Unit Tests provided to better understand the expected outputs for various inputs.
Note: To solve this task, you should not use any built-in map-type functions such as collect(), map(), fmap(), etc. These are provided by standard libraries in many programming languages. You should construct the solution using basic constructs such as loops instead.
Good luck!