functional-csharp-code
functional-csharp-code copied to clipboard
Error thrown for list of working employees
If we pass to AverageYearsWorkedAtTheCompany an empty list or a list that contains only employees that still employed, method Average() will throw an exception, as it requires at least one element (https://github.com/la-yumba/functional-csharp-code/blob/e151c288252c41ba9936e1e9d05431c587cd847a/Exercises/Chapter04/Solutions.cs#L67). I've solved it like this: https://github.com/emanuelefirmani/FunctionalProgrammingCSharp/blob/master/FunctorsMonads/WorkPermitCode.cs#L20
If we do not implement the requirement "Only employees that left", we could write:
return employees.Any() ? employees.Map(GetYearsWorked).Average() : new Option<double>();