标签:
F#
F#
When I first met Isaac Abraham in 2015, I knew immediately that he shared my passion
for educating .NET developers in the art of F#. His book, which you hold in your hands,
is the text I had wanted when I first kindled my F# romance. Inside, Isaac will lead you
on a journey of F# that’s both pragmatic and relevant. If you’re a C# or Visual Basic .NET
developer, you’ll learn through a series of lessons how to apply F# to everyday .NET
development. You won’t have to wade through the deep theoretical explanations that
are often the staple of functional programming books. Instead, Isaac will guide you with
practical advice and best practices derived from his own experience as an F# developer.
I first fell in love with F# when it was still a research language being developed out of
Microsoft Research in Cambridge, England. The language was a bit rough around the
edges, but every monthly release brought a fresh set of exciting new features to explore.
The community was small but already vibrant, eager to evangelize any who would listen
about how F# was so expressive, powerful, and fun. I found myself caught up in it all.
Since F# was still very new, there weren’t many books available to help me learn the language.
Being a self-taught C# programmer, I wanted something that was light on theory
and heavy on practice, but such books hadn’t been written yet. I even made my own
meager attempt to fill that void, by writing a blog series (viewable today only via the
Internet Archive) titled, “Why I Love F#.”
Basic F# syntax, types, and functions // Define module MyCode in namespace Company.Rules (lesson 12) module Company.Rules.MyCode // Open System namespace open System // Define a simple value (lesson 4) let playerName = "Joe" // Create and unwrap a tuple (lesson 9) let playerTuple = playerName, 21 let name, age = playerTuple // Define and create a record (lesson 10) type Player = { Name : string; Score : int; Country : string } let player = { Name = playerName; Score = 0; Country = "GB" } // Function definition with copy-and-update record syntax (lessons 10, 11) let increaseScoreBy score p = { p with Score = p.Score + score } // Piping functions (lesson 11) player |> increaseScoreBy 50 |> printfn "%A" // Function with basic pattern matching and nested expressions (lesson 7, 20) type GreetingStyle = Friendly | Normal let greet style player = let greeting = match style with | Friendly -> "Have a nice day!" | Normal -> "Good luck." sprintf "Hello, player %s! %s" player.Name greeting // Partial function application (lesson 11) let friendlyGreeting = greet Friendly // Composing functions together (lesson 11) let printToConsole text = printfn "%s" text let greetAndPrint = friendlyGreeting >> printToConsole Get Programming with F# A guide for .NET developers Isaac Abraham MANNING Shelter Island For online information and ordering of this and other Manning books, please visit www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact Special Sales Department Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Email: orders@manning.com ©2018 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books we publish printed on acid-free paper, and we exert our best efforts to that end. Recognizing also our responsibility to conserve the resources of our planet, Manning books are printed on paper that is at least 15 percent recycled and processed without the use of elemental chlorine. Manning Publications Co. 20 Baldwin Road PO Box 761 Shelter Island, NY 11964 Acquisitions editor: Michael Stephens Development editor: Elesha Hyde Review editor: Aleksandar Dragosavljevic´ Technical development editor: Mark Elston Technical proofreader: Dane Balia Production editor: David Novak Copyeditor: Sharon Wilkey Proofreader: Melody Dolab Typesetter: Dottie Marsico Cover designer: Monica Kamsvaag ISBN 9781617293993 Printed in the United States of America 1 2 3 4 5 6 7 8 9 10 – EBM – 23 22 21 20 19 18 Contents Foreword v Preface viii Acknowledgments x About this book xi About the author xiv Welcome to Get Programming with F#! 1 Lesson 13 Lesson 14 Achieving code reuse in F# 149 Capstone 2 160 Unit 4 COLLECTIONS IN F# Useful collection functions 186 Lesson 15 Working with collections in F# 173 Lesson 16 Lesson 17 Maps, dictionaries, and sets 197 Folding your way to success 206 Lesson 18 Capstone 3 219 Lesson 19 Unit 5 THE PIT OF SUCCESS WITH THE F# TYPE SYSTEM Program flow in F# 231 Lesson 20 Lesson 21 Modeling relationships in F# 244 Lesson 22 Lesson 23 Lesson 24 Fixing the billion-dollar mistake 257 Business rules as code 270 Capstone 4 284 Unit 6 LIVING ON THE .NET PLATFORM Lesson 25 Lesson 26 Working with NuGet packages 310 Consuming C# from F# 299 Unit 1 F# AND VISUAL STUDIO Lesson 1 Lesson 2 Lesson 3 The Visual Studio experience 17 Creating your first F# program 25 The REPL—changing how we develop 34 Unit 2 HELLO F# Saying a little, doing a lot 47 Trusting the compiler 58 Lesson 4 Lesson 5 Lesson 6 Working with immutable data 70 Expressions and statements 81 Lesson 7 Capstone 1 92 Lesson 8 Unit 3 TYPES AND FUNCTIONS Lesson 9 Lesson 10 Lesson 11 Lesson 12 Shaping data with tuples 101 Shaping data with records 111 Building composable functions 125 Organizing code without classes 138 iii
更多简介内容
评论