Description: https://images.manning.com/360/480/resize/book/5/d8b12cb-463a-4042-b4a8-260ffc0d020e/Abraham-MEAP-HI.png

From F# in Action by Isaac Abraham

Are you a software developer who is interested in F# and what it can do? If so, this book is for you!

This article details what F# is, how it works, and what this book will teach you about using it.


Take 25% off F# in Action by entering fccabraham3 into the discount code box at checkout at manning.com.


What is F#?

F# is a general-purpose programming language that’s designed to build systems that are robust, succinct and performant across Windows, Mac and Linux.

It has a lightweight syntax, yet a powerful type-system and compiler. This makes it a compelling choice for writing applications, as it enables developers to rapidly model real-world systems in a way that does what they intend. It’s also an opinionated language with a set of characteristics that it is optimized towards.

And as F# runs on the modern, lightweight and cross-platform .NET Core runtime, it has a large, mature ecosystem of free and commercial tools and libraries, ranging from web servers, database access, cloud, data and analytical libraries, actor frameworks, and more. This means that you won’t have to create entire platforms yourself, but instead re-use existing battle-tested components that are already out there, leaving you to focus on delivering business value. You can seamlessly interoperate with C# code, creating hybrid F# / C# solutions if required – so if you’re already using .NET, much of what you already know about the platform will continue to apply.

Why F#?

There are many reasons why F# might be a great fit for you to start using – here’s a few to whet your appetite!

F# is rewarding and fun. People that use F# find it a rewarding and enjoyable language to quickly write applications in that simply lets you get stuff done, freeing you to focus on the business of writing your application whilst the language does its best to help you along the way and guide you into the pit of success. I’ve spoken with many people who said that they were in a programming “rut” – but gained a new-found enthusiasm for software development when they switched to F#, because it freed them to focus on delivering business value to their customers. This also works for the hiring manager – happier developers generally leads to less turnover, lower costs and better results.

F# is highly productive. At its core, F# is a relatively simple language, with a succinct and easy-to-read syntax and a relatively consistent way of problem solving. Yet at the same time, it’s a safe language to use, with a powerful compiler and type system (more so than many popular programming languages) that can trap many classes of potential bugs as you type. It also runs on a performant, mature popular platform (.NET) with a wide variety of time-saving packages. In my experience, people writing applications in F# do so more quickly than in other languages because it hits a sweet spot between simplicity, safety, and power.

F# is also very flexible – you can use it in a wide variety of scenarios, from simple scripts for data exploration and experimentation to full-blown cloud-enabled end-to-end micro-services and web applications. There’s a very good chance that whatever you’re doing today, you can also do in F# – as well as the possibility that F# will open the door to areas that you may not have considered before. For example, if you’re coming from a C# background, F# will show you how you can use .NET for more data oriented and analytical tasks that perhaps previously you might have thought can only be done in SQL. For those of you coming from a Python background, you’ll see a language whose syntax is not a million miles away from what you already know yet is statically typed and highly performant. And for those coming from a JavaScript background, you’ll appreciate how some of the functional patterns that are becoming popular in that language are first-class citizens here, and with a static type system and syntax that gets out of your way.

Additionally, if you’re interested in learning some of the fundamentals of functional programming (FP), F# is a great way to do that – it has its roots in languages such as ML and OCaml. However it’s what I would call a “light-touch” functional programming language – you’ll appreciate the way it guides you towards writing simple, error-free code without needing to know any of the “maths and theory” that people often associate with FP. Importantly, it also provides you with enough escape hatches such that if you need to break out into procedural or object oriented programming, you can do it without much effort.

When F#?

Since F# is a general-purpose programming language, the simplest answer might be “whatever you’re doing today in whatever programming language you’re working in, you can probably do it in F#”. Nonetheless, let’s look at a few different use-cases of F# to give you an idea of what’s possible.

  • Console Applications

You can write basic console applications such as command line tools in F#. It’ll happily read and write from the terminal and has libraries to make animated, colorful console applications with widgets such as tables, charts and progress bars.

  • Background Workers

F# allows you to write background services or daemons which don’t necessarily have any console inputs. Perhaps you need a process that runs on a virtual machine that reads data from a database and creates a report once a day, or an application that sits in the background and monitors a folder for new files as they’re created to parse and process them? No problem!

  • Desktop Applications

