Embracing Functional Paradigm in F# for Enhanced Productivity
F# is a relatively new primarily Functional programming language for the .NET platform. It is a statically typed managed functional language that is fully inter-operable with other .NET languages like C#, Visual Basic.NET etc. It builds on the power of Functional Paradigm and combines it with .NET Object-Oriented model enabling the developer to use the best approach for a given problem.
This workshop introduces Functional Programming in F# from ground up. No prior experience in Functional Programming or .NET is needed, familiarity with a mainstream programming language like C++/Java/C# should be enough.
Functional programming (FP) offers several benefits. The code tends to be terse which leads to enhanced developer productivity. FP encourages pure functions which are much easier to reason about and debug, as well as eliminates large class of bugs due to side effect free programming. Moreover, immutability leads to easy parallelization of the code. Algebraic Data Types can be used to express domain object conveniently and control state space explosion.
F# is great practical choice for developing reliable and highly scalable real-world system that are quick and easy to develop due to the design of the language itself combined with the ability of the language to use a large no. of 3rd party libraries designed for the .NET platform.
Unfortunately, support for multiple paradigms often leads to confusion. Newcomers tend to find the transition from object-oriented world to functional world difficult. Moreover, it often leads to abuse where developer tries to use the same old imperative style of coding in a functional language and is unable to take advantage of the features, the language has to offer.
Outline/Structure of the Workshop
- Introduction to Functional Programming
- Benefits of Functional Programming
- Introducing F#
- Benefits of F#
- DEclarative Style
- Data Immutability
- Functionals as First class Values
- Higher-order functions & Functions composition
- Partial Application
- Structured way of storing data
- Discriminated Unions & Pattern matching
- F# Collections: List, Array & Sequences
- Pipeline operator
- Units of Measure
- Type Providers
- Object Oriented Programming in F#
- Interfacing F# with C# code
- Introduce the developer to Functional paradigm and functional way of thinking in F#
- Develop the ability to decide which paradigm to use for what problems
- Leverage the existing code in C# and other .NET languages as well as large no. of 3rd party libraries written for the .NET platform.
Engineers interested in learning Functional Programming
Prerequisites for Attendees
Familiarity with a mainstream programming language like C++/Java/C# should be enough.
schedule Submitted 4 years ago
People who liked this proposal, also liked:
Nikhil Barthwal - Implementing Event-Driven Microservices architecture in Functional languageNikhil BarthwalSr. Software Engineer
schedule 4 years agoSold Out!
Web services are typically stateless entities, that need to operate at scale at large. Functional paradigm can be used to model these web services work and offer several benefits like scalability, productivity, and correctness.
This talk describes how to implement Event-Driven Microservices with examples in F#. It starts with introducing Domain Driven Design to create Microservices boundaries. Using Discriminated Unions (F#'s Algebraic Data Types), the domain model can be captured as code eliminating the need for separate documentation. Moreover, using Computation expressions (F#'s Monads), one can model custom workflows easily.
It then introduces event-driven architecture, where every external action generates an event that the system responds to. Events act as the notification messages for any significant change in state and may generate other event(s) as services invoke each other. They are immutable by nature.
An explanation on why 2-phase commits cannot be used in Microservices having their own databases. Further the talk explains, how Event Driven Architecture solves this problem in an eventually consistent manner without sacrificing availability or partition tolerance. Distributed Sagas as a protocol for coordinating Microservices is introduced and its implementation in F# is also provided.
Event Sourcing can be used to model the system state. Event sourcing models the state of entity as a sequence of state-changing events. Whenever the state of a business entity changes, a new event is appended. List fold operation is ideal for implementing Event sourcing where the application reconstructs an entity's current state by replaying the events. An example with F#'s List.fold is provided.
Some aspects of evolutionary architecture are also discussed, particularly on how to evolve Microservices interface. F#'s Type providers can be used for the same though there are alternate approaches using Apache Thrift/Google Protobuf (They don't have support for F# but they do have support for C#, which F# code can leverage).
Events and their responses can be very easily modeled with Discriminated Unions. Data immutability captures the behavior of these events, since events are immutable by nature. A service can be thought of as a function that accepts an event (input) and gives back a response (output). A service may call other services, which is equivalent to a function calling other functions or even Higher-Order functions.
Immutability allows infinite scalability as it eliminated the need to worry about a mutex, a lock, or a race. As functional code is much more terse compares to object-oriented code, it provides productivity benefits. Its strict typing makes writing correct code easy as mismatch of types are caught at compile time.
Most of the services are implemented as set of pure functions. These functions which have no internal state, where outputs depend only on inputs and constants and it is very easy to test such functions. The absence of internal state means that there are no state transitions to test. The only testing left is to collect a bunch of inputs that tests for all the boundary conditions, pass each through the function under test and validate the output.
The objective of the talk is to show how to create a scalable & highly distributed web service in F#, and demonstrate how various characteristics of functional paradigm captures the behavior of such services architecture very naturally.