mertz d. - functional programming in python - 2015.pdf

(1593 KB) Pobierz
Functional
Programming
in Python
David Mertz
Additional
Resources
4 Easy Ways to Learn More and Stay Current
Programming Newsletter
Get programming related news and content delivered weekly to your inbox.
oreilly.com/programming/newsletter
Free Webcast Series
Learn about popular programming topics from experts live, online.
webcasts.oreilly.com
O’Reilly Radar
Read more insight and analysis about emerging technologies.
radar.oreilly.com
Conferences
Immerse yourself in learning at an upcoming O’Reilly conference.
conferences.oreilly.com
©2015 O’Reilly Media, Inc. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. #15305
Functional Programming
in Python
David Mertz
Functional Programming in Python
by David Mertz
Copyright © 2015 O’Reilly Media, Inc. All rights reserved.
Attribution-ShareAlike 4.0 International (CC BY-SA 4.0).
See: http://creativecommons.org/licenses/by-sa/4.0/
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://safaribooksonline.com). For
more information, contact our corporate/institutional sales department:
800-998-9938 or corporate@oreilly.com.
Editor:
Meghan Blanchette
Production Editor:
Shiny Kalapurakkel
Proofreader:
Charles Roumeliotis
Interior Designer:
David Futato
Cover Designer:
Karen Montgomery
May 2015:
2015-05-27:
First Edition
First Release
Revision History for the First Edition
The O’Reilly logo is a registered trademark of O’Reilly Media, Inc.
Functional Pro‐
gramming in Python,
the cover image, and related trade dress are trademarks of
O’Reilly Media, Inc.
While the publisher and the author have used good faith efforts to ensure that the
information and instructions contained in this work are accurate, the publisher and
the author disclaim all responsibility for errors or omissions, including without limi‐
tation responsibility for damages resulting from the use of or reliance on this work.
Use of the information and instructions contained in this work is at your own risk. If
any code samples or other technology this work contains or describes is subject to
open source licenses or the intellectual property rights of others, it is your responsi‐
bility to ensure that your use thereof complies with such licenses and/or rights.
978-1-491-92856-1
[LSI]
Table of Contents
Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . v
(Avoiding) Flow Control. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
Encapsulation
Comprehensions
Recursion
Eliminating Loops
1
2
5
7
Callables. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Named Functions and Lambdas
Closures and Callable Instances
Methods of Classes
Multiple Dispatch
The Iterator Protocol
Module: itertools
12
13
15
19
27
29
Lazy Evaluation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Higher-Order Functions. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
Utility Higher-Order Functions
The operator Module
The functools Module
Decorators
35
36
36
37
iii
Zgłoś jeśli naruszono regulamin