了解和使用 Boost Phoenix 库,重点关注惰性评估

发布于 2024-09-25 17:46:15 字数 879 浏览 0 评论 0原文

我刚刚发现了 Boost Phoenix 库(隐藏在 Spirit 项目中),并且作为函数式编程风格的粉丝(但仍然是业余爱好者;对 haskell 和方案有一些小经验),我想尝试一下这个库来学习关于这个库的合理应用。

除了使用 fp 风格提高代码的表现力和清晰度之外,我特别对惰性感兴趣以低成本加速计算的评估。

一个小而简单的例子如下: 存在某种路由问题(如 tsp),它使用欧几里德距离矩阵。我们假设距离矩阵的某些值从未使用过,而有些则经常使用(因此每次调用时都动态计算它们并不是一个好主意)。现在,使用惰性数据结构保存距离值似乎是合理的。凤凰怎么可能做到这一点? (忽略这样一个事实,即我根本不需要 fp 风格编程就可以轻松完成)阅读 phoenix 的官方文档并没有让我理解到足以回答这个问题。

有可能吗? (例如,在 Haskell 中,创建 thunk 的能力保证了稍后可以计算该值,这是该语言的核心)。

将向量与 phoenix 中定义的所有惰性函数一起使用意味着什么?像我一样天真,我尝试用随机值填充两个矩阵(向量>),一个使用正常的push_back,另一个使用boost::phoenix::push_back,并尝试从这些矩阵中仅读出少量值矩阵并将其存储在容器中以供打印。懒惰的人总是空虚的。我是否以错误的方式使用phoenix/这应该是可能的?或者我是否误解了phoenix中容器/算法的功能。后一个问题的一个小线索是 FP++ 库中存在一种特殊的列表数据结构,它影响了 phoenix。

另外:

  • 你用phoenix做什么?
  • 您知道有关凤凰城的一些好资源吗? (教程、博客文章...)

感谢您的所有投入!

I just found out about the Boost Phoenix library (hidden in the Spirit project) and as a fan of the functional-programming style (but still an amateur; some small experience with haskell and scheme) i wanted to play around with this library to learn about reasonable applications of this library.

Besides the increasement of expressiveness and clarity of the code using fp-style, i'm especially interested in lazy-evaluation for speeding up computations at low costs.

A small and simple example would be the following:
there is some kind of routing problem (like the tsp), which is using a eucliedean distance matrix. We assume, that some of the values of the distance matrix are never used, and some are used very often (so it isn't a good idea to compute them on the fly for every call). Now it seems to be reasonable to have a lazy data-structure holding the distance values. How would that be possible with phoenix? (ignoring the fact that i would be easily done without fp-style-programming at all) Reading the official documentation of phoenix didn't let me understand enough to answer that.

Is it possible at all? (in Haskell for example the ability to create thunks which are guaranteeing that the value can be computed later are in the core of the language).

What does using a vector with all the lazy functions defined in phoenix mean? As naive as i am, i tried to fill two matrices (vector >) with random values, one with the normal push_back, the other with boost::phoenix::push_back and tried to read out only a small amount of values from these matrices and store them in a container for printing out. The lazy one was alway empty. Am i using phoenix in a wrong way / it should be possible? Or did i misunderstand the function of the containers/algorithms in phoenix. A small clue for the latter one is the existence of a special list-data-structure in the FP++ library, which influenced phoenix.

Additionally:

  • What are you using phoenix for?
  • Do you know some good ressources regarding phoenix? (tutorials, blog entries...)

Thanks for all your input!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2024-10-02 17:46:15

根据要求,我的评论(添加和小的修改)作为答案......

我确切地知道你的立场,我也玩过Phoenix(虽然我没有深入挖掘,主要是阅读Boost的副产品:: Spirit教程)不久前,在发现功能错误并学习基本的Haskell之后不久 - 我没有得到任何工作:(顺便说一句,这与我对黑暗模板魔法的一般经验同步:非常容易误解,搞砸了并因完全意想不到的行为或难以理解的错误消息而受到打击,

我建议您长期远离 Phoenix 我也喜欢 FP,但 C++ 中的 FP 甚至比 Haskell 中的可变性更丑陋。正面交锋,但 C++ 已经很丑陋了,而 Haskell,至少根据 Larry Wall 的说法,是有史以来最美丽的语言;))。学习并使用 FP,当你擅长它并被迫使用 C++ 时,请使用 Phoenix。但对于学习而言,不建议在已经很复杂的语言(即 C++ 中的 FP)上使用完全不同的范例的库。

As requested, my comment (with additions and small modifications) as an answer...

I know your position exactly, I too played around with Phoenix (although I didn't dig in very deeply, mostly a byproduct of reading the Boost::Spirit tutorial) a while ago, relatively soon after catching the functional bug and learning basic Haskell - and I didn't get anything working :( This is btw in synch with my general experience with dark template magic: Very easy to misunderstand, screw up and get punched in the face with totally unexpected behaviour or incomprehensible error messages.

I'd advice you to stay away from Phoenix for a long time. I like FP too, but FP in C++ is even uglier than mutability in Haskell (they'd be head to head but C++ is already ugly and Haskell is, at least according to Larry Wall, the most beautiful language ever ;) ). Learn and use FP, and when you're good at it and forced to use C++, use Phoenix. But for learning, a library that bolts a wholly different paradigm on an already complex language (i.e. FP in C++) is not advisable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文