Book Image

F# 4.0 Design Patterns

By : Gene Belitski
Book Image

F# 4.0 Design Patterns

By: Gene Belitski

Overview of this book

Following design patterns is a well-known approach to writing better programs that captures and reuses high-level abstractions that are common in many applications. This book will encourage you to develop an idiomatic F# coding skillset by fully embracing the functional-first F# paradigm. It will also help you harness this powerful instrument to write succinct, bug-free, and cross-platform code. F# 4.0 Design Patterns will start off by helping you develop a functional way of thinking. We will show you how beneficial the functional-first paradigm is and how to use it to get the optimum results. The book will help you acquire the practical knowledge of the main functional design patterns, the relationship of which with the traditional Gang of Four set is not straightforward. We will take you through pattern matching, immutable data types, and sequences in F#. We will also uncover advanced functional patterns, look at polymorphic functions, typical data crunching techniques, adjusting code through augmentation, and generalization. Lastly, we will take a look at the advanced techniques to equip you with everything you need to write flawless code.
Table of Contents (20 chapters)
F# 4.0 Design Patterns
Credits
About the Author
Acknowledgements
About the Reviewer
www.PacktPub.com
Preface

Preface

Following design patterns is a well-known approach to writing better programs that captures and reuses the high-level abstractions that are common in many applications. This book will encourage you to develop an idiomatic F# coding skillset by fully embracing the functional-first F# paradigm. It will also help you harness this powerful instrument to write succinct, bug-free, and cross-platform code.

F# 4.0 Design Patterns will start off by helping you develop a functional way of thinking. We will show you how beneficial the functional-first paradigm is and how to use it to get the optimum results. The book will help you acquire the practical knowledge of the main functional design patterns, relationship of which with the traditional “Gang of Four” set is not straightforward.

We will take you through pattern matching, immutable data types, and sequences in F#. We will also uncover advanced functional patterns, look at polymorphic functions, see typical data crunching techniques, and learn adjusting code through augmentation and generalization. Lastly, we take a look at the advanced techniques to equip you with everything you may need to write flawless code. In addition, we will explore how the paradigm shift to functional-first affects the design principles and patterns of the object-oriented universe and wrap up the book with specifics of functional code troubleshooting.

By reading this book you will achieve the following goals:

  • Acquire the practical knowledge to use the main functional design patterns

  • Realign some imperative and object-oriented principles under the functional approach

  • Develop your confidence in building and combining first-order and higher-order functions

  • Learn how to use core language pattern matching effectively

  • Learn how to use embedded algebraic data types in place of custom types for added effectiveness and code succinctness

  • Navigate and use F# core libraries with ease by seeing patterns behind specific library functions

  • Recognize and measure the difference in resource consumption between sequences and materialized data structures

  • Master writing generic polymorphic code

What this book covers

Chapter 1, Begin Thinking Functionally, should help you in developing a manner of coding usually associated with the functional paradigm. It will equip you with the knowledge, the key concepts, and the target list of skills pertinent to the functional-first nature of F# programming language.

Chapter 2, Dissecting F# Origins and Design, will help you to find out the origins of F# contemporary design, how F# evolved, and what place it occupies in the .NET ecosystem.

Chapter 3, Basic Functions, helps you to acquire a firm foundation for idiomatic F# use. It gives you 360-degree review of the functional paradigm cornerstone, the notion of the function. You will be taught how to represent any solution as a assortment of functions plugged together with a handful of combinators. This chapter prepares you to absorb the main subject-the patterns of F# idiomatic use.

Chapter 4, Basic Pattern Matching, gives you a good grasp of the language mechanism that is put into at the core of the language to address any data transformations-F# pattern matching. The chapter covers basic pattern matching features leaving data decomposition and active patterns for the following chapters.

Chapter 5, Algebraic Data Types, shows you the patterns of using the F# standard algebraic data types (tuples, discriminated unions, and records) as a better alternative to developing custom types. It covers these types composition, equality, comparison, decomposition, and augmentation.

Chapter 6, Sequences - The Core of Data Processing Patterns, acquaints you with one of the most essential arrangements of functional programming, the sequences. Sequences lay in the foundation of few fundamental functional patterns, such as lazy evaluation, sequence generators, and sequences of indefinite lengths. This chapter also puts down a blueprint of data transformation patterns taxonomy.

Chapter 7, Advanced Techniques: Functions Revisited, builds upon already covered patterns of language use in functions, pattern matching, and sequences. It introduces readers to such F# patterns as recursion, folding, memoization, and continuation passing.

Chapter 8, Data Crunching - Data Transformation Patterns, continues to dig deeper into the data transformation patterns we began uncovering in relation to sequences. You are given a full taxonomy of polymorphic data transformation patterns captured by the F# 4.0 core library. Now you are fully prepared to blueprinting your designs with the help of highly optimized high quality library function compositions, mostly avoiding custom implementations.

Chapter 9, More Data Crunching, adds to the data transformation patterns defined by F# core library arrangement data queries with F# query expressions and data parsing with the help of type providers.

Chapter 10, Type Augmentation and Generic Computations, covers two F# usage patterns based on opposite type transformations-code generalization and code specialization. You will get a demonstration of the benefits that may be achieved through the application of these patterns.

Chapter 11, F# Expert Techniques, scratches the surface of really advanced F# patterns. We walk through the use of F# type providers, concurrent and reactive programming, and wrap up with metaprogramming.

Chapter 12, F# and OOP Principles/Design Patterns, correlates the book's vision of design patterns with those of the OOP paradigm. We will see that well-known OOP design principles and specific patterns may morph, diminish, or literally seize to be in the context of functional-first paradigm.

Chapter 13, Troubleshooting Functional Code, represents an important addendum to the main subject, showing how to develop following the explorative style, how the nature of problems with F# code development significantly shifts into compile-time from run-time, and how to address some typical issues.

What you need for this book

The most friendly development platform for the readers of this book is Visual Studio 2015 because some material, as the book's title indicate, is specific to F# v4.0 or above  versions. If you already have Visual Studio 2015 Professional or above editions you are ready to go without any extra effort.

Otherwise you may install  free Visual Studio 2015 Community edition from https://www.visualstudio.com/vs/community/.

For other options available for Windows platform please visit fsharp.org at: http://fsharp.org/use/windows/.

Somewhat limited set of book's code samples will also work under Linux and Mac. For correspondent installations instructions please visit http://fsharp.org/use/linux/ and http://fsharp.org/use/mac/.

Requirements to the hardware are dictated by listed above versions of development environments.

Who this book is for

This book is for web programmers and .NET developers (C# developers and F# developers). So, if you have basic experience in F# programming and developing performance-critical applications, then this book is for you. For F# absolute beginners the comprehension curve would be rather steep, but preceding the book with one or more beginner courses from Learning F# (http://fsharp.org/learn.html) may make it more palatable.

Conventions

In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.

Elements of code such as language keywords, operators, expressions and value names are shown as follows: "In order to define recursive function let binding can be extended with modifier rec", "Literal "$42" is bound to value total"

A block of code is set as follows:

type OrderType = Sale | Refund
type Transaction = Transaction of OrderType * decimal

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

// Imperative monolithic solution a-la C/C++

#load "HugeNumber.fs"

let number = hugeNumber.ToCharArray()

URLs are shown as https://jet.com.

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: "Clicking the Next button moves you to the next screen."

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of. To send us general feedback, simply e-mail [email protected], and mention the book's title in the subject of your message. If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

You can download the code files by following these steps:

  1. Log in or register to our website using your e-mail address and password.

  2. Hover the mouse pointer on the SUPPORT tab at the top.

  3. Click on Code Downloads & Errata.

  4. Enter the name of the book in the Search box.

  5. Select the book for which you're looking to download the code files.

  6. Choose from the drop-down menu where you purchased this book from.

  7. Click on Code Download.

Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:

  • WinRAR / 7-Zip for Windows

  • Zipeg / iZip / UnRarX for Mac

  • 7-Zip / PeaZip for Linux

The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Fsharp-4.0-Design-Patterns. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.

To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.

Piracy

Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at [email protected] with a link to the suspected pirated material.

We appreciate your help in protecting our authors and our ability to bring you valuable content.

Questions

If you have a problem with any aspect of this book, you can contact us at [email protected], and we will do our best to address the problem.