Kit Eason
Stylish F# 6
Crafting Elegant Functional Code for .NET 6
2nd ed.
Logo of the publisher
Kit Eason
Farnham, Surrey, UK
ISBN 978-1-4842-7204-6 e-ISBN 978-1-4842-7205-3
https://doi.org/10.1007/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, youll need at least some knowledge of F# syntax and concepts. Maybe youve read some of the wide range of beginner material thats available, and probably youll 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 dont need any background in computer science or functional programming. I dont have even a trace of formal education in either of these areas.
So whats between the covers? In Chapter , Ill briefly reiterate what weve learned.
As this book is primarily about the language, youll find relatively few references to other libraries. Of course, to build substantial systems, youll 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 Ive 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. Dont forget to have fun!
Any source code or other supplementary material referenced by the author in this book is available to readers on GitHub via the books product page, located at www.apress.com/9781484272046. For more detailed information, please visit http://www.apress.com/source-code.
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 . 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 Ive 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
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.