C中的高阶函数 [英] Higher order functions in C

查看:19
本文介绍了C中的高阶函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种正确"的方法可以在 C 中实现高阶函数.

Is there a "proper" way to implement higher order functions in C.

我对这里的可移植性和语法正确性以及是否有不止一种方法的优缺点感到好奇.

I'm mostly curious about things like portability and syntax correctness here and if there are more than one ways what the merits and flaws are.

我想知道如何创建高阶函数的原因是我编写了一个系统来将 PyObject 列表(您在调用 python 脚本时获得)转换为包含相同数据但以不依赖于python.h 库.所以我的计划是有一个函数来遍历一个 pythonic 列表,并在列表中的每个项目上调用一个函数,并将结果放在一个列表中,然后它返回.

The reason I want to know how to create higher order functions are that I have written a system to convert PyObject lists (which you get when calling python scripts) into a list of C structures containing the same data but organized in a way not dependant on the python.h libraries. So my plan is to have a function which iterates through a pythonic list and calls a function on each item in the list and places the result in a list which it then returns.

所以这基本上是我的计划:

So this is basically my plan:

typedef gpointer (converter_func_type)(PyObject *)

gpointer converter_function(PyObject *obj)
{
    // do som stuff and return a struct cast into a gpointer (which is a void *)
}

GList *pylist_to_clist(PyObject *obj, converter_func_type f)
{
   GList *some_glist;
   for each item in obj
   {
       some_glist = g_list_append(some_glist, f(item));
   }
   return some_glist;
}

void some_function_that_executes_a_python_script(void)
{
   PyObject *result = python stuff that returns a list;
   GList *clist = pylist_to_clist(result, converter_function);
}

为了澄清这个问题:我想知道如何在更安全、更正确的 C 语言中做到这一点.我真的很想保持高阶函数风格,但如果不同意,我非常感谢其他一些方法方式.

And to clearify the question: I want to know how to do this in safer and more correct C. I would really like to keep the higher order function style but if that is frowned upon I greatly appreciate ways to do this some other way.

推荐答案

如果您热衷于在普通 C 中执行此操作,您需要记住包含从函子的调用者处传入上下文指针的选项(高阶函数)传递给传入的函数.这使您可以模拟足够多的闭包,使事情可以轻松地工作.该指针指向什么......好吧,这取决于您,但它应该是函子 API 中的 void*(或它的众多别名之一,例如 gpointer 在 GLib 世界中或 ClientData 在 Tcl C API 中).

If you're keen on doing this in plain C, you need to remember to include the option to pass in a context pointer from the caller of the functor (the higher-order function) to the function passed in. This lets you simulate enough of a closure that you can make things work easily enough. What that pointer points to... well, that's up to you, but it should be a void* in the functor's API (or one of the many aliases for it, such as gpointer in the GLib world or ClientData in the Tcl C API).

:使用/调整您的示例:

: To use/adapt your example:

typedef gpointer (converter_func_type)(gpointer,PyObject *)

gpointer converter_function(gpointer context_ptr,PyObject *obj)
{
    int *number_of_calls_ptr = context_ptr;
    *number_of_calls_ptr++;
    // do som stuff and return a struct cast into a gpointer (which is a void *)
}

GList *pylist_to_clist(PyObject *obj, converter_func_type f, gpointer context_ptr)
{
   GList *some_glist;
   for each item in obj
   {
       some_glist = g_list_append(some_glist, f(context_ptr,item));
   }
   return some_glist;
}

void some_function_that_executes_a_python_script(void)
{
   int number_of_calls = 0;
   PyObject *result = python stuff that returns a list;
   GList *clist = pylist_to_clist(result, converter_function, &number_of_calls);
   // Now number_of_calls has how often converter_function was called...
}

这是一个简单的例子,说明如何去做,但它应该告诉你方法.

This is a trivial example of how to do it, but it should show you the way.

这篇关于C中的高阶函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