gpt4 book ai didi

c - 有没有办法在 C 中进行柯里化(Currying)?

转载 作者:太空狗 更新时间:2023-10-29 16:20:48 24 4
gpt4 key购买 nike

假设我有一个指向函数 _stack_push(stack* stk, void* el) 的指针。我希望能够调用 curry(_stack_push, my_stack) 并取回仅采用 void* el 的函数。我想不出一种方法来做到这一点,因为 C 不允许运行时函数定义,但我知道这里有比我聪明得多的人:)。有什么想法吗?

最佳答案

我找到了 Laurent Dami 的一篇论文,其中讨论了 C/C++/Objective-C 中的柯里化(Currying):

More Functional Reusability in C/C++/Objective-c with Curried Functions

感兴趣的是它是如何在 C 中实现的:

Our current implementation uses existing C constructs to add the currying mechanism. This was much easier to do than modifying the compiler, and is sufficient to prove the interest of currying. This approach has two drawbacks, however. First, curried functions cannot be type-checked, and therefore require careful use in order to avoid errors. Second, the curry function cannot know the size of its arguments, and counts them as if they were all of the size of an integer.

该论文不包含curry() 的实现,但您可以想象它是如何使用function pointers 实现的和 variadic functions .

关于c - 有没有办法在 C 中进行柯里化(Currying)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1023261/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com