la-yumba / functional-csharp-code

Code samples for Functional Programming in C#

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error thrown for list of working employees

emanuelefirmani opened this issue · comments

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 (

static double AverageYearsWorkedAtTheCompany(List<Employee> employees)
).
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>();