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

查看:132
本文介绍了用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列表到含有相同的数据C结构的列表(你调用的Python脚本时获得),但组织的方式不依赖于在python.h库。所以,我的计划是有它通过Python的列表迭代函数,并呼吁列表中的每个项目的功能和结果放在一个列表,它然后返回。

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);
}

和以clearify的问题:我想知道如何做到这一点的更安全,更正确的C.我真的想保持高阶函数的风格,但如果是在我非常AP preciate方式皱起了眉头做一些其他的方式。

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(或它的众多别名之一,如为 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天全站免登陆