TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Functional Programming in Python [pdf]

153 点作者 mseri将近 10 年前

13 条评论

delluminatus将近 10 年前
Even knowing a lot about FP, I still found this worth skimming for the esoteric Python syntax. When it comes to constructing dictionaries with list comprehensions, I would always do something like this:<p><pre><code> dict([n, 2 ** n] for n in range(5)) </code></pre> But they pointed out an actual &quot;dict comprehension&quot; that I didn&#x27;t even realize existed:<p><pre><code> { n: n ** 2 for n in range(5) } </code></pre> And there is a similar &quot;set comprehension&quot;:<p><pre><code> { n ** 2 for n in range(5) } </code></pre> Always amazes me how you can use Python for so many years and still encounter new features in the language.
评论 #9945200 未加载
评论 #9945145 未加载
评论 #9945484 未加载
agentultra将近 10 年前
Nice, short little book!<p>`compose` can be simpler:<p><pre><code> def compose(fn, *fns): def _composer(f, g): return lambda *args: f(g(*args)) return reduce(_composer, fns, fn) </code></pre> This little function is really, really cool because it allows you to build up more interesting functions by piecing together a bunch of small, useful ones.<p><pre><code> def upper(s): return s.upper() def exclaim(s): return s + &#x27;!&#x27; # instead of this really_angry = lambda s: exclaim(exclaim(upper(s))) really_angry(&#x27;napster bad&#x27;) # NAPSTER BAD!! # we can do this really_angry = compose(upper, exclaim, exclaim) really_angry(&#x27;fire good&#x27;) # FIRE GOOD!! # and import operator as op from functools import partial as p max(map(compose(p(op.add, 1), p(op.mul, 3)), (1, 2, 3, 4))) </code></pre> `compose` is a neat function and worth exploring. This is a cool book and I always hope Python gets more light shone on its FP-friendly features.
评论 #9952209 未加载
评论 #9945882 未加载
zepolud将近 10 年前
Funny how things turned out. I still remember this post[1], it was profoundly disappointing to see Guido&#x27;s way of thinking. Much of the damage was reversed but it still left an indelible impression that there&#x27;s a lack of vision for what&#x27;s going to be important if the language is to stay relevant in the future.<p>[1] <a href="http:&#x2F;&#x2F;www.artima.com&#x2F;weblogs&#x2F;viewpost.jsp?thread=98196" rel="nofollow">http:&#x2F;&#x2F;www.artima.com&#x2F;weblogs&#x2F;viewpost.jsp?thread=98196</a>
评论 #9945998 未加载
评论 #9945783 未加载
评论 #9945544 未加载
评论 #9946288 未加载
tromp将近 10 年前
<a href="https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Python_Bridge" rel="nofollow">https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Python_Bridge</a><p>&quot;Python Bridge, officially known as High Bridge, is a bridge that spans the canal between Sporenburg and Borneo Island in Eastern Docklands, Amsterdam. It was built in 2001 and won the International Footbridge Award in 2002. The bright red bridge spans 90 meters and was designed by Adriaan Geuze of the architectural firm West 8&quot;<p>Coincidentally, Amsterdam can be considered the birthplace of Python, where Guido used to work at the Center for Mathematics and Computer Science (CWI).<p>And now for some obligatory functional python. Run with<p><pre><code> python lambda.py 2&gt;&amp;1 | head -c 200 </code></pre> to avoid filling your screen with exhausted recursion depth. Notice any pattern in the output?<p><pre><code> import sys def c(j,t): sys.stdout.write(j(&#x27;.&#x27;)(&#x27;P&#x27;)) return t (lambda z:lambda y:z(z(y(lambda p:lambda n:(lambda s:lambda z:z(lambda x: lambda y:y)(lambda d:p(s)(y(s))(d)))(lambda x:lambda a:lambda s:lambda p: p(a)(lambda y:s(n(x))(y))))(lambda c:lambda a:lambda s:z(lambda y:s(c)(y) ))))(y(lambda p:lambda b:lambda t:t(c(b,p)))))(lambda s:lambda p:p(lambda x:lambda y:x)(s))(lambda f:(lambda q:q(q))(lambda x:f(lambda y:x(x)(y))))</code></pre>
评论 #9945311 未加载
melling将近 10 年前
It&#x27;s becoming more common to see &quot;Functional Programming in X&quot;. Why don&#x27;t we use functional languages like oCaml or Haskell more often? Are we making the jump in two steps instead of one? I&#x27;ve never written more than a few lines of either so I can&#x27;t tell if something &quot;better&quot; is waiting for me in functional land.
评论 #9946829 未加载
评论 #9945941 未加载
评论 #9946480 未加载
评论 #9945979 未加载
评论 #9946072 未加载
benkuykendall将近 10 年前
Yeah. Python has the functional programming features I expect of any modern language. However, I feel that Python has a lot of unneeded syntax. I always prefer apply() over * and map() and filter() over list comprehensions.<p><pre><code> func(*args) apply(func, args) [func(a) for a in collection] map(func, collection) [a for a in collection if func(a)] filter(func, collection) </code></pre> I don&#x27;t see why people use all of this special syntax.
评论 #9945316 未加载
评论 #9946417 未加载
评论 #9946049 未加载
评论 #9963645 未加载
评论 #9946372 未加载
评论 #9946294 未加载
评论 #9945297 未加载
metalliqaz将近 10 年前
Hard to tell from a quick scan, but it appears to be slightly more informative than the classic document in the python docs: <a href="https:&#x2F;&#x2F;docs.python.org&#x2F;dev&#x2F;howto&#x2F;functional.html" rel="nofollow">https:&#x2F;&#x2F;docs.python.org&#x2F;dev&#x2F;howto&#x2F;functional.html</a>
评论 #9945271 未加载
fuzzythinker将近 10 年前
Use mochi if you really want to use FP in python.<p><a href="https:&#x2F;&#x2F;github.com&#x2F;i2y&#x2F;mochi" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;i2y&#x2F;mochi</a>
评论 #9945877 未加载
Juha将近 10 年前
For anyone wanting a quick intro to fp in Python I stumbled into this awesome presentation (50 slides) about Functional Programming in Python. I especially like his short but clear examples: <a href="http:&#x2F;&#x2F;kachayev.github.io&#x2F;talks&#x2F;uapycon2012&#x2F;#&#x2F;" rel="nofollow">http:&#x2F;&#x2F;kachayev.github.io&#x2F;talks&#x2F;uapycon2012&#x2F;#&#x2F;</a> . Good intro before getting deeper into the linked book.
zkanda将近 10 年前
FP Library in Python: <a href="https:&#x2F;&#x2F;github.com&#x2F;kachayev&#x2F;fn.py" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kachayev&#x2F;fn.py</a>
JustSomeNobody将近 10 年前
Mostly OT, but I like the short form books oreilly and packt are producing. I never liked the trend in technical books where every single one had to have 6 chapters of language tutorial, etc.
stared将近 10 年前
I am curious why lists do not have &quot;.map&quot; and &quot;.filter&quot; methods? IMHO it would be so better for chaining, now using a few maps and filters is inconvenient and looks unreadable. Plus, something shorter for &quot;lambda&quot;...<p>It&#x27;s one of not too man aspects, where I prefer JavaScript (especially ES6) to Python.
评论 #9947124 未加载
TazeTSchnitzel将近 10 年前
Functional programming? In a language with <i>no tail calls</i> and no function expressions?<p>I mean sure, you can do it, but you&#x27;d have an easier time in a language which supports those.