废弃 f# 中的样板
我使用了废弃你的样板并且Haskell 编程语言中的 Uniplate 库,我会发现基于可区分联合的泛型编程形式非常有用。 f# 编程语言中是否有等效的库?
I've used the Scrap Your Boilerplate and Uniplate libraries in the Haskell programming language, and I would find that form of generic programming over discriminated unions to be really useful. Is there an equivalent library in the f# programming language?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
据我所知,没有;如果没有语言/编译器内置的支持,我希望唯一的选择是基于反射的版本。 (我不知道 Uniplate 是如何实现的 - 你知道吗?)
这是代码基于原始演示文稿中的示例的基于反射的版本。我没有深入思考它的局限性,但这比我想象的要简单得多。
everywhere
函数遍历任意 DU 的整个结构,并将函数f
应用于f
所处理类型的每个节点,留下所有其他节点按原样。Not that I know of; without support built-in to the language/compiler, I expect the only alternative is a reflection-based version. (I don't know how Uniplate is implemented - do you?)
Here's the code for a reflection-based version based on the example from the original presentation. I have not thought deeply about its limitations, but this was much simpler to write than I would have guessed.
The
everywhere
function traverses the entire structure of an arbitrary DU and applies the functionf
to each node that is the type thatf
works on, leaving all other nodes as-is.