고차 함수

일반적인 예

수많은 함수형 프로그래밍 언어에서 볼 수 있는 map 함수는 고차 함수의 한 예이다.

파이썬

>>> def twice(function):...     return lambda x: function(function(x))>>> def f(x):...     return x + 3>>> g = twice(f)>>> print g(7)13

F#

let twice f = f >> flet f = (+) 3twice f 7 |> printf "%A" // 13

하스켈

twice :: (a -> a) -> (a -> a)twice f = f . ff :: Num a => a -> af = subtract 3main :: IO ()main = print (twice f 7) -- 1

클로저

(defn twice [function x]
  (function (function x)))(twice #(+ % 3) 7) ;13

스킴

(define (add x y) (+ x y))(define (f x)
  (lambda (y) (+ x y)))(display ((f 3) 7))(display (add 3 7))

얼랑

or_else([], _) -> false;or_else([F | Fs], X) -> or_else(Fs, X, F(X)).or_else(Fs, X, false) -> or_else(Fs, X);or_else(Fs, _, {false, Y}) -> or_else(Fs, Y);or_else(_, _, R) -> R.or_else([fun erlang:is_integer/1, fun erlang:is_atom/1, fun erlang:is_list/1],3.23).

자바스크립트

var twice = function(f, v) {
    return f(f(v));};var f = function(v) {
    return v + 3;};console.log(twice(f, 7)); // 13

고

func twice(f func(int) int, v int) int {
return f(f(v))}func main() {
f := func(v int) int {
return v + 3
}
twice(f, 7) // returns 13}

스칼라

def twice(f:Int=>Int) = f compose ftwice(_+3)(7) // 13

자바 (1.8 이상)

Function<Function<Integer, Integer>, Function<Integer, Integer>> twice = f -> f.andThen(f);twice.apply(x -> x + 3).apply(7); // 13

스위프트

func f(x: Int) -> Int {
    return x + 3}func g(function: (Int) -> Int, paramX: Int) -> Int {
    return function(paramX) * function(paramX)}g(function: f, paramX: 7)

러스트

// Take function f(x), return function f(f(x))fn twice<A, F>(function: F) -> Box<Fn(A) -> A>    where F: 'static + Fn(A) -> A{    Box::new(move |a| function(function(a)))}// Return x + 3fn f(x: i32) -> i32 {    x + 3}fn main() {    let g = twice(f);    println!("{}", g(7));}

C++

// Generic lambdas provided by C++14.#include <iostream>auto twice = [](auto f, int v){
    return f(f(v));};auto f = [](int i){
    return i + 3;};int main(){
    std::cout << twice(f, 7) << std::endl;}// Or, use std::function in C++11#include <iostream>#include <functional>auto twice = [](const std::function<int(int)>& f, int v){
    return f(f(v));};auto f = [](int i){
    return i + 3;};int main(){
    std::cout << twice(f, 7) << std::endl;}

콜드퓨전 마크업 언어 (CFML)

twice = function(f, v) {
    return f(f(v));};f = function(v) {
    return v + 3;};writeOutput(twice(f, 7)); // 13

PHP

$twice = function($f, $v) {
    return $f($f($v));};$f = function($v) {
    return $v + 3;};echo($twice($f, 7)); // 13

R

twice <- function(func) {
  return(function(x) {
    func(func(x))
  })}

f <- function(x) {
  return(x + 3)}

g <- twice(f)> print(g(7))[1] 13

XACML

rule allowEntry{
    permit
    condition anyOfAny(function[stringEqual], citizenships, allowedCitizenships)}

함수 포인터

#include <stdio.h>double square(double x){
    return x * x;}double cube(double x){
    return x * x * x;}/* Compute the integral of f() within the interval [a,b] */double integral(double f(double x), double a, double b, int n){
    int i;
    double sum = 0;
    double dt = (b - a) / n;
    for (i = 0;  i < n;  ++i) {
        sum += f(a + (i + 0.5) * dt);
    }
    return sum * dt;}int main(){
    printf("%g\n", integral(square, 0, 1, 100));
    printf("%g\n", integral(cube, 0, 1, 100));
    return 0;}

오브젝트

program example;type
  int = integer;
  Txy = record x, y: int; end;
  Tf = function (xy: Txy): int;function f(xy: Txy): int;begin
  Result := xy.y + xy.x;end;function g(func: Tf): Tf;begin
  result := func;end;var
  a: Tf;
  xy: Txy = (x: 3; y: 7);begin
  a := g(@f);     // return a function to "a"
  writeln(a(xy)); // prints 10end.

기능상실

// Defunctionalized function data structurestemplate<typename T> struct Add { T value; };template<typename T> struct DivBy { T value; };template<typename F, typename G> struct Composition { F f; G g; };// Defunctionalized function application implementationstemplate<typename F, typename G, typename X>auto apply(Composition<F, G> f, X arg) {
    return apply(f.f, apply(f.g, arg));}template<typename T, typename X>auto apply(Add<T> f, X arg) {
    return arg  + f.value;}template<typename T, typename X>auto apply(DivBy<T> f, X arg) {
    return arg / f.value;}