You are previewing F# for C# Developers.
O'Reilly logo
F# for C# Developers

Book Description

Extend your C# skills to F#—and create data-rich computational and parallel software components faster and more efficiently. Focusing on F# 3.0 and Microsoft Visual Studio 2012, you’ll learn how to exploit F# features to solve both computationally-complex problems as well as everyday programming tasks.

Topics include: C# and F# data structures; F# for functional, object-oriented, and imperative programming; design patterns; type providers; and portable support for Windows 8. You’ll examine real-world applications, including Windows 8-style HTML5 and JavaScript apps, along with cloud and service apps. You’ll write your own type provider. And you’ll see how to expand F# computation power to high-performance GPU computing.

Table of Contents

  1. Special Upgrade Offer
  2. A Note Regarding Supplemental Files
  3. Foreword
  4. Introduction
    1. Who Should Read This Book
    2. Who Should Not Read This Book
    3. Organization of This Book
      1. Finding Your Best Starting Point in This Book
    4. Conventions and Features in This Book
    5. System Requirements
    6. Code Samples
      1. Installing the Code Samples
      2. Using the Code Samples
    7. Acknowledgments
    8. Errata & book support
    9. We want to hear from you
    10. Stay in touch
  5. I. C# and F#
    1. 1. C# and F# Data Structures
      1. Basic Data Types
        1. Triple-Quoted Strings
        2. Variable Names
      2. Flow Control
        1. for Loop
        2. while Loops
        3. if Expressions
        4. Match
        5. Console Output
      3. Run Your Program
        1. Creating a Console Application
        2. Using F# Interactive
          1. FSIAnyCPU
        3. FSI Directives
        4. Compiler Directives
      4. Some Useful Add-ins
      5. List, Sequence, and Array Data Structures
        1. Lists
        2. Sequences
        3. Arrays
        4. Pipe-Forward Operator
        5. The Sequence, List, and Array Module Functions
          1. length
          2. exists and exists2
          3. forall and forall2
          4. find
          5. map
          6. filter
          7. fold
          8. collect
          9. append
          10. Math Operations
          11. zip and zip3
          12. rev
          13. sort
          14. Convert to Seq/List/Array
          15. Convert from Seq/List/Array
        6. What Changed
      6. Other F# Types
        1. Defining Constants by Using Attributes
        2. Enumerations
        3. Tuples
        4. Functions
          1. Recursive Functions
        5. Pipe/Composite Operators
        6. Unit Types
        7. Type Aliases
        8. Type Inferences
        9. Interop and Function Parameters
        10. Module, Namespace, and Program Entry Points
    2. 2. Using F# for Object-Oriented Programming
      1. Using Classes
        1. Adding Fields
        2. Defining a Property
        3. Defining a Method
        4. Defining a Static Method
        5. Using Constructors
        6. Creating an Indexer
        7. Using a Self-Identifier
        8. Using a Special/Reserved Member Name
      2. Using Inheritance
      3. Using Abstract and Sealed Classes
      4. Creating an Instance
      5. Using Type Casting
        1. Converting Numbers and Using enum
        2. Upcasting and Downcasting
        3. Boxing and Unboxing
      6. Defining an Interface
        1. Using the IDisposable Interface
      7. Using F# Generic Types and Constraints
      8. Defining Structure
      9. Using Extension Methods
      10. Using Operator Overloading
      11. Using Delegates and Events
      12. Interoperating with a C# Project
        1. Adding a Reference
        2. Using AssemblyInfo
      13. Real-World Samples
        1. Using the WPF Converter
        2. Using ObservableCollection with List Features
    3. 3. F# and Design Patterns
      1. Using Object-Oriented Programming and Design Patterns
      2. Working with F# and Design Patterns
        1. Working with the Chain of Responsibility Pattern
        2. Working with the Adapter Pattern
        3. Working with the Command Pattern
        4. Working with the Observer Pattern
        5. Working with the Decorator Pattern
        6. Working with the Proxy Pattern
        7. Working with the Strategy Pattern
        8. Working with the State Pattern
        9. Working with the Factory Pattern
        10. Working with the Singleton Pattern
        11. Working with the Composite Pattern
        12. Working with the Template Pattern
        13. Working with the Private Data Class Pattern
        14. Working with the Builder Pattern
        15. Working with the Façade Pattern
        16. Working with the Memento Pattern
      3. Writing Design Patterns: Additional Notes
  6. II. F#’s Unique Features
    1. 4. Type Providers
      1. Using the LINQ-to-SQL Type Provider
        1. SQL Type Provider Parameters
      2. SQL Entity Type Provider
        1. SQL Entity Type Provider Parameters
      3. WSDL Type Provider
        1. WSDL Type Provider Parameters
      4. OData Type Provider
        1. OData Type Provider Parameters
      5. Other Type Providers
      6. Query
        1. Using the select Operator
        2. Using the where Operator
        3. Using the join Operator
        4. Using the sortBy Operator
        5. Using the group Operator
        6. Using the take and skip Operators
        7. Using the min/max, average, and sum Operators
        8. Using the head, last, and nth Operators
        9. Using the count and distinct Operators
        10. Using the contains, exists, and find Operators
        11. Using the exactlyOne and all Operators
        12. SQL Query and F# Query
        13. Other F# Operators
      7. Using a Type Provider to Connect to the Windows Azure Marketplace
        1. Setting Up the Azure Account
        2. Connecting To and Consuming Data
        3. Performing Translations with Microsoft Translator
        4. Storing Data Locally
          1. Performing Serialization
          2. Exporting to Microsoft Excel
          3. Generating a Word Document
    2. 5. Write Your Own Type Provider
      1. What Is a Type Provider?
      2. Setting Up the Development Environment
      3. Exploring the HelloWorld Type Provider
      4. Using the Regular-Expression Type Provider
      5. Using the CSV Type Provider
      6. Using the Excel-File Type Provider
      7. Using the Type-Provider Base Class
        1. Sharing Information Among Members
        2. Using a Wrapper Type Provider
      8. Using the Multi-Inheritance Type Provider
      9. Using the XML Type Provider
      10. Using the DGML-File Type Provider
      11. Separating Run Time and Design Time
      12. Generated Type Provider
      13. Using Type-Provider Snippets
      14. Type-Provider Limitations
    3. 6. Other Unique Features
      1. Working with Reference Cells
      2. Working with Object Expressions
      3. Working with Options
      4. Working with Units of Measure
      5. Working with Records
        1. Using the CLIMutable Attribute
        2. Comparing a Record with Other Data Structures
      6. Working with Discriminated Unions
      7. Working with Comparison Operations for a Record, Tuple, and DU
      8. Using Pattern Matching
        1. Using the Tuple Pattern
        2. Using the List and Array Patterns
        3. Using the NULL Pattern
        4. Using the Record and Identifier Patterns
        5. Working with the And/Or Pattern and Pattern Grouping
        6. Using Variable Patterns and the when Guard
        7. Using the Type Pattern and as pattern
        8. Using the Choice Helper Type
      9. Working with Active Patterns
        1. Using Single-Case Active Patterns
        2. Using Partial-Case Active Patterns
        3. Using Multicase Active Patterns
        4. Using Parameterized Active Patterns
      10. Working with Exceptions
        1. Catching Exceptions
        2. Throwing Exceptions
        3. Defining Exceptions
      11. Working with a Generic Invoke Function
        1. Using the inline function
      12. Working with Asynchronous and Parallel Workflows
        1. Using Threads
        2. Using Asynchronous Workflows
          1. Working with the Asynchronous Programming Model
          2. Handling Exceptions
          3. Using Cancellation
          4. Building Your Own Asynchronous Primitives
          5. Debugging Multithreaded Applications
        3. Using Agents
      13. Working with Computation Expressions
        1. Using Computation Expression Attributes
        2. Using Computation Expression Sample
      14. Using Reflection
        1. Defining Attributes
        2. Working with Type and Member Info
        3. Using Reflection on F# Types
          1. Using Tuples
          2. Using Functions Related to Discriminated Unions
          3. Using Functions Related to Records
          4. Using Exceptions with F#
          5. Function
          6. Checking F# Types
      15. Working with Code Quotation
      16. Working with the Observable Module
      17. Using Lazy Evaluation, Partial Functions, and Memoization
      18. Summary
  7. III. Real-World Applications
    1. 7. Portable Library and HTML/JavaScript
      1. Developing Windows Store Applications with F#
        1. Creating an F# Portable Library
        2. Using the CompiledName Attribute
        3. Exploring the Portable Library Samples
          1. Developing the WinRT Application
          2. Using an Array, List, and Seq
            1. Sorting data types
            2. Using a quick-sort algorithm
            3. Performing a merge sort
            4. Finding a sum from an array
            5. Merging two sorted arrays
            6. Finding the maximum sum of an array
            7. Finding the median of two sorted arrays
            8. Dealing with the Dutch national flag problem
            9. Finding the longest increasing sequence
          3. Creating a Tree Structure
            1. F# tree representation and traversal
            2. F# binary tree and in-order traversal
            3. Binary search tree
            4. Delete a tree
            5. Build a tree from pre-order and in-order sequences
            6. Build a BST from a pre-order iteration list
            7. Check that a BST has only one child
            8. Find the common elements from two BSTs
            9. Find the tree diameter
            10. Find the lowest common ancestor
            11. Validate the sum of all child nodes
            12. Calculate the tree boundary
          4. Using String Operations
            1. Substring
            2. Palindrome in a string
            3. Permutations of a string
            4. String interleaving
            5. KMP string search algorithm
          5. Using the Graph Library
            1. Depth-first search
            2. Breadth-first search
            3. Find all paths in a graph
            4. A* algorithm
            5. Dijkstra algorithm
          6. Examining Other Samples
            1. Combination
            2. Phone keyboard
            3. Shuffle algorithm
            4. Reservoir sampling
            5. Check intersecting line segments
            6. Find triangles
            7. Neural network
          7. Using the Continuation Passing Style
      2. Working with HTML5 and WebSharper
        1. Creating an ASP.NET Website with WebSharper
        2. Using a Formlet Type to Get Input and Generate Output
        3. Using a Formlet Type as a Wizard
        4. Creating an HTML5 Page
    2. 8. Cloud and Service Programming with F#
      1. Introducing Windows Azure
        1. Setting Up Your Environment
        2. Developing a Windows Azure Application
          1. Azure Cloud Queue
          2. Azure WCF Service
          3. Azure Blob Storage
          4. Azure SQL Database
          5. Code Snippet for Azure Development
      2. MapReduce
        1. MapReduce Design Patterns
      3. Genetic Algorithms on Cloud
        1. Understanding Genetic Algorithms
          1. Selection
          2. Crossover and Mutation
          3. Elitism
        2. Azure Communication
          1. Setting Up the Service from Azure Management Portal
          2. Service-Side Code
          3. Client-Side Code
        3. Genetic Algorithms in the Cloud
    3. 9. GPGPU with F#
      1. Introducing GPU and GPGPU
      2. CUDA
        1. CUDA Toolkit
          1. cuRAND Library
          2. cuBLAS Library
      3. F# Quotation and Transform
      4. F# Quotation on GPGPU
        1. Pascal Triangle
      5. Using Binomial Trees and the BOPM
      6. Maximum Values in Subarrays
      7. Using the Monte Carlo Simulation to Compute the π Value on a GPU
      8. Useful Resources
      9. In Closing
    4. A. About the Author
  8. Index
  9. About the Author
  10. Special Upgrade Offer
  11. Copyright