In the Functional Programming in C# 9 presentation by Simon Painter, he discusses various aspects of functional programming in C#, including the benefits of new features like arrow functions and syntax for selecting object properties. He also introduces concepts such as thunks, higher-order functions, and extension methods for validating insurance numbers, as well as nullable value types, record types, and currying. Simon shares his experience with implementing functional programming structures like Maybe monads and Option Types in C#. He expresses his preference for functional programming despite its challenges.
Functional Programming in C# 9: A comprehensive overview
Introduction to Functional Programming in C#
Simon Painter begins by introducing himself and outlining the topic of functional programming in C#. He highlights how C# has progressively embraced functional programming, sharing examples like parsing a CSV file functionally using the link function. This approach is functional because it doesn’t modify the original data and requires functions as parameters. Simon underscores that each C# release has been deliberately more supportive of functional paradigms, complementing its object-oriented and hybrid programming capabilities.
Validating rules with thunks and higher-order functions
Simon then delves into a more complex example: validating a set of rules for a National Insurance number. He introduces the concept of thunks, which are functions stored in variables, and demonstrates their use in arrays for validation tasks. Using higher-order functions and extension methods, he shows how to create a more functional and efficient validation process. The concept of alt is introduced, allowing operations to be run sequentially until one returns a useful result, highlighting the maintainability and reduced boilerplate code benefits of functional programming.
Handling null values with forks and pattern recognition
In the next section, Simon discusses handling null values in generics using a conditional structure called a fork. He provides a code example that checks if an input is null and runs a specific function if so. This discussion extends to pattern recognition, an exciting new area in C# 9. He illustrates this with a bank account system example, showcasing how different account types with varying interest rates can be managed using switch statements and an optional when clause.
Arrow functions, immutability, and nullable types
Simon explores new C# 9 features like arrow functions and object property selection syntax, which streamline code by reducing type checking and boilerplate. He also touches on immutability in C#, admitting the challenge due to mutable classes from external libraries. The addition of nullable enable is a step forward but not a complete solution for immutability. He explains nullable value types and record types, the latter being passed by value rather than reference, and highlights the with syntax for updating properties efficiently.
Currying and partial application
Currying and partial application are key functional programming concepts. While C# doesn’t support currying natively, similar results can be achieved through partial application and extension methods. Simon provides an example of parsing a CSV file using partial application and notes the boilerplate code required for implementation. He emphasizes the functional flow involving filtering, converting, and aggregating data into a single answer.
Custom aggregation functions and tuples
Simon uses Doctor Who episodes to illustrate custom aggregation functions in C#. He introduces tuples for functional flows and demonstrates creating a custom aggregation function using a tuple and a function for current value and accumulator. This method simplifies calculations and functional programming structures, like identity, which replace loops and if statements.
Implementing monads and option types
In a deeper dive, Simon explains implementing monads, specifically the Maybe monad, in C#. Monads handle function chaining and side effects in functional programming. The Maybe monad deals with functions that could return something or nothing, using a base class and two subclasses. The bind function chains functions, and if one fails, it returns the Nothing class. Simon also discusses libraries like Language-Ext for implementing Option Types and recommends Enrico Bonanno’s book Functional Programming in C# as a resource.
Simon’s journey with Functional Programming
Simon shares his personal journey with functional programming, admitting initial struggles due to its mathematical nature. He prefers explanations from an engineering perspective. Although he has worked with F#, he notes that most .NET developers are more familiar with C#, making F# less accessible for teams. He believes learning functional programming enhances programming skills due to its conciseness and reliability, leading to more robust programs.
Conclusion and further resources
The presentation concludes with Simon Painter encouraging the audience to explore functional programming further. Simon’s insights into functional programming in C# 9 offer a valuable roadmap for developers looking to enhance their skills and embrace the functional paradigm within the C# ecosystem.