F# has support for desktop applications with a number of technologies, whether it’s simpler database-driven applications or richer applications with sophisticated user interfaces, using either Windows-specific or cross-platform libraries and frameworks.

  • Cloud Services

You can write and deploy applications designed for just about any cloud provider – whether it’s a more infrastructure-focused service or one of the platform services that the bigger players such as Azure, Amazon or Google offer, there’s support for F#.

  • Mobile Applications

Through the Xamarin and upcoming MAUI projects, Microsoft offers a full suite of frameworks and tools for developing mobile applications for both Android and iOS without knowing Java, ObjectiveC or Swift. Instead, applications can be written once in F# and simultaneously target both platforms.

  • Web Services

F# can write web services that are secure, reliable, highly scalable, and extremely performant through the ASP .NET Core framework. Not only are web applications a natural fit for F#’s stateless programming model, but F# also has several libraries that give a truly first-class F# experience on top of ASP .NET Core.

  • Web Applications

Want to write applications in the browser? F# has you covered here, too! Through the Fable framework, you can compile your F# directly into JavaScript and run applications in the browser directly. This is a real boon if your team are not JavaScript experts and prefer the benefits that static typing brings, where you can get the best of both worlds: Use popular JavaScript libraries and frameworks such as React, whilst writing code safely in F#.

  • Data exploration and analysis

F# has a solid data-analysis-and-scripts story, from consuming data from multiple sources through to visualizing data through charts and tables.

  • Data Modelling

One of F#’s often overlooked features is how effective it is at modelling domains. Not only does it have a type system that allows you to easily model the real world in a minimum amount of code, but it’s readable code – the sort that you can legitimately sit down with a non-technical user and explore a domain, evolving a model of the real world step-by-step, side-by-side.

How will this book teach me F#?

This book is designed to teach software developers who already understand the fundamentals of software development how to develop F# applications on the .NET platform. It won’t teach you everything about the F# language – it’s not a reference guide – but it will teach you the core of the language in a way that will give you the confidence to start creating applications on your own and learning how to find out more about the language and ecosystem on your own.

As part of the journey of describing F#, this book will also teach you the basics of functional programming (FP). You won’t need to know anything about FP before buying this book, and you won’t leave it as an expert in F#, but you will understand the core concepts – and be able to apply them in the real world to build applications from simple console applications to fully-featured web applications.

How does F# work?

Let’s take a look at what development in F# looks and feels like. This is based not only on my own personal experiences of the language, but many others who have used it extensively.

Figure 1 shows you how I see some of the common features of what your development experience will look like.


Figure 1 – The mental model of the development process in F#


Collaborative exploration

One of the features of the F# development experience that might not be so familiar to you is that it contains a highly interactive “scripting” mode, where you can experiment with different ways of modelling code, try different ways of implementing solutions and see how they work. Once you’re happy with the result, you can then quickly migrate it into a full-blown application – whether a console application or a data-driven web API running on ASP .NET Core (the standard .NET web server).

Another aspect to this is the collaborative element that F# brings. F#’s syntax is highly readable for non-technical people, and combined with its type system, it is especially effective for domain modelling. This means that you can (and I have done, many times) sit alongside a customer and starting from ground zero try to build up a model of a real-world system and test it out with some arbitrary code. There’s no need for a console test runner or full-blown database – just some code and in-memory data. It’s a much more iterative process, where ideas can be tested and thrown away at low cost, and both you and the domain experts can start to build a common understanding of the system you’re trying to model.

Type Driven Development

F# is a type-safe statically-typed programming language, which means that you formally declare the types of your system, and the compiler will enforce rules that you define about those types. F# makes it very, very easy – and therefore cheap – to create and use types, meaning that you generally can afford to be a little more specific in how you model your systems. Doing this means that your code can be used to encode more business rules and is clearer and easier to read than it might otherwise be. Doing this not only means that we can more easily support business rules about our types but also protects against accidentally mixing and matching incorrect types of data. Finally, it also helps from a readability point of view: for example, whilst both City and Country in an Address type might be both strings, in F# it wouldn’t be unusual to have “wrapper” types for both, each with their own invariant rules and constraints.

So, although F# is totally happy with you not doing any of this stronger domain modelling, it also doesn’t impose much of a cost on doing so – and thus encourages us to “do the right thing” up front.

Trusting the compiler

