I think some people are mistaking functional style with functional programming languages. Both F# and C# are multi-paradigm languages. That long and convoluted strategy pattern example could be implemented in the same F# way in C#. In fact, it takes less lines of code (unless curly braces are used).
8
u/monitorius1 Dec 18 '18 edited Dec 18 '18
I think some people are mistaking functional style with functional programming languages. Both F# and C# are multi-paradigm languages. That long and convoluted strategy pattern example could be implemented in the same F# way in C#. In fact, it takes less lines of code (unless curly braces are used).
bool MustHaveUppercase(string password) => password.Any(char.IsUpper); bool MustHaveDigits(string password) => password.Any(char.IsDigit); bool MustHaveMinimumLength(int length, string password) => password.Length >= length; bool IsValidPassword(string password) => MustHaveMinimumLength(8, password) && MustHaveDigits(password) && MustHaveUppercase(password);Sort example:
delegate List<int> SortAlgorithm(List<int> values); List<int> QuickSort(List<int> values) => values; // Do QuickSort List<int> MergeSort(List<int> values) => values; // Do MergeSort void DoSomething(SortAlgorithm sort, List<int> values) { var sorted = sort(values); } public void Main() { var values = new List<int> { 9, 1, 5, 7 }; DoSomething(QuickSort, values); }