Higher order functions
2014-06-27 17:47
363 查看
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:
C higher-order function to compute integral:
Reference:
http://en.wikipedia.org/wiki/Higher-order_function
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
相关文章推荐
- 《Two Dozen Short Lessons in Haskell》学习(九)- Types of Curried Forms and Higher Order Functions
- Covariance and Contravariance In C#, Part Five: Higher Order Functions Hurt My Brain
- Scala函数式程序设计原理 week2 Higher Order Functions
- tensorflow编程: Higher Order Functions
- JavaScript Functional Programming:高阶函数 Higher order functions
- Javascript—Higher Order Functions
- 函数编程之高阶函数漫谈(Higher order functions)
- Scala新手指南中文版 -第十篇 Staying DRY with higher-order functions(用高阶函数来消除重复代码)
- HigherOrderPerl
- [React] Cleanly Map Over A Stateless Functional Component with a Higher Order Component
- Higher order SVD + Tensor Grassman manifold
- [RxJS] Flatten a higher order observable with mergeAll in RxJS
- 测试链接外部图像 Higher order tensor structure
- Execution Order of Event Functions
- [RxJS] Flatten a higher order observable with concatAll in RxJS
- React高级指南(十一)【Higher Order Components】
- [React] Implement a Higher Order Component with Render Props
- [React] Higher Order Components (replaces Mixins)
- 《Higher-order organization of complex networks》-论文学习笔记
- [cvpr2017]Domain Adaptation by Mixture of Alignments of Second- or Higher-Order Scatter Tensors