Following on from this, one of the things that F# encourages us to do is to trust the compiler. In many statically typed programming languages, software developers see the compiler as their enemy – something that must be somehow “overcome” to run your program so that you can test that it works, or not. In F#, this relationship changes to one where the compiler becomes your partner, giving you guidance and help in order to write code that does the correct thing.

Eventually, the developer trusts the compiler to prove business rules without feeling the need to run the application at all: Whilst F#’s type system isn’t all-powerful, it has enough smart features so that you can often code for several hours at a time, modelling your systems, writing functions, mapping types from one shape to another, following the trail of breadcrumbs that the compiler gives you to follow to get your code working correctly – without ever once running your application. And generally, once your code compiles, that first time you do run it after the coding session, you’ll find that it just works. So, you and your team will typically spend, as a proportion of your entire development flow, more time actually coding and less time “trying things out” in the app end-to-end.

I know that this sounds unlikely – unbelievable even – but it’s true. This is based not only on my own experiences, but from working with and speaking to lots of different F# teams and solo developers – they all say the same thing: F# makes them more confident developers because it helps them do the right thing.

An opinionated language

F# is an opinionated language, and it optimizes for a specific way of writing applications safely that leads to fewer bugs and quicker time to market. The language makes it easy to do the “right” thing first thing, and conversely makes you pay a cost to do the “wrong” thing.

For example, imagine you’re writing a system that needs to export a report in either Excel, PDF or Web formats. In F#, the compiler will give you a warning or error if you forget to handle any of those cases; if you add a fourth report module in the future, the compiler will again instantly show you every place you’ll need to change your code to handle the new report.

This kind of approach is shown throughout the language, meaning that you’ll have much more confidence when your code compiles that it actually works as you expect it to, than not.

Data and Functions

Although F# is different from the majority of popular mainstream languages out there, it’s not hard to learn. It eliminates many concepts that you would need to learn in the object-oriented world, such as inheritance and polymorphism, and replaces those with a simple idea: you have data that represents that state of your application, and functions that act on that data. Each function knows how to transform some data of one type into another type, and you compose those transformations into pipelines. You repeat this pattern throughout your application until the very end of the request, at which point you’ll, for example, write out a file or return some JSON data to the caller of your web service.

For example, you might have a validation function that can handle a customer purchase request, and another function that can convert that request into an invoice, and another one that can email the invoice to the customer.

In fact, this simplicity makes it a great choice for non-software developers, including (but not only) data analysts.

Separation of business logic and external systems

Another thing you’ll notice with F# is that because of the features of the language, it encourages (but doesn’t force you) to maintain a separation between your “domain logic” and external dependencies such as databases or web services.

Doing this has many benefits. Your code will generally be more easily testable, for starters – so if you want to create some unit tests for a specific portion of code, you’ll find that it’s more likely to already support testing, or if not easier to refactor out to do so. This also feeds into the exploratory “scripting” phase that I mentioned at the start of this section: Using scripts is something that you might well carry on into the main development cycle of your application as a way to quickly call parts of the codebase in an ad-hoc fashion. This is especially useful for parts of the system that are more difficult to reach within your main application. People commonly use console test rigs or even unit testing libraries for this, but an interactive script is normally a much more effective and efficient tool for this.

Varied targets

F# has a flexible and varied set of target devices and platforms that it can compile into. .NET itself supports Mac, Linux and Windows platforms, as well as all the major cloud vendors, and can develop desktop app software or backend services. Alternatively, you can write web applications with F# that either focus on backend data-oriented web services using ASP .NET Core, or you can target the browser using the Fable project which compiles F# directly into JavaScript or using Xamarin / MAUI to compile into iOS or Android.

When it comes to data access, there are a plethora of libraries available to you, either built-in to .NET, available as C# “packages” that you can seamlessly use in F#, or bespoke F# wrappers which are designed to give the highest fidelity F# experience. So you’ll have ready-made access to SQL and JSON libraries, cloud platforms such as Microsoft Azure or Amazon Web Services (AWS), and much more.

In some ways, F# gets the best of both worlds – you have a feature set that many other languages (including C#) have been playing catch-up to for a few years now, but at the same time you’ll have access to a popular, mature, high-performance, and reliable platform and ecosystem in .NET.

That about sums it up! If you’re interested in learning more, check out the book here.