C language and functional programming 1 (wrapper function)

Source: Internet
Author: User

I. C language and functional programming model (funcitonal programming)

1) the C language provides support for funcitonal programming through function pointers (function pointers can be used as parameters or return values ).

2) but it is far from powerful enough. It does not support closures and nested definitions. It is far from reaching the realm of the first class function (high order function) in funcitonal programming.


Ii. Explanation of function pointers in C Language Standards
1) function pointers can be converted to another type, that is, from one type of function pointer to another type.

2) but does not support calls of incompatible function pointers (two function pointers have different return values), which may damage the stack.


Iii. Example (wrapper function)
1) TaskCreate a thread and the entry function is entryPoint. The function type of entryPoint is different from that of pthread_create.
2) directly performing conversions may introduce the risks mentioned above.
3) encapsulate an entry_wrapper function and pass entryPoint to pthread_create as the thread entry function parameter.


The source code is as follows:
Void * entry_wrapper (void (* entryPoint) (void) // ljc define a wrapper funciton, (tycast different signature function pointer is OK, but call it use another type may have upt stack)
{
// EntryPoint ();
EntryPoint ();
Return NULL;

}


Int TaskCreate (UINT8 sName [], UINT32 dStackSize, void (* entryPoint) (void), INT32 dPriority, void * pPara, UINT32 * pTaskId)
{

Pthread_t thread;
Pthread_attr_t attr;
Struct sched_param param_sched;


Param_sched.sched_priZ shard? Http://www.bkjia.com/kf/ware/vc/ "target =" _ blank "class =" keylink "> cipher + CnJldHVybiAtMTs8YnI + CnByaW50Zig =" % s % d \ n ",__ FUNCTION __, __line __, PTHREAD_STACK_MIN );
Printf ("% d \ n", pthread_attr_setstacksize (& attr, (size_t) dStackSize ));
Printf ("% d \ n", pthread_attr_setschedpolicy (& attr, SCHED_RR ));
Printf ("% d \ n", pthread_attr_setschedparam (& attr, & param_sched ));
If (pthread_attr_setstacksize (& attr, (size_t) dStackSize )! = 0) | (pthread_attr_setschedpolicy (& attr, SCHED_RR )! = 0) | (pthread_attr_setschedparam (& attr, & param_sched )! = 0 ))
Return-1;
Printf ("% s % d \ n" ,__ FUNCTION __,__ LINE __);
// If (pthread_create (& thread, & attr, (void * (*) (void *) entryPoint, pPara )! = 0) // ljc when startroutine over, it will auto call pthread_exit; take startroutine's return value as it's exit status
If (pthread_create (& thread, & attr, (void * (*) (void *) entry_wrapper, entryPoint )! = 0)
Return-1;
If (pthread_attr_destroy (& attr )! = 0)
Return-1;
If (pTaskId)
* PTaskId = (UINT32) thread;
Return 0;
}


4. high order function)
1) entry_wrapper has limitations. If TaskCreate is defined as follows:
Int TaskCreate (UINT8 sName [], UINT32 dStackSize, void (* entryPoint) (void *), INT32 dPriority, void * pPara, UINT32 * pTaskId)

That is, if pPara is not empty, pPara in this example cannot be used to pass entryPoint, and the above entry_wrapper will not work properly.


2) the policy for solving this problem can be used for higher-order functions (receiving a function as a parameter and returning a new function pointer). The definition of higher-order functions must meet one of the following conditions:
Function as the input of another function, that is, the parameter
Function as the output of another function, that is, the return value
Lua code example for higher-order functions:
Function newCounter ()
Local I = 0
Return function () -- anonymous function
I = I + 1
Return I
End
End
3) The implementation of higher-order functions is relatively complicated due to the limitations of the C language itself, and will be further explained later.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.