Robert Pickering and
Kit Eason
2nd ed. 2016
Robert Pickering
St. Germain-En-Laye, France
Kit Eason
Any source code or other supplementary material referenced by the author in this text is available to readers at www.apress.com . For detailed information about how to locate your book’s source code, go to www.apress.com/source-code/ .
ISBN 978-1-4842-1375-9
e-ISBN 978-1-4842-1374-2
DOI 10.1007/978-1-4842-1374-2
© Robert Pickering and Kit Eason 2016
Beginning F# 4.0
Managing Director: Welmoed Spahr
Lead Editor: James DeWolf
Development Editor: Douglas Pundick
Technical Reviewer: Fabio Claudio Ferracchiati
Editorial Board: Steve Anglin, Pramila Balen, Louise Corrigan, James DeWolf, Jonathan Gennick, Robert Hutchinson, Celestin Suresh John, Michelle Lowman, James Markham, Susan McDermott, Matthew Moodie, Jeffrey Pepper, Douglas Pundick, Ben Renow-Clarke, Gwenan Spearing
Coordinating Editor: Melissa Maldonado
Copy Editor: Mary Behr
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
For information on translations, please e-mail [email protected] , or visit www.apress.com .
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/bulk-sales .
Standard Apress
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Printed on acid-free paper
For Noah and Darwin.
That moment. That secret weapon moment. That I-got-lucky-and-came-across-something-that-helped-me-win-the-game moment. This might just be it.
This book teaches you all you need to know to get going with F#, an open source, cross-platform programming language suitable for just about every kind of programming and data processing task. And yes, learning F# is like learning a secret weapon. Wielding it, you will find yourself doing things previously beyond your limits: it will empower you, and with it you will do great things.
Why do I know this? Because I have seen the authors of this book, Kit Eason and Robert Pickering, learn F# and wield it powerfully and effectively in job after job. I’ve seen how it has changed the way they think about programming. In this book, they share what they have learned with you, and they want to help you learn and use this secret weapon too.
No programming language is a “silver bullet;” F# won’t magically slay dragons in and of itself. However, F# does an excellent job of removing the incidental complexity that bedevils so much of programming. Take one example: the pervasive use of “null” values in languages such as C# and Java. In one real-world case study by Simon Cousins, using F# reduced the incidence of null checks by 200x in a transaction processing engine, with over 3,000 extra null checks in the fully object-oriented application in question. This is no minor thing: to continue the secret weapon analogy, a null check is like a potential weapon failure at a critical moment, a gun-jam. At any one of these 3,000 code points, that application was at risk of failing. It is simply better–and less risky–to fight your battles with a weapon that doesn’t jam. F# reduces the number of potential failure points, and does so systematically. Some argue that incidental complexity is not important. This is wrong: removing incidental complexity is the first and most essential step you can take to becoming a more productive programmer, since it liberates you to address the real heart of programming. Learning F# will help you do this.
F# is known as a “functional-first” programming language, meaning you use simple functional programming as the first way to solve most problems. This is simple and easy, and lets you solve most programming problems with ease. F# programming is, however, pragmatic. Good software engineering really matters in F#: we care about code quality, naming, documentation, and good design. This book will show you all of these things. It will also teach you how to use F# for data access, web programming, parallel programming, and a myriad of other practical tasks. Finally, it will show you how to contribute back to the F# community through open source packages.
Take the moment and learn F#, and don’t look back. Thousands of people are enjoying this language daily, and with more efficient, expressive coding, and higher productivity. And not just productivity, but delight and happiness in craftsmanship as well.
Don Syme
The first vote of thanks must go to the person who changed my professional life, and those of many others: Don Syme, the “father of F#.” Don does invaluable work in mentoring and encouraging me and the whole F# community. He sets a positive tone that is reflected in everything the community does.
Huge thanks also to Fabio Claudio Ferracchiati, our technical reviewer, who has turned around reams of material in what seems like record time.
Thanks also to the various employers who have supported me (admittedly with varying degrees of scepticism!) during my journey into F#. Extra special thanks to Edoardo Turelli and all the team at Adbrain, who have somehow unlocked the secret of having the right people working on the right problems in the right way.
I’m deeply indebted to the many members of the F# community (both open source and within Microsoft) who have grown F# from an academic project to a thriving commercial and scientific language with a broad-based ecosystem. Every commit counts!
I’d like to thank everyone at Apress who contributed to this book, notably James DeWolf, Melissa Maldonado, and Douglas Pundick. Thank you for your patience, professionalism, and support.
Finally, huge appreciation to my wife, Val, for her unstinting support in everything I do; and to my children, Matt and Kate, both of them frankly more talented developers than I, for keeping me on my toes.
Robert Pickering is a Microsoft MVP acknowledged as a community leader and a world class expert on F#. In his ten year career as a software engineer he has participated in a wide range of projects building large, scalable, and fault tolerant systems on the Microsoft .NET platform. He has experience in both consulting and working as an engineer for a software house. He has been invited to speak at prestigious events such as Microsoft TechEd, JAOO, and TechDays Paris. He has been involved in organizing conferences as track lead for events such as Functional Programming eXchange, London, and he organizes the ALT.NET community meetings in Paris. He has also appeared in podcast and screen casts such as dnrtv.com and hanselminutes.com.
Kit Eason is a software developer and educator with 30 years of experience in industries from automotive engineering through university supercomputing to energy trading. He currently works as a senior developer at Adbrain ( www.adbrain.com ), a leading data intelligence company that empowers marketers to regain control of their customer relationships in today’s hyperconnected yet highly fragmented world. Kit also works as an author for Pluralsight: his courses there include “F# Jumpstart” and “F# Functional Data Structures.” He has also appeared on the “.NET Rocks!” podcast.
Fabio Claudio Ferracchiati is a senior consultant and a senior analyst/developer using Microsoft technologies. He works for Blu Arancio ( www.bluarancio.com ). He is a Microsoft Certified Solution Developer for .NET, a Microsoft Certified Application Developer for .NET, a Microsoft Certified Professional, and a prolific author and technical reviewer. Over the past ten years, he’s written articles for Italian and international magazines and coauthored more than ten books on a variety of computer topics.
18.117.186.46