O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering F#

Book Description

A comprehensive and in-depth guide to writing functional programs using F#

About This Book

  • Learn how to manage, run, and automate your servers using Puppet
  • Explore how to use F# to develop large-scale applications quickly and simply, and become more productive in today’s age of cloud computing and multi-core programming
  • This easy-to-follow guide is packed with real-world examples that will jump-start you with F# development on the .NET platform

Who This Book Is For

If you are a C# developer with a basic knowledge of F# and want to explore the functional programming paradigm further to master your F# skills, then this book is for you.

What You Will Learn

  • Understand the basics of F# and organize F# source code with Visual Studio
  • Work with F# data structures and create functional data structures in F# interoperate with C#
  • Build and use asynchronous programming patterns with F#
  • Create and use type providers that help perform data analysis from within Visual Studio
  • Develop applications with pure F# code in WPF or ASP.NET MVC
  • Find out how to perform distributed programming with ServiceBus or ZeroMQ
  • Visualize data with charts, and work with Excel and R language Type providers

In Detail

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.

Style and approach

This easy-to-follow guide with syntaxes will help you master the concepts of F#. Packed with in-depth examples of real-world uses, this book covers each topic in detail with a reference to C#, so you will understand the difference between the languages.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased 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 code file.

Table of Contents

  1. Mastering F#
    1. Mastering F#
    2. Credits
    3. About the Authors
    4. About the Reviewer
    5. www.PacktPub.com
      1. Why subscribe?
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    7. 1. Getting Started in F#
      1. Key features of F#
        1. Functional and imperative languages
      2. F# and integrated development environments
        1. Using F# with Visual Studio
          1. F# project structure
          2. F# Script File
        2. Using the F# Interactive window
          1. The Visual Studio interactive window
          2. The FSI interactive window
      3. Basic values
        1. Getting started with functions
          1. Partially applied functions
          2. Recursive functions
          3. Higher-order functions
      4. Summary
    8. 2. Functional Core with F#
      1. F# primitive types
        1. The unit type
        2. Operator precedence
        3. Custom operators
      2. Immutability, type declarations, and strong type inference
        1. Pattern matching
          1. Functions with pattern matching
          2. Pattern expressions
          3. Guard expressions
          4. Incomplete matches
          5. Wildcard pattern
        2. Tuples
          1. Value matching
        3. Records
          1. Updating records
            1. Functional update
            2. Mutable fields
          2. Field label declarations
          3. Value matching
          4. Common Language Infrastructure mutable records
        4. Union cases
        5. Options in F#
        6. Structural equality and comparison
          1. Custom equality and comparison
        7. Reference cells and side-effects
        8. Active patterns
          1. Partial active patterns
      3. Summary
    9. 3. Data Structures in F#
      1. F# Collections
        1. Sequence
          1. Sequence expressions
          2. Working with Seq functions
          3. Creating sequences
          4. Searching and finding elements
          5. Sequence generators
          6. Sequence computation operations
        2. Arrays
          1. Working with Array functions
          2. Arrays and tuples
          3. Array comprehensions
          4. Array.Parallel module
        3. Lists
          1. Tail recursion
          2. List and tuples
          3. List comprehensions
        4. Sets
        5. Map
      2. Advanced data structures
        1. Binary trees
        2. Balanced red-black trees
      3. Summary
    10. 4. Imperative Programming in F#
      1. Control structures
        1. Looping
          1. The for...to expression
          2. The for...in expression
          3. The while...do expression
        2. Conditions
          1. Working with conditions
      2. Object-oriented programming
        1. Defining a class
          1. Constructors
        2. Structs
        3. Declaring fields, properties, methods, and static methods
          1. Fields
            1. Explicit fields
          2. Properties
            1. Static properties
            2. Indexed properties
          3. Methods
            1. Static methods
            2. Abstract and virtual methods
            3. Parameters
        4. Inheritance
          1. Abstract
          2. Defining an interface
            1. Implement interface in F# types
            2. Interface inheritance
        5. Extensions
      3. Generics
        1. Using generic constructs
        2. Wildcard generic constructs
        3. Generic constraints
        4. Generic extensions
      4. Using LINQ in F#
      5. Events
        1. Declaring events
        2. Event processing
        3. Declaring delegates
      6. Observables
      7. Interop with C#
        1. Optional parameters
        2. Algebraic data types
        3. Generic extensions consumable in C#
        4. Exposing F# functionality to C#
      8. Summary
    11. 5. Asynchronous Programming
      1. Asynchronous workflows in F#
        1. Using let!
        2. Understanding async workflows
        3. Async module
          1. Async.AsBeginEnd
          2. Async.AwaitEvent
          3. Async.AwaitIAsyncResult
          4. Async.AwaitWaitHandle
          5. Async.AwaitTask
          6. Async.FromBeginEnd
          7. Async.FromContinuations
          8. Async.Start
          9. Async.StartChild
          10. Async.StartAsTask
          11. Async.StartChildAsTask
          12. Async.StartImmediate
          13. Async.SwitchToNewThread
          14. Async.SwitchToThreadPool
          15. Async.SwitchToContext
          16. Async.Parallel
          17. Async.OnCancel
      2. Actor programming with MailboxProcessor
        1. Design patterns with MailboxProcessor
          1. Type-safe MailboxProcessor, an imperative approach
          2. Type-safe MailboxProcessor, a functional approach
          3. Messages and union types
          4. Reporting results from the mailbox
          5. Agents and errors
        2. MailboxProcessor functions
        3. Implementing a non-blocking queue
      3. Reactive programming with async workflows
      4. Summary
    12. 6. Type Providers
      1. SQL type provider
        1. Preparing the test database
        2. Using the type provider
          1. Querying the data
      2. SQL entity type provider
        1. Using the type provider
          1. Querying the data
      3. OData type provider
        1. Prerequisites
        2. Using the type provider
          1. Querying the data
      4. Web service type provider
        1. Prerequisites
        2. Using the type provider
          1. Querying the data
      5. Data analysis with type providers
        1. Prerequisites
        2. Using the type provider
      6. Query builder
      7. Summary
    13. 7. Web Programming in F#
      1. ASP.NET Web API 2
      2. Suave
        1. Routing with Suave
        2. Templating with Suave
      3. WebSharper
        1. Installing WebSharper
        2. Sitelets
        3. UI.Next
      4. Fable
        1. Using Fable with React
      5. Summary
    14. 8. Application Development in F#
      1. GitHub Electron
        1. npm
        2. webpack
      2. Using React components with Electron
      3. npm start
        1. Automatic refresh on code changes
        2. Using Node APIs from the renderer process
      4. Summary
    15. 9. Testing in F#
      1. Unit testing
        1. Unit tests with NUnit
        2. Setting up the test environment
        3. Mocking
        4. F# idiomatic assertions
        5. Asynchronous tests
        6. Running NUnit tests with FAKE
        7. NUnit and Fable
      2. Property-based testing
        1. Integrating FsCheck and NUnit
        2. Automated browser testing
      3. Summary
    16. 10. Distributed Programming in F#
      1. Actor Model
        1. MailboxProcessor
      2. Akka.NET
        1. Supervision
        2. Remote actors
      3. Summary