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

Product algebraic data types


In the simplest case, consider that I use the analogy of set product to combine types A and B; the result would be a set of data pairs where the first pair constituent is of type A, the second constituent is of type B, and the whole combination is a Cartesian product of A and B.

F# offers two product algebraic data types, that is, tuples and records.

Tuples

I have already touched tuples in previous chapters; now I'll go deeper into this subject.

Tuple composition

A tuple is a combination of two or more values of any type. The tuple value element type can be of anything: primitive types, other tuples, custom classes, and functions. For example, take a look at the following code line (Ch5_1.fsx):

let tuple = (1,"2",fun() ->3) 

This represents a tuple assembled from three elements of type int* string * (unit -> int).

In order to belong to the same type of tuple, two tuple values must have the same number of elements with the similar types in the order of occurrence...