expert f# 2.0

625 600 0
expert f# 2.0

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

Syme Granicz Cisternino Expert F# 2.0 Companion eBook Available this print for content only—size & color not accurate BOOKS FOR PROFESSIONALS BY PROFESSIONALS ® Shelve in .NET User level: Intermediate–Advanced www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $10 eBook version ISBN 978-1-4302-2431-0 9 781430 224310 5 69 9 9 Expert F# 2.0 Dear Reader, Functional programming is about concise, beautiful, and powerful code; and as you master the functional way of thinking you will get a boost in productivity and great personal enjoyment out of the clarity, compositionality, and brevity of your programs. In this book, you’ll learn about F# 2.0, an exciting young language that brings functional programming to .NET. F# 2.0 offers a unique combination of function- al and object-oriented styles, has complete and seamless interoperability with .NET, and has many features and characteristics of dynamic languages but with the performance of a compiled language. Expert F# 2.0 is the single most complete and comprehensive guide to the lan- guage, including all of its advanced features such as active patterns, sequence and computation expressions, quotations, units of measure, and lazy evaluation. Whether you’re using standard functional features such as pattern matching and recursive functions or some unique F# features such as asynchronous workflows, it becomes natural to write programs that spawn threads, model probabilistic events, perform lightning-fast symbolic or numerical computations, translate and manipulate language representations, describe server and client code for web pages in a single entity, manipulate data using typed queries, draw cus- tom UI controls, and interoperate with components built on unmanaged code, to name only a few. All of these and more are demonstrated by example in this book. Many of the examples are only a few lines long, and all are crafted with care, ready for use in your applications. For functional programmers, this book is full of treasures and a whole host of practical guidance. If this is your first time with a functional language, look no further—you are in for a treat and a great journey! We wish you bon voyage and lots of fun on your exploration of this language. Don Syme, Adam Granicz, and Antonio Cisternino THE APRESS ROADMAP Expert F# 2.0 Beginning F# Don Syme, Adam Granicz, and Antonio Cisternino, Authors of Expert F# CYAN MAGENTA YELLOW BLACK PANTONE 123 C trim = 7.5" x 9.25" spine = 1.15625" 616 page count Expert F# 2.0 Don Syme, Adam Granicz, and Antonio Cisternino Take functional programming to the next level while enjoying the benefits of a language based on the .NET Framework THE EXPERT’S VOICE ® IN F# www.it-ebooks.info www.it-ebooks.info Expert F# 2.0 ■ ■ ■ Don Syme, Adam Granicz, and Antonio Cisternino www.it-ebooks.info Expert F# 2.0 Copyright © 2010 by Don Syme, Adam Granicz, and Antonio Cisternino All rights reserved. No part of this work may be reproduced or transmitted in any form or by any means, electronic or mechanical, including photocopying, recording, or by any information storage or retrieval system, without the prior written permission of the copyright owner and the publisher. ISBN-13 (pbk): 978-1-4302- 2431-0 ISBN-13 (electronic): 978-1-4302-2432-7 Printed and bound in the United States of America 9 8 7 6 5 4 3 2 1 Trademarked names may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, we use the names only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. President and Publisher: Paul Manning Lead Editor: Jonathan Hassell Technical Reviewer: André van Meulebrouck Editorial Board: Clay Andres, Steve Anglin, Mark Beckner, Ewan Buckingham, Gary Cornell, Jonathan Gennick, Jonathan Hassell, Michelle Lowman, Matthew Moodie, Duncan Parkes, Jeffrey Pepper, Frank Pohlmann, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Matt Wade, Tom Welsh Coordinating Editor: Anita Castro Copy Editor: Tiffany Taylor Compositor: Mary Sudul Indexer: Brenda Miller Artist: April Milne Cover Designer: Anna Ishchenko Distributed to the book trade worldwide by Springer-Verlag New York, Inc., 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax 201-348-4505, e-mail orders- ny@springer-sbm.com , or visit www.springeronline.com. For information on translations, please e-mail rights@apress.com, or visit www.apress.com. Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at www.apress.com/info/bulksales. The information in this book is distributed on an “as is” basis, without warranty. Although every precaution has been taken in the preparation of this work, neither the author(s) nor Apress shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the information contained in this work. The source code for this book is available to readers at www.apress.com. You will need to answer questions pertaining to this book in order to successfully download the code. www.it-ebooks.info This book is dedicated to the memory of James Huddleston, the editor at Apress who initiated this book project and encouraged the authors with his insights, loyalty, enthusiasm, and humor. Jim passed away in February 2007, an enormous loss to his family, Apress, and the authors. www.it-ebooks.info iv Contents at a Glance Contents vi Foreword xxi About the Authors xxiii About the Technical Reviewer xxiv Acknowledgments xv ■ Chapter 1: Introduction 1 ■ Chapter 2: Getting Started with F# and .NET 7 ■ Chapter 3: Creating Your First F# Program— Introducing Functional Programming 25 ■ Chapter 4: Introducing Imperative Programming 67 ■ Chapter 5: Mastering Types and Generics 97 ■ Chapter 6: Working with Objects and Modules 123 ■ Chapter 7: Encapsulating and Packaging Your Code 153 ■ Chapter 8: Mastering F#: Common Techniques 175 ■ Chapter 9: Introducing Language-Oriented Programming 207 ■ Chapter 10: Using the F# and .NET Libraries 249 ■ Chapter 11: Building Graphical User Interfaces 271 ■ Chapter 12: Working with Symbolic Representations 321 ■ Chapter 13: Reactive, Asynchronous, and Parallel Programming 355 ■ Chapter 14: Building Smart Web Applications 391 ■ Chapter 15: Working with Data 435 www.it-ebooks.info v ■ Chapter 16: Lexing and Parsing 463 ■ Chapter 17: Interoperating with C and COM 491 ■ Chapter 18: Debugging and Testing F# Programs 523 ■ Chapter 19: Designing F# Libraries 545 ■ Appendix: F# Brief Language Guide 563 Index 571 www.it-ebooks.info ■ CONTENTS vi Contents Contents at a Glance iv Foreword xxi About the Author xxiii About the Technical Reviewer xxiv Acknowledgments xv ■ Chapter 1: Introduction 1 The Genesis of F# 1 About This Book 2 Who This Book Is For 5 ■ Chapter 2: Getting Started with F# and .NET 7 Creating Your First F# Program 7 Documenting Code Using XMLDocs 9 Using let 9 Understanding Types 10 Calling Functions 11 Lightweight Syntax 12 Understanding Scope 13 Using Data Structures 14 Using Properties and the Dot-Notation 15 Using Tuples 16 Using Imperative Code 18 Using .NET Libraries from F# 19 Using open to Access Namespaces and Modules 20 www.it-ebooks.info ■ CONTENTS vii Using new and Setting Properties 21 Fetching a Web Page 22 Summary 24 ■ Chapter 3: Creating Your First F# Program— Introducing Functional Programming 25 Getting Started with F# Arithmetic 25 Basic Literals 25 Arithmetic Operators 26 Bitwise Operations 27 Arithmetic Conversions 28 Arithmetic Comparisons 29 Overloaded Math Functions 29 Introducing Simple Strings 30 Working with String Literals and Primitives 30 Building Strings 32 Working with Lists and Options 33 Using F# Lists 33 Using F# Option Values 37 Using Option Values for Control 38 Working with Conditionals: && and || 38 Defining Recursive Functions 39 Introducing Function Values 40 Using Anonymous Function Values 41 Computing with Aggregate Operators 42 Composing Functions with >> 44 Building Functions with Partial Application 45 Using Local Functions 46 Using Functions as Abstract Values 47 Iterating with Aggregate Operators 48 Abstracting Control with Functions 48 Using .NET Methods as First-Class Functions 49 www.it-ebooks.info ■ CONTENTS viii Getting Started with Pattern Matching 50 Matching on Structured Values 51 Guarding Rules and Combining Patterns 53 Getting Started with Sequences 53 Using Range Expressions 54 Iterating a Sequence 55 Transforming Sequences with Aggregate Operators 55 Which Types Can Be Used as Sequences? 56 Using Lazy Sequences from External Sources 57 Using Sequence Expressions 58 Creating Sequence Expressions Using for 58 Enriching Sequence Expressions with Additional Logic 59 Generating Lists and Arrays Using Sequence Expressions 60 Exploring Some Simple Type Definitions 60 Defining Type Abbreviations 60 Defining Records 61 Handling Non-Unique Record Field Names 62 Cloning Records 63 Defining Discriminated Unions 63 Using Discriminated Unions as Records 65 Defining Multiple Types Simultaneously 66 Summary 66 ■ Chapter 4: Introducing Imperative Programming 67 Imperative Looping and Interating 68 Simple for Loops 68 Simple while Loops 68 More Iteration Loops over Sequences 69 Using Mutable Records 69 Mutable Reference Cells 70 Avoiding Aliasing 72 Hiding Mutable Data 73 Using Mutable Locals 74 www.it-ebooks.info [...]... representations, and how to use F# active patterns to hide representations You also learn three advanced features of F# programming: F# computation expressions (also called workflows), F# reflection, and F# quotations These are used in later chapters, particularly Chapters 13 and 15 Chapter 10, “Using the F# and NET Libraries,” gives an overview of the libraries most frequently used with F#, including the NET... supported version of F# is F# 2.0, and this is the version of the language described in this book and is also the version included with Visual Studio 2010 F# shares a core language with the programming language OCaml, and in some ways it can be considered an OCaml for NET F# wouldn’t exist without OCaml, which in turn comes from the ML family of programming languages, which dates back to 1974 F# also draws... and Visual Studio debugging tools with F#, how to use F# Interactive for exploratory development and testing, and how to use the NUnit testing framework with F# code Chapter 19, “Designing F# Libraries,” gives our advice on methodology and design issues for writing libraries in F# You learn how to write vanilla NET libraries that make relatively little use of F# constructs at their boundaries in order... .238 Using F# Reflection 239 Reflecting on Types 239 Schema Compilation by Reflecting on Types .239 Using the F# Dynamic Reflection Operators .243 Using F# Quotations 244 Example: Using F# Quotations for Error Estimation 245 Resolving Top Definitions 247 Summary .248 ■ Chapter 10: Using the F# and NET Libraries... the book The book’s chapters are as follows: Chapter 2, “Getting Started with F# and NET,” begins by introducing F# Interactive, a tool you can use to interactively evaluate F# expressions and declarations and that we encourage you to use while reading this book In this chapter, you use F# Interactive to explore some basic F# and NET constructs, and we introduce many concepts that are described in more... F# Since joining Microsoft Research in 1998, he has been a seminal contributor to a wide variety of leading-edge projects, including generics in C# and the NET Common Language Runtime, F# itself, F# asynchronous programming and Units of Measure in F# He received a Ph.D from the University of Cambridge Computer Laboratory in 1999 ■ ADAM GRANICZ is the CEO of IntelliFactory, the leading provider of F#. .. viable choice for many mainstream and professional programming activities The lead designer of the F# language, Don Syme, is one of the authors of this book This book benefits from his authority on F# and NET and from all the authors’ years of experience with F# and other programming languages The Genesis of F# F# began in 2002, when Don Syme and others at Microsoft Research decided to ensure that the ML... the standard NET Framework design guidelines The appendix, F# Brief Language Guide,” gives a compact guide to all key F# language constructs and the key operators used in F# programming Because of space limitations, we only partially address some important aspects of programming with F# It’s easy to access hundreds of other libraries with F# that aren’t covered in this book, including Managed DirectX,... Beginning F# by Robert Pickering, also published by Apress, and originally titled Foundations of F# The first edition of this book was published under the title Expert F#, and the title was revised at the suggestion of the publisher 4 www.it-ebooks.info CHAPTER 1 ■ INTRODUCTION Who This Book Is For We assume you have some programming knowledge and experience If you don’t have experience with F#, you’ll... helpful for understanding F# code: with a simple mouse movement, you can examine the inferred types of the sample programs These types play a key role in understanding the behavior of the code ■ Note You can download and install F# from www.fsharp.net You can download all the code samples used in this book from www .expert- fsharp.com; they were prepared and checked with F# 2.0 As with all books, it’s . Intermediate–Advanced www.apress.com SOURCE CODE ONLINE Companion eBook See last page for details on $ 10 eBook version ISBN 978-1-43 02 - 2431 -0 9 7814 30 22 43 10 5 69 9 9 Expert F# 2. 0 Dear Reader, Functional programming is about concise, beautiful,. Processing Syntax Trees 20 1 Events 20 2 Events as First-Class Values 20 3 Creating and Publishing Events 20 4 Summary 20 5 ■ Chapter 9: Introducing Language-Oriented Programming 20 7 Using XML as a. IN F# www.it-ebooks.info www.it-ebooks.info Expert F# 2. 0 ■ ■ ■ Don Syme, Adam Granicz, and Antonio Cisternino www.it-ebooks.info Expert F# 2. 0 Copyright © 20 10

Ngày đăng: 01/08/2014, 16:14

Từ khóa liên quan

Mục lục

  • Prelim

  • Contents at a Glance

  • Contents

  • Foreword

  • About the Authors

  • About the Technical Reviewer

  • Acknowledgments

  • Introduction

    • The Genesis of F#

    • About This Book

    • Who This Book Is For

    • Getting Started with F# and .NET

      • Creating Your First F# Program

        • Documenting Code Using XMLDocs

        • Using let

        • Understanding Types

        • Calling Functions

        • Lightweight Syntax

        • Understanding Scope

        • Using Data Structures

        • Using Properties and the Dot-Notation

        • Using Tuples

        • Using Imperative Code

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan