Thứ Bảy, 15 tháng 3, 2014

Tài liệu Functional Programming for Java Developers pptx


LINK DOWNLOAD MIỄN PHÍ TÀI LIỆU "Tài liệu Functional Programming for Java Developers pptx": http://123doc.vn/document/1041421-tai-lieu-functional-programming-for-java-developers-pptx.htm


Functional Programming for
Java Developers
Dean Wampler
Beijing

Cambridge

Farnham

Köln

Sebastopol

Tokyo
Functional Programming for Java Developers
by Dean Wampler
Copyright © 2011 Dean Wampler. All rights reserved.
Printed in the United States of America.
Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472.
O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions
are also available for most titles (http://my.safaribooksonline.com). For more information, contact our
corporate/institutional sales department: (800) 998-9938 or corporate@oreilly.com.
Editors: Mike Loukides and Shawn Wallace
Production Editor: Teresa Elsey
Cover Designer: Karen Montgomery
Interior Designer: David Futato
Illustrator: Robert Romano
Printing History:
July 2011: First Edition.
Nutshell Handbook, the Nutshell Handbook logo, and the O’Reilly logo are registered trademarks of
O’Reilly Media, Inc. Functional Programming for Java Developers, the image of a pronghorn antelope,
and related trade dress are trademarks of O’Reilly Media, Inc.
Many of the designations used by manufacturers and sellers to distinguish their products are claimed as
trademarks. Where those designations appear in this book, and O’Reilly Media, Inc., was aware of a
trademark claim, the designations have been printed in caps or initial caps.
While every precaution has been taken in the preparation of this book, the publisher and authors assume
no responsibility for errors or omissions, or for damages resulting from the use of the information con-
tained herein.
ISBN: 978-1-449-31103-2
[LSI]
1311172935
Table of Contents
Preface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . vii
1. Why Functional Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
I Have to Be Good at Writing Concurrent Programs 2
Most Programs Are Just Data Management Problems 2
Functional Programming Is More Modular 3
I Have to Work Faster and Faster 4
Functional Programming Is a Return to Simplicity 5
2. What Is Functional Programming? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
The Basic Principles of Functional Programming 8
Avoiding Mutable State 8
Functions as First-Class Values 10
Lambdas and Closures 12
Higher-Order Functions 13
Side-Effect-Free Functions 13
Recursion 14
Lazy vs. Eager Evaluation 15
Declarative vs. Imperative Programming 16
Designing Types 17
What About Nulls? 17
Algebraic Data Types and Abstract Data Types 21
Exercises 22
3. Data Structures and Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Lists 23
Maps 29
Combinator Functions: The Collection Power Tools 29
Persistent Data Structures 36
Some Final Thoughts on Data Structures and Algorithms 37
Exercises 38
v
4. Functional Concurrency . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
The Actor Model 41
Software Transactional Memory 44
Exercises 47
5. Better Object-Oriented Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
Imperative, Mutable Code 49
The Liskov Substitution Principle 50
More on Design Patterns 51
Pattern Matching 51
What Makes a Good Type? 53
Rethinking Object-Oriented Middleware 55
Exercises 55
6. Where to Go From Here . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57
Functional Tools for Java 58
A Recap 58
Exercises 60
Appendix: References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61
Glossary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
vi | Table of Contents
Preface
Welcome to Functional Programming for Java Developers
Why should a Java developer learn about functional programming (FP)? After all, hasn’t
functional programming been safely hidden in academia for decades? Isn’t object-
oriented programming (OOP) all we really need? This book explains why functional
programming has become an important tool for the challenges of our time and how
you, a Java developer, can use it to your advantage.
The recent interest in functional programming started as a response to the growing
pervasiveness of concurrency as a way of scaling horizontally, through parallelism.
Multithreaded programming (see, e.g., [Goetz2006]) is difficult to do well and few
developers are good at it. As we’ll see, functional programming offers better strategies
for writing robust, concurrent software.
An example of the greater need for horizontal scalability is the growth of massive data
sets requiring management and analysis, the so-called big data trend. These are data
sets that are too large for traditional database management systems. They require clus-
ters of computers to store and process the data. Today, it’s not just Google, Yahoo!,
Facebook, and Twitter who work with big data. Many organizations face this challenge.
Once you learn the benefits of functional programming, you find that it improves all
the code you write. When I learned functional programming a few years ago, it re-
energized my enthusiasm for programming. I saw new, exciting ways to approach old
problems. The rigor of functional programming complemented the design and testing
benefits of test-driven development, giving me greater confidence in my work. I learned
functional programming using the Scala programming language [Scala] and co-wrote
a book on Scala with Alex Payne, called Programming Scala (O’Reilly). Scala is a JVM
language, a potential successor to Java, with the goal of bringing object-oriented and
functional programming into one coherent whole. Clojure is the other well-known
functional language on the JVM. It is a Lisp dialect that minimizes the use of OOP in
favor of functional programming. Clojure embodies a powerful vision for how pro-
gramming should be done.
vii
Fortunately, you don’t have to adopt a new language to enjoy many of the benefits of
functional programming. Back in early 1990s, I used an object-oriented approach in
the C software I wrote, until I could use C++. Similarly, if you’re working with an
object-oriented language, like Java, you can still apply many of the ideas from functional
programming.
Unfortunately, much of the literature on functional programming is difficult to under-
stand for people who are new to it. This short book offers a pragmatic, approachable
introduction to functional programming. While aimed at the Java developer, the prin-
ciples are general and will benefit anyone familiar with an object-oriented language.
I assume that you are well versed in object-oriented programming and you can read
Java code. You’ll find some exercises at the end of each chapter to help you practice
and expand on what you’ve learned.
Because this is a short introduction and because it is difficult to represent some func-
tional concepts in Java, there will be several topics that I won’t discuss in the text,
although I have added glossary entries, for completeness. These topics include curry-
ing, partial application, and comprehensions. I’ll briefly discuss several other topics,
such as combinators, laziness, and monads, to give you a taste of their importance.
However, fully understanding these topics isn’t necessary when you’re new to func-
tional programming.
I hope you find functional programming as seductive as I did. Let me know how it goes!
You can learn more at the book’s catalog page (http://oreilly.com/catalog/
9781449311032/).
Conventions Used in This Book
The following typographical conventions are used in this book:
Italic
Indicates new terms, URLs, email addresses, filenames, and file extensions. Many
italicized terms are defined in the Glossary.
Constant width
Used for program listings, as well as within paragraphs to refer to program elements
such as variable or function names, databases, data types, environment variables,
statements, and keywords.
Constant width bold
Shows commands or other text that should be typed literally by the user.
Constant width italic
Shows text that should be replaced with user-supplied values or by values deter-
mined by context.
viii | Preface
This icon signifies a tip, suggestion, or general note.
This icon indicates a warning or caution.
Using the Code Examples
This book is here to help you get your job done. In general, you may use the code in
this book in your programs and documentation. You do not need to contact us for
permission unless you’re reproducing a significant portion of the code. For example,
writing a program that uses several chunks of code from this book does not require
permission. Selling or distributing a CD-ROM of examples from O’Reilly books does
require permission. Answering a question by citing this book and quoting example
code does not require permission. Incorporating a significant amount of example code
from this book into your product’s documentation does require permission.
We appreciate, but do not require, attribution. An attribution usually includes the title,
author, publisher, and ISBN. For example: “Functional Programming for Java
Developers, by Dean Wampler (O’Reilly). Copyright 2011 Dean Wampler,
978-1-449-31103-2.”
If you feel your use of code examples falls outside fair use or the permission given above,
feel free to contact us at permissions@oreilly.com.
Some of the code examples were adapted from examples provided with the Akka dis-
tribution, copyright © 2009-2011 Scalable Solutions AB. The Akka code base is covered
by the Apache 2 License.
Getting the Code Examples
You can download the code examples from http://examples.oreilly.com/
9781449311032/. Unzip the files to a convenient location. See the README file in the
distribution for instructions on building and using the examples.
Note that some of the files won’t actually compile, because they introduce speculative
concepts that aren’t supported by current compilers or libraries. Those files end with
the extension .javax. (The build process skips them.)
Preface | ix
Safari® Books Online
Safari Books Online is an on-demand digital library that lets you easily
search over 7,500 technology and creative reference books and videos to
find the answers you need quickly.
With a subscription, you can read any page and watch any video from our library online.
Read books on your cell phone and mobile devices. Access new titles before they are
available for print, and get exclusive access to manuscripts in development and post
feedback for the authors. Copy and paste code samples, organize your favorites, down-
load chapters, bookmark key sections, create notes, print out pages, and benefit from
tons of other time-saving features.
O’Reilly Media has uploaded this book to the Safari Books Online service. To have full
digital access to this book and others on similar topics from O’Reilly and other pub-
lishers, sign up for free at http://my.safaribooksonline.com.
How to Contact Us
Please address comments and questions concerning this book to the publisher:
O’Reilly Media, Inc.
1005 Gravenstein Highway North
Sebastopol, CA 95472
800-998-9938 (in the United States or Canada)
707-829-0515 (international or local)
707-829-0104 (fax)
We have a web page for this book, where we list errata, examples, and any additional
information. You can access this page at:
http://oreilly.com/catalog/0636920021667/
To comment or ask technical questions about this book, send email to:
bookquestions@oreilly.com
For more information about our books, courses, conferences, and news, see our website
at http://www.oreilly.com.
Find us on Facebook: http://facebook.com/oreilly
Follow us on Twitter: http://twitter.com/oreillymedia
Watch us on YouTube: http://www.youtube.com/oreillymedia
x | Preface
Acknowledgments
I want to think my editor at O’Reilly, Mike Loukides, who suggested that I write this
book. Brendan McNichols and Bobby Norton provided helpful feedback on drafts of
the book. Debasish Ghosh provided valuable comments on the Liskov Substitution
Principle and suggested the Olin Shivers quotes on the meaning of foldLeft and fold
Right [Shivers]. Daniel Spiewak provided invaluable feedback that helped clarify many
of the concepts in the book, such as Monads.
I have learned a lot about functional programming from fellow developers around the
world, many of whom are fellow Scala enthusiasts. Martin Odersky, Jonas Bonér, De-
basish Ghosh, James Iry, Daniel Spiewak, Simon Peyton Jones, Rich Hickey, Conal
Elliot, David Pollak, Paul Snively, and others have illuminated dark corners with their
writing, speaking, personal conversations, and code! Finally, my fellow members of the
Chicago Area Scala Enthusiasts (CASE) group have also been a source of valuable
feedback and inspiration over the last several years.
Of course, any errors and omissions are mine alone.
Preface | xi

Không có nhận xét nào:

Đăng nhận xét