Book Image

Mastering F#

By : Alfonso García-Caro Núñez, Suhaib Fahad
Book Image

Mastering F#

By: Alfonso García-Caro Núñez, Suhaib Fahad

Overview of this book

F# is a multi-paradigm programming language that encompasses object-oriented, imperative, and functional programming language properties. Now adopted in a wide range of application areas and is supported both by industry-leading companies who provide professional tools and by an active open community, F# is rapidly gaining popularity as it emerges in digital music advertising, creating music-focused ads for Spotify, Pandora, Shazam, and anywhere on the web. This book will guide you through the basics and will then help you master F#. The book starts by explaining how to use F# with Visual Studio, file ordering, and the differences between F# and C# in terms of usage. It moves on to explain the functional core of F# such as data types, type declarations, immutability, strong type interference, pattern matching, records, F# data structures, sequence expressions, and lazy evaluation. Next, the book takes you through imperative and asynchronous programming, F# type providers, applications, and testing in F#. Finally, we look into using F# with distributed programming and using F# as a suitable language for data science. In short, this book will help you learn F# for real-world applications and increase your productivity with functional programming.
Table of Contents (16 chapters)

Property-based testing


Unit tests are a tremendously useful tool to verify the robustness of our program. However, as we have seen in the earlier examples, we are always checking against single cases, and, even when automatically generating parameters for our tests, it is always difficult to be sure that there are no edge cases we may be missing.

The solution is not to test against the results of a function but against properties. This way, we can feed any value to the function, and verify that the properties are always fulfilled. We can also think of the properties as the requirements of the application. For example, we may want to verify that our add function, given earlier, fulfills the following three properties of addition:

  • Commutative property: add x y should be the same as add y x
  • Associative property: add x y |> add z should be the same as add y z |> add x
  • Identity property: add x 0 should be the same as x

Thanks to F#'s expressiveness, it is really easy to write these conditions...