Higher order functions

Higher order function is powerful concept, it takes one or more functions as an input(argument) and outputs a function as return. It can make some complex problem become easy. Many languages support this concept and 

also has some built-in functions like map and filter. They use these functions in a very well-defined way to accomplish specific behaviors. Higher-order functions allow to implement much more sophisticated behaviors and can use it to write much more interesting programs. Have a try first. 

In math higher-order functions are known as operators or functionals. Map a function to another function. Many functional languages has map funcion, it takes as arguments function f and a list of elements, and apply each element of the list to f. In C and Java, some sorting functions take comparison function as a parameter, like standard functionqsort as an example.


Some python code of higher-order function for fun:

'''
Created on Jun 27, 2014

@author: will
'''

# Higher order functions.

def double(val): 
    return 2 * val

def square(val):
    return val ** 2

print double(3)
print square(3)

def twice(func, val):
    return func(func(val))

print twice(double, 3)
print twice(square, 3)

data = [1, 3, 6, 9, 18]
newdata = [double(item) for item in data]
print newdata

newdata2 = map(square ,data)
print newdata2

def even(val):
    if val % 2:
        return False
    else:
        return True

newdata3 = filter(even, data)
print newdata3

# Get the area of a curve, it is just approximate the result
def area(func, low, high, stepsize):
    total = 0.0
    loc = low
    while loc < high:
        total += func(loc) * stepsize
        loc += stepsize
    
    return total

def f(x):
    return x

def g(x):
    return x ** 2

def h(x):
    if x < 3:
        return x
    elif x < 7:
        return x ** 2
    else:
        return 7 * x - 4
    
print area(f, 0, 10, .01)
print area(g, 0, 10, .0001)
print area(h, 0, 10, .001)


C higher-order function to compute integral:

#include <stdio.h>

double integral(double f(double x), double a, double b, int n)
{
    // Compute the integral of f() with the interval [a, b]
    double sum = 0, dt = (b - a) / n;
    int i;
    for (i = 0; i < n; i++)
    {
        sum += f(a + (i + .5) * dt);
    }
    return sum * dt;
}

double f(double x)
{
    return 3 * x * x;
}

int main()
{
    double integ = integral(f, 0, 1, 100);
    printf("The integral is: %g\n", integ);
    return 0;
}

Reference:

http://en.wikipedia.org/wiki/Higher-order_function


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值