Kit Eason

Stylish F# 6

Crafting Elegant Functional Code for .NET 6

2nd ed.
Kit Eason
Farnham, Surrey, UK
ISBN 978-1-4842-7204-6e-ISBN 978-1-4842-7205-3
© Kit Eason 2022
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication does not imply, even in the absence of a specific statement, that such names are exempt from the relevant protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or the editors give a warranty, expressed or implied, with respect to the material contained herein or for any errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional claims in published maps and institutional affiliations.

This Apress imprint is published by the registered company APress Media, LLC part of Springer Nature.

The registered company address is: 1 New York Plaza, New York, NY 10004, U.S.A.

To Val, Matt, Meg, Kate, Andy, Noah, and Darwin: my own persistent collection.

Introduction

There are three distinct philosophies which you can apply to computer programming. You can think of programming as a science, where the measure of progress is how well you discover and reflect fundamental mathematical concepts in your code. You can think of it as a discipline, where you seek to establish and follow rules about how code should be written and organized. Or, best of all, you can think of it as a craft, where, yes, you apply some of the science and some of the discipline; but you leaven those with a generous helping of human creativity. To do this successfully, you need a fair bit of experience, because crafting something is an inherently intuitive process. This book aims to get you to a level where you can craft code confidently. It does this by distilling and passing on my own experience of writing F# systems in numerous different industries over the past ten years.

Before you start this book, you’ll need at least some knowledge of F# syntax and concepts. Maybe you’ve read some of the wide range of beginner material that’s available, and probably you’ll have written at least a few simple F# programs yourself. You may well have deeper experience of other languages, such as C# or Python. That said, I have framed the book so that C# knowledge is not a hard prerequisite: I learned F# before I learned C#, and if I can do it, so can you! Also you definitely don’t need any background in computer science or functional programming. I don’t have even a trace of formal education in either of these areas.

So what’s between the covers? In Chapter 1, I’ll establish some principles which will help us decide whether we are coding well and say a little bit about why coding stylishly is important. In Chapter 2, we’ll pick up the basic tools of our craft and learn to chisel out elegant and reliable functions. In Chapter 3, we’ll tackle the thorny issue of missing data, learning some effective techniques for writing dependable code when certain values might not be available. In Chapter 4, we’ll pick up some more powerful crafting tools, the so-called collection functions, and explore how you can use them to achieve a surprising amount with very little code. In Chapter 5, we’ll delve into the strange world of immutability: how you can write programs which achieve a result without explicitly changing anything. In Chapter 6, we’ll look at pattern matching, a concept you may have looked at a little when you learned F# syntax, but which is surprisingly pervasive and powerful in quality F# code. In Chapter 7, we’ll explore record types, F#’s go-to structure for storing groups of labeled values. In Chapter 8, we’ll cover some ground which might already be familiar to C# developers: object-oriented classes. In Chapter 9, we’ll return to the topic of F# functions and explore what it means for a function to also be a first-class value. In Chapter 10, we’ll tame the apparent complexity of asynchronous and parallel programming: it needn’t be as hard as you think! In Chapter 11, we’ll look at Railway Oriented Programming, an interesting metaphor you can use to help you think about processing pipelines. In Chapter 12, we’ll investigate performance: Can you really write code which is both elegant and fast? In Chapter 13, we’ll establish some useful techniques for laying out your code and naming items to maximize readability. In Chapter 14, I’ll briefly reiterate what we’ve learned.

As this book is primarily about the language, you’ll find relatively few references to other libraries. Of course, to build substantial systems, you’ll almost always want to pull in NuGet packages for requirements such as unit testing, serialization, web serving, and so forth. But these libraries constitute a large and fast-changing landscape, so I’ve chosen to pare things down to the F# essentials for this book. For the same reason, I hardly mention graphical user interface or web development. Since the first edition of this book, F# has become much more widely used in these areas, but this has been via innovations in the surrounding ecosystem (notably the Fable transpiler), whereas in these pages, I want to concentrate on the F# language itself.

This focus also means that most of the code examples can be typed in and run as cells in a .NET Interactive notebook. They are provided in notebook form in the downloadable code samples. In the small number of cases where you need to write a compilable program, I take you through the process in the text alongside the example. These non-notebook examples are also provided in the downloadable code but in “project” form so that you can open and build them in Visual Studio Code (with the Ionide extension), Visual Studio, or JetBrains Rider.

I very much hope you enjoy sharing my F# experience as much as I enjoyed acquiring it. Don’t forget to have fun!

Acknowledgments

I am grateful for the generous help I received in putting Stylish F# together. Thanks to Stachu Korick (Second Edition) and Quinton Coetzee (First Edition) for their exceedingly diligent and constructive technical reviews. To Val Eason for detecting many typos and poor turns of phrase. To Dr. Jon Harrop for providing detailed technical feedback on Chapter 12, and to several other F# community members who have provided feedback on the first edition and ideas for the second. To Jason Heeris for kindly giving permission to reproduce the cartoon in Chapter 1. To Don Syme, Phillip Carter, and the F# community for the never-ending stream of compiler and tooling improvements that propel F# forward. And to Matt Jones and the amazing team at Perpetuum for providing the best working environment I’ve ever experienced. Thanks also to the tireless crew at Apress: Joan Murray, Jill Balzano, and Laura Berendson. Any errors, omissions, or plain wrong-headedness are, of course, still my own responsibility.

Table of Contents
About the Author
Kit Eason

is a software developer and educator with more than 20 years of experience. He has been programming in F# since 2011 and is employed at Perpetuum Ltd., working on an extensive network of energy-harvesting vibration sensors fitted to railway rolling stock and infrastructure. Kit is an avid F# user who is passionate about teaching others. He has contributed to several publications, including the Apress book “Beginning F#.” He often teaches on the topic of F#, and his popular videos appear on Udemy and Pluralsight.

 
About the Technical Reviewer
Stachu Korick
../images/462726_2_En_BookFrontmatter_Figb_HTML.jpg
stumbled upon F# in 2014 and instantly fell in love with both the language and the surrounding community. Most importantly, he met his wife Olya after speaking on F# at a local .NET conference near Philadelphia. As time allows, he works on an F# podcast WTF# (https://wtfsharp.com). Beyond software, Stachu spends his time as an amateur woodworker, playing with his cats, practicing chemistry, or jotting down bits of lyrics to eventually compose into music.
 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
13.59.69.201