Generic Programming – Definition of Generic Function

cgeneric-programmingprogramming-languagespython

1)

Below is a python function summation, that can perform sum of cubes/squares/.., similar operations.

def identity(k):
    return k

def cube(k):
    return pow(k, 3)

def square(k):
    return pow(k,2)

def summation(n, term):
    if n == 0:
        return 0
    else:
        return term(n) + summation(n-1, term)

def sum_cubes(n):
    return summation(n, cube)

if __name__ == '__main__':
    sum = sum_cubes(4)
    print(sum)

 """ In C, We can implement the same using function pointers. Goal is, to    
    perform similar operations(Sum of ..) using single function summation()"""  

2)

Consider, below sorting api from C,

void qsort(void *base, size_t nmemb, size_t size,
                            int (*compar)(const void *, const void *));

Here, qsort can sort data of any type, array of floats/file names in a directory/strings/…


Question:

How to define a Generic function?

Is summation a generic function?

or

Is qsort a generic function?

or

Given two examples, Is Generic function an invalid terminology?

Note: Motivation-To term qsort or any sort function that I design

Best Answer

There are several meanings to "generic".

Informal definition

"generic" in everyday language something that shares common properties but is less specific in some ways.

Under this perspective, you could consider qsort() as generic : the code of this function is able to sort any fixed size data structure for which you can define a comparison function by using the QSORT algorithm.

The same applies to your summation() function, which summarizes terms obtained using any functions with one parameter.

Formal definition

Programming languages like C++ or Java allow for generic programming with the use of templates or generics:

Definition from the C++14 standard: A template defines a family of classes or functions or an alias for a family of types.

The principle is that a class or a function's implementation can be parametrized by types.

According to this more formal point of view, qsort() is not a generic function. Its implementation doesn't need to determine any type at compilation, and its behavior is type independent. The only thing it needs, is the size of the elements being sorted, and this size is an ordinary argument that is processed at run-time.

For a language that is not statically typed such as Python, I'm not sure what to answer for summation(). I think it is not generic because its implementation and its behavior is not type dependent : this function is just a function of higher order, with the argument term being a function. It doesn't use any feature that would alter the behavior of this function based on types.

For illustration of a generic function, you could take a look at the C++ standard function std::sort() : its implementation depends on the type of its arguments (and optionally a comparison function with arguments of a determined type). By using the features of C++ templates, it can sort any container of any type, under the condition that it has the operators/member functions/traits/iterators that are required by the implementation of the generic function.

Can a dynamic typed language have generic functions

Dynamically typed language require less generic code than statically typed languages.

For example, if you have a container of objects of dynamic type, a qsort function could generically sort the container, as long as any combination of two elements in the container can be compared.

But even in such a flexible environment, generic --type dependent-- programming could be helpful. The typical use case is multimethods , where the behavior or the code depends on the type of the arguments or even the combination of types (such as for determining the intersection between two different shapes). For additional information see: