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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Haskelling My Python

172 点作者 barrenko23 天前

13 条评论

brianberns20 天前
This idea comes from a functional pearl called &quot;Power Series, Power Serious&quot; [0], which is well worth reading.<p>I implemented the same thing myself in F#. [1]<p>[0]: <a href="https:&#x2F;&#x2F;citeseerx.ist.psu.edu&#x2F;document?repid=rep1&amp;type=pdf&amp;doi=4666d43d39c890cfe88630eb93e39afe3110f930" rel="nofollow">https:&#x2F;&#x2F;citeseerx.ist.psu.edu&#x2F;document?repid=rep1&amp;type=pdf&amp;d...</a><p>[1]: <a href="https:&#x2F;&#x2F;github.com&#x2F;brianberns&#x2F;PowerSeries">https:&#x2F;&#x2F;github.com&#x2F;brianberns&#x2F;PowerSeries</a>
评论 #43752071 未加载
评论 #43752907 未加载
notpushkin20 天前
Absolutely unrelated, but there’s a Haskell-like syntax for Python: <a href="https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20241205024857&#x2F;https:&#x2F;&#x2F;pyos.github.io&#x2F;dg&#x2F;" rel="nofollow">https:&#x2F;&#x2F;web.archive.org&#x2F;web&#x2F;20241205024857&#x2F;https:&#x2F;&#x2F;pyos.gith...</a><p><pre><code> f = x -&gt; raise if x :: int =&gt; IndexError x otherwise =&gt; ValueError x </code></pre> Complete with pipelines, of course:<p><pre><code> &quot;&gt; {}: {}&quot;.format &quot;Author&quot; &quot;stop using stale memes&quot; |&gt; print</code></pre>
评论 #43751834 未加载
评论 #43751716 未加载
nexo-v120 天前
I really like this idea too. Generators are one of my favorite parts of Python — super memory efficient, and great for chaining transformations. But in practice, I’ve found they can get hard to reason about, especially when you defer evaluation too much. Debugging gets tricky because you can’t easily inspect intermediate states.<p>When working with other engineers, I’ve learned to be careful: sometimes it’s better to just materialize things into a list for clarity, even if it’s less “elegant” on paper.<p>There’s a real balance between cleverness and maintainability here.
评论 #43755390 未加载
评论 #43765029 未加载
评论 #43757104 未加载
cartoffal20 天前
&gt; The $ operator is nothing but syntactic sugar that allows you to write bar $ foo data instead of having to write bar (foo data). That’s it.<p>Actually, it&#x27;s even simpler than that: the $ operator is nothing but a function that applies its left argument to its right one! The full definition is<p><pre><code> f $ x = f x </code></pre> (plus a directive that sets its precedence and association)
评论 #43752960 未加载
评论 #43755164 未加载
mark_l_watson20 天前
Well, definitely very cool, but: the Haskell code is delightfully readable while the Python code takes effort for me to read. This is not meant as a criticism, this article is a neat thought experiment.
评论 #43754415 未加载
whalesalad20 天前
Generators are one of my favorite features of Python when used in this way. You can assemble complex transformation pipelines that don’t do any actual work and save it all for one single materialization.
louthy20 天前
I&#x27;ve never written a line of python in my life, so I&#x27;m interested in how this &quot;recursive function&quot; can do anything different on each recursive call if it takes no arguments?<p><pre><code> def ints(): yield 1 yield from map(lambda x: x + 1, ints()) </code></pre> Surely it would always yield a stream of `1`s? Seems very weird to my brain. &quot;As simple as that&quot; it is not!
评论 #43751810 未加载
评论 #43751809 未加载
sanderjd20 天前
This is certainly neat. This isn&#x27;t a criticism, but I think more like an expansion on the author&#x27;s point:<p>The reason this all works is that generators plus memoization is &quot;just&quot; an implementation of the lazy sequences that Haskell has built in.
评论 #43753028 未加载
nurettin20 天前
Excited to read their next blog where they discover functools.partial and return self.
评论 #43755684 未加载
benrutter20 天前
I like this! Tiny question, is the cache at the end any different from the inbuilt functools cache?
BiteCode_dev20 天前
Or, you know, use numpy.
评论 #43756125 未加载
ltbarcly320 天前
I have no idea why this is even slightly neat? Like, it&#x27;s not surprising that it works, it&#x27;s not clever, it&#x27;s not performant, and you can&#x27;t actually code like this because it will overflow the stack pretty quickly. It doesn&#x27;t even save lines of code.<p>Alternatives that aren&#x27;t dumb:<p><pre><code> for x in range(2**256): # not infinite but go ahead and run it to the end and get back to me from itertools import repeat for x, _ in enumerate(repeat(None)): # slightly more annoying but does work infinitely </code></pre> Granted these aren&#x27;t clever or non-performant enough to excite functional code fanboys.
评论 #43767856 未加载
评论 #43752984 未加载
fp6420 天前
This is one of the reasons I hate python, it allows for so many weird things that are only borderline standardised, if at all. When I started with python decades ago, I was also all hype, list comprehensions and functional patterns everywhere, and why not monkey patch a third party library on runtime? Now I regularly see such code written by the new junior devs, painful to maintain, painful to integrate into a larger code base, most certainly failing with TorchScript, and plainly convoluted for the sake of appearing smart.<p>If you want to code it Haskell, have you considered using Haskell?
评论 #43751694 未加载