r/Python • u/RichKatz • Sep 29 '16
O'Reilly offers "Functional Programming in Python" by David Mertz as free eBook.
http://www.oreilly.com/programming/free/functional-programming-python.csp?cmp=em-prog-free-na-lgen_safari_2016092939
u/madduffy Sep 29 '16
Functional Programming in Python (2015)
Table of contents:
(Avoiding) Flow Control 1
Encapsulation 1
Comprehensions 2
Recursion 5
Eliminating Loops 7
Callables 11
Named Functions and Lambdas 12
Closures and Callable Instances 13
Methods of Classes 15
Multiple Dispatch 19
Lazy Evaluation 25
The Iterator Protocol 27
Module: itertools 29
Higher-Order Functions 33
Utility Higher-Order Functions 35
The operator Module 36
The functools Module 36
Decorators 37
49 pages total
1
24
u/ConciselyVerbose Sep 29 '16
I don't have useful commentary to add, but I want you to know that someone used it, at least, so thanks for the link.
2
15
u/lead999x learning Rust, Haskell, and C++ Sep 30 '16 edited Sep 30 '16
I feel like Python is definitely not the language to be learning functional programming in. I've recently started learning Haskell and it really does open your mind up a certain way. And if anything learning Haskell will make you reason better when you write Python or C++ after learning it. Haskell is just a sexy language altogether. And I don't say things like that often.
8
u/keypusher Sep 30 '16
I don't think this book's intention is to teach people functional programming. It's more geared towards people that have already understood the interesting things that come out of languages such as Lisp and Haskell, but would like to apply some of those things to a language they use in production every day such as Python.
1
u/lead999x learning Rust, Haskell, and C++ Sep 30 '16
That makes sense. I think the major strength in Python is just how practical it can be and quickly you can write working code in it.
1
Oct 01 '16
[removed] — view removed comment
1
u/lead999x learning Rust, Haskell, and C++ Oct 01 '16
Yeah it really is. And it's syntax looks like straight math which for some people makes a lot of sense.
1
Oct 01 '16
[removed] — view removed comment
2
u/lead999x learning Rust, Haskell, and C++ Oct 01 '16
It's interesting but for me a big turn off is that it's on the Java platform. Also it looks like there's a lot of boilerplate code which might not be there in Haskell. I think the only JVM languages that I've seen and liked so far are Kotlin and Clojure.
1
Oct 01 '16 edited Oct 01 '16
[removed] — view removed comment
1
u/lead999x learning Rust, Haskell, and C++ Oct 02 '16
Admittedly I haven't seen much of Kotlin, I just like what I did see. I like Haskell because it's new and it does the same old tasks in a more intuitive manner. And because it's a functional language that isn't a Lisp.
16
u/DonaldPShimoda Sep 29 '16
I'm excited to check out this book when I've got a chance!
That said, here's a related SO post about "Why isn't Python very good for functional programming?"
18
u/ubernostrum yes, you can have a pony Sep 30 '16
It's worth noting that most "Python is bad for FP" views -- including the one you linked -- really boil down not to "Python is bad at FP", but rather "Python allows this to coexist with other paradigms of programming". Most of the things people then say they'd want to make Python "better" at FP ultimately are "turn Python into a single-paradigm FP language", not "keep Python multi-paradigm, but improve the FP constructs".
14
u/DonaldPShimoda Sep 30 '16
Hmm but some of the fundamentals aren't quite right. Like tail recursion — that's a pretty big deal for FP, and Python is bad at it by design.
Don't get me wrong; I love Python! It's my preferred language, in fact. But because it wasn't built to be functional from the beginning, it will likely never be able to be used as a "good" functional language.
11
u/wting Sep 30 '16
Hmm but some of the fundamentals aren't quite right. Like tail recursion — that's a pretty big deal for FP, and Python is bad at it by design.
Disclaimer: The definition of FP is a ambiguous and thus quite open to interpreation.
I think TCO is a bit of a red herring. Haskell only does it when compiled with
-O2, Scala requires function annotations. Clojure defaults to TCO, and most but not all Lisp dialects support TCO in some form. In practice, Haskell generally prefers folds over recursive calls + TCO.Also you can hack in Python with a decorator.
9
u/DonaldPShimoda Sep 30 '16
Sure, right, I know there's no "correct" definition of what constitutes FP. But if you ask any functional programmer for a list of techniques in their everyday toolbar, I can about guarantee that recursion will be in there.
Maybe TCO isn't handled by default in some of them, but Haskell certainly doesn't have a max recursion depth of 1000 (pretty sure that's Python's... on mobile now or I'd double-check).
And yeah, I've used that decorator setup before. It's definitely neat, and the fact that things like that are so easy to accomplish is part of why I love Python! But it's fundamentally "unpythonic" — Guido would hate it!
To me what makes Python "bad" for functional is that if you were to deliberately write a whole program in functional style, Python would not go out of its way to help you. But I think that's fine — because writing purely functional code would be completely unpythonic! So I think the language is perfectly capable of writing some bits in a functional way, but it's sort of like emulating classes in C with structs: possible, but maybe a little painful.
(Before this goes any further, I just want to explicitly state that I respect your opinion and I'm not trying to argue; I'm just having a discussion because I enjoy talking about these things!)
2
4
u/wting Sep 30 '16
Most of the things people then say they'd want to make Python "better" at FP ultimately are "turn Python into a single-paradigm FP language", not "keep Python multi-paradigm, but improve the FP constructs".
I think this is a really valuable insight. The reality is when designing a language you can encourage pardigms by making it easier / more difficult. At the end of the day, GvR doesn't like FP and he will not make it easier to do in Python.
In fact, lambdas and functools only made it into Python because of an early contributor during Python 1.x or 2.2 days and people used it. Now GvR's stuck with it for backwards compatibility purposes.
I code FP-style Python when it's appropriate, but I wish people wouldn't kid themselves and pretend Python's a first-class functional language (or will ever be).
4
u/ubernostrum yes, you can have a pony Sep 30 '16
Some of the suggestions in the post aren't bad, but would change the character of the language.
Others -- like automatic currying -- probably can't be integrated into Python in any useful way (automatic currying and variadic functions don't play super nice together, and if forced to choose one or the other I'd keep variadic functions easy and make currying something you have to do explicitly).
7
u/DavidMertz Sep 30 '16
I quite like http://toolz.readthedocs.io/en/latest/curry.html. I didn't know about this library when I wrote the book, but it is very good.
1
u/hanpari Sep 30 '16
And what about pyrsistent? I believe toolz and pyrsistent are quite fundamental for FP in Python :) I thought both of them were included in your book.
Maybe I would add attrs https://attrs.readthedocs.io/en/stable/
Even it is not quite FP but can help o reduce codebase significantly.
BTW, your book is great, thank you.
6
u/needed_an_account Sep 30 '16
Sigh.
I'm 100% certain that my programming style will change after reading through this.
Thanks Mertz (for real)
4
u/evilzergling Sep 29 '16
Well, I'm all down for free books/knowledge.
Thanks for sharing, I'll check it out :)
3
3
3
u/28f272fe556a1363cc31 Sep 29 '16
How do these free books from O'Reilly work? Will I be notified of updates like other books I've bought from them?
0
5
u/penguinland Sep 30 '16
Eh... I know I shouldn't complain about free stuff, but so far I'm not impressed. I'm a little over halfway through, and it seems like a lot of the book so far could be summarized as "here's how to make your code more functional if you really wanted to do that, but these idioms are inefficient, unwieldy or otherwise unPythonic and shouldn't be used in practice."
I'll probably read the whole thing, but so far don't anticipate recommending this to my coworkers.
3
Sep 30 '16 edited Sep 30 '16
I read this back when it came out, and if I remember correctly, the author even says something to that effect. You can pick and choose from the examples he gives to see if there is anything that is more efficient/clearer in a particular instance, but I believe that he admits that most of the book is about shoehorning the concepts of functional programming into Python syntax. Honestly, set/dict/list/generator expressions and their use of set builder notation is plenty functional for me. I do not usually use the constructs like map, filter and reduce unless there is a strong case to be made for doing so.
2
u/redrumsir Sep 29 '16
Is it DRM'd?
22
2
u/madduffy Sep 29 '16
Nope. PDF, MOBI, and EPUB
6
u/redrumsir Sep 29 '16 edited Sep 29 '16
PDF, MOBI and EPUB formats can have DRM features. They can be DRM-free, but that is not necessarily the case. e.g. For PDF and EPUB, consider http://www.epubsoft.com/how-to-remove-drm-from-adobe.html . Mobi is similar.
[ After looking at some responses, I googled o'reilly+drm+free and found that their stuff is always DRM free: http://shop.oreilly.com/category/ebooks.do . It's good to know. Although I've stripped DRM out of a book or two over the years ... it's annoying and difficult enough, that I just don't buy (or even do a free download) of DRM'd books anymore. ]
5
u/ConciselyVerbose Sep 29 '16
I have used OReilly for other content in the past and am fairly sure they offer DRM free for everything they sell.
1
2
u/ScyllaHide peginneeerrr Sep 30 '16
awsome thanks for sharing! 49 pages are read within 1-2 hours in the train lol
3
Oct 01 '16 edited Oct 01 '16
[removed] — view removed comment
3
u/ScyllaHide peginneeerrr Oct 01 '16
i think you should publish it.
3
2
1
1
1
u/TotesMessenger Sep 30 '16
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
[/r/functionalprogramming] O'Reilly offers "Functional Programming in Python" by David Mertz as free eBook. • /r/Python
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
1
u/CanadianJogger Sep 30 '16
Where the hell is the actual link(s), in general terms? From the link below I select a book, and just end up at its review page again. I received a "report" email, but that is where I got the list link from in the first place. What am I overlooking?
I don't want a link: I want to understand how to use their website.
1
Oct 01 '16
[removed] — view removed comment
1
u/CanadianJogger Oct 01 '16 edited Oct 01 '16
Yeah, that was how I got the email. A newsletter of some sort, that contained links, such as to a list of free ebooks. The book? Not so much. Clicking the list of books? Back to the form page. Once around the block is enough, and it asked me to sign up twice.
2
-2
u/CarpeTuna Sep 30 '16
49 pages? Better to get Functional Python Programming by Steven Lott, incredible number of examples - 361 pages.
113
u/_illogical_ Sep 29 '16
Direct Download Links:
ePub
Mobi
PDF