ebook img

Get Programming with F# PDF

580 Pages·2018·8.451 MB·english
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Get Programming with F#

Get Programming with F# A guide for .NET developers Isaac Abraham MANNING ©2018 by Manning Publications Co. ISBN 9781617293993 Printed in the United States of America Contents Foreword v Preface viii Acknowledgments x About this book xi About the author xiv Welcome to Get Programming Lesson 13 Achieving code reuse in F# 149 with F#! 1 Lesson 14 Capstone 2 160 Unit 1 Unit 4 F# AND VISUAL STUDIO COLLECTIONS IN F# Lesson 1 The Visual Studio experience 17 Lesson 15 Working with collections in F# 173 Lesson 2 Creating your first F# program 25 Lesson 16 Useful collection functions 186 Lesson 3 The REPL—changing how we develop 34 Lesson 17 Maps, dictionaries, and sets 197 Lesson 18 Folding your way to success 206 Unit 2 Lesson 19 Capstone 3 219 Unit 5 HELLO F# Lesson 4 Saying a little, doing a lot 47 THE PIT OF SUCCESS WITH THE F# TYPE Lesson 5 Trusting the compiler 58 SYSTEM Lesson 6 Working with immutable data 70 Lesson 20 Program flow in F# 231 Lesson 7 Expressions and statements 81 Lesson 21 Modeling relationships in F# 244 Lesson 8 Capstone 1 92 Lesson 22 Fixing the billion-dollar mistake 257 Unit 3 Lesson 23 Business rules as code 270 Lesson 24 Capstone 4 284 TYPES AND FUNCTIONS Unit 6 Lesson 9 Shaping data with tuples 101 Lesson 10 Shaping data with records 111 LIVING ON THE .NET PLATFORM Lesson 11 Building composable functions 125 Lesson 25 Consuming C# from F# 299 Lesson 12 Organizing code without classes 138 Lesson 26 Working with NuGet packages 310 Lesson 27 Exposing F# types and functions Lesson 37 Exposing data over HTTP 439 to C# 321 Lesson 38 Consuming HTTP data 453 Lesson 28 Architecting hybrid language Lesson 39 Capstone 7 464 applications 331 Lesson 29 Capstone 5 342 Unit 9 Unit 7 UNIT TESTING Lesson 40 Unit testing in F# 477 WORKING WITH DATA Lesson 41 Property-based testing in F# 489 Lesson 30 Introducing type providers 355 Lesson 42 Web testing 501 Lesson 31 Building schemas from live data 365 Lesson 43 Capstone 8 511 Lesson 32 Working with SQL 376 Lesson 33 Creating type provider-backed Unit 10 APIs 388 Lesson 34 Using type providers in the real WHERE NEXT? world 401 Lesson 35 Capstone 6 411 Appendix A The F# community 521 Appendix B F# in my organization 527 Unit 8 Appendix C Must-visit F# resources 537 Appendix D Must-have F# libraries 543 WEB PROGRAMMING Appendix E Other F# language features 556 Lesson 36 Asynchronous workflows 425 Index 564 Foreword 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 lan- guage. 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#.” In each blog article, I attempted to present F# to everyday C# developers like myself, highlighting features that marry well with .NET development such as the F# Interactive environment, type-safe format strings, tuples, and pattern matching. I wanted to encourage others to explore this beautiful language that I’d become quite smitten with, and to teach them how to use it effectively to create .NET programs. Eventually, my blog fell by the wayside. After joining the .NET languages team at Microsoft, I could no lon- ger find the time to keep it current and gave it up. Fast-forward to today. Today, F# is more popular than ever and growing at a rapid pace. F# code is running quite literally everywhere: on desktops, mobile devices, the web, IoT devices, and in the cloud. F# has reached its fourth major version, boasts an active and fiercely passionate community, and enjoys first-class tooling support in many IDEs and editors, such as Visual Studio, Visual Studio for Mac, and JetBrains Rider. And of course, F# books are available on a wide variety of topics, from the more general pur- pose to fully scientific. F# exemplifies an aspect of programming that all too often is forgotten: programming should be fun. With Isaac as your teacher, I’m confident you’ll have an absolute blast. —DUSTIN CAMPBELL PRINCIPAL SOFTWARE ENGINEER, .NET LANGUAGES TEAM, MICROSOFT In the Introduction to F#, Isaac mentions that F# leads you to a pit of success, which is something that I wholeheartedly agree with. I believe that the way this happens has a lot to do with how a pragmatic way of thinking, functional ideas, and a powerful run- time come together in F#. The functional ideas serve as a fantastic tool for understand- ing and modeling the problem domain in a simple way. At the same time, F# gives you a versatile toolbox for solving concrete problems within the domain. This includes a lan- guage that supports multiple paradigms including functional, object-oriented, and imperative, but also powerful .NET and F# libraries. Again, this book introduces F# from exactly the right perspective. It rightly puts a strong emphasis on thinking about the domain in a functional way, but at the same time, it introduces all of the important tools from the F# toolbox. When I started learning the F# language 10 years ago, the typical advice was that F# is just a functional programming language for .NET, so you should “forget everything you know about programming,” read a couple of academic functional programming text- books, and then figure out how to use the functional concepts to write .NET applica- tions on your own. I’m happy that this isn’t the advice we give to people who are curious about F# these days. There are two main reasons for this. First, it turns out that “forgetting everything” is just a poor strategy for learning F# if you have existing programming knowledge. Sec- ond, it also turns out that the “functional programming” label is only partly appropriate for the kind of code you write when using F# to solve real-world problems. This book reflects both points, which is what makes it a fantastic material for learning F#. The difference between F# and other .NET languages is equally easy to overstate and to understate. On one hand, many functional language constructs now exist in C# and VB .NET, so you might think that the difference isn’t that big. On the other hand, F# is rooted in functional programming and leads you to different way of thinking about problems, so you might think that you need to start from a clean slate. As is often the case, the truth is somewhere in the middle. When using F#, you’ll find yourself design- ing software differently and, as a result, falling into the pit of success described in unit 5. At the same time, learning F# shouldn’t be as daunting as it was 10 years ago. You’ll find many similarities to what you know already, and this book does a great job of using them to explain F# clearly and effectively. If you’re a C# or VB .NET developer who cares about solving problems in a pragmatic way, this is the book for you. There’s no need to wait for the “right problem” that will be “well suited to functional programming” and give you an excuse to learn F#. As this book clearly explains, the point of F# isn’t just in functional programming, but in the pit of success, where the F# way of solving problems leads you. —TOMAS PETRICEK PARTNER, FSHARPWORKS Preface Several years ago, when I was a full-time C# developer working for a .NET consultancy in the United Kingdom, I embarked on a goal to improve the quality of software I developed. I was fed up with writing software and having the customer find bugs that I felt I should have caught, and disappointed that I didn’t know how to use OO pro- gramming to model real-world problems. So I learned about SOLID, became a TDD fanatic, and read up on things like dependency injection and design patterns. And my software did improve! My clients were happier, and my managers were pleased with the lower bug rates. But soon afterward, I once again became frustrated with a lack of progress. Did we really need this amount of rigor, of process, and of unit tests in order to become proficient soft- ware developers? I knew that I’d taken a step forward in terms of quality by adopting SOLID and TDD, but I wanted to achieve it in a more productive fashion. I wanted more support from my programming language to do the right thing by default; something that guided me to the “pit of success” without my needing to use a myriad of design patterns in a specific way, and allowed me to get to the heart of a problem that I was try- ing to solve. Fast-forwarding a little, I ended up working for a financial services company working on a rules engine to monitor dodgy-looking trades—things like insider trading. We spent a great deal of time looking at how to create a component-based rules engine with pluggable “rules” that we could use to build “larger” rules. What I discovered by the end was that we had spent the last 18 months trying to implement a functional pipeline using object-oriented constructs and design patterns. So, having heard about F#, I thought I’d give it a look. I was initially interested in it as a means to an end; I’d heard that it could help me write software on .NET more quickly and with fewer bugs. I attended an intensive two-day course, and tried to use F# as much in my own time as I could. Interestingly, while learning F#, I found that it reinvig- orated me as a developer—not only was I learning something entirely new, but I could see the benefits of using the language in a tangible sense. I was able to rapidly deliver software and yet also had the confidence that what I had delivered to my customers worked, but without the rigor I’d experienced beforehand. At the same time, I found an entirely new community of .NET developers that I didn’t even know existed, with dif- ferent ideas and ways of working that made me question many of the fundamentals that I took for granted. I realized that I was excited about and enjoying software develop- ment again! Over time, I learned more about F# and functional programming; coming from a C# background, I had many preconceptions about what F# was (and wasn’t), and made lots of mistakes on the way to learning how to use F# more effectively. This book is the cul- mination of what I wish I had had when I was taking my first steps with F# while at the same time trying to find out where it fits in my day-to-day work. I couldn’t put everything that I wanted to in this book—and many of the lessons deliber- ately leave out the hard-core details that could be useful as reference material—but as long as you understand what this book is and isn’t, I think that you’ll find it a great start to the world of F# and FP on .NET. It’ll help get you up and running, building on your existing knowledge of .NET, and although it won’t necessarily take you all the way to the finish line, it’ll offer you signposts and guidance on how to get there yourself. About this book Who should read this book This book takes a practical look at how to start using functional programming tech- niques with F# in production applications, using frameworks, libraries, and tools that you’re already familiar with, as well as exploring F#-specific libraries that open up all sorts of interesting options that aren’t possible today in C# and VB .NET. It’s important to note from the outset that this book won’t teach you everything about the F# language. Instead, we’ll focus on a core subset of the language that gives you the most effective return for your investment; where there’s further learning available that could be worthwhile for you, I’ll point it out. You won’t learn everything about func- tional programming, either; again, we’ll concentrate on the most important fundamen- tals from which the more advanced techniques and practices will naturally reveal themselves to you over time. So, if you’re expecting to learn the ins and outs of functors, monads, and applicatives, this book isn’t for you. If, on the other hand, you want to learn the parts that will enable you to achieve the same qualities in software that you strive for already, without spending time learning the inner workings of exactly why things work from a theoretical point of view, then keep reading! If you’re an enterprise developer on .NET, it’s likely that you’re looking to see how F# can provide tangible benefits to you in the minimum amount of time. This book focuses on showing you things that you can start using today with F#, be they frameworks that you might already know, or F#-specific libraries designed to take full advantage of F#’s powerful type system that will provide real-world benefits over existing libraries. As such, this book focuses on using Visual Studio on Windows as the primary develop- ment platform, although you can use almost (but not all) of the libraries mentioned here through other IDEs and OSs.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.