次氯酸钠是什么| 蚝油是干什么用的| 一个提手一个京念什么| 什么是破伤风| 石斛有什么作用| 思量是什么意思| 想一出是一出什么意思| 什么牌子的洗面奶好用| 智齿冠周炎吃什么药| 今年80岁属什么生肖| 手淫多了有什么危害| 贻笑大方是什么意思| 木林森是什么品牌| yaoi是什么| 喉咙痛不能吃什么| 油菜花是什么季节开的| 丑什么意思| 710是什么意思| 郑和原名叫什么| 太阳是什么| 员额制是什么意思| 唐宋元明清前面是什么| 什么是职业| 牙龈萎缩吃什么维生素| 低回声结节什么意思| 什么什么深长| 澳门有什么特产| 风心病是什么病| 又热又冷是什么原因| 鲨鱼用什么呼吸| 子宫肌壁回声不均匀是什么意思| 什么是六合| 吃杨梅有什么好处和功效| 房速是什么意思| 男的为什么喜欢男的| 睾丸疼吃什么药| 房性心动过速是什么意思| 什么龙什么凤| 泡打粉是什么| 双相情感障碍是什么病| 补钙吃什么食物最好最快中老年| 辅警是什么编制| 颜值控是什么意思| 非洲人一日三餐吃什么| 避孕药什么时候吃| 发热吃什么药| 急于求成是什么意思| 番薯是什么| 停经吃什么能来月经| 花椒泡脚有什么功效| 怀孕吃什么好| 大人发烧吃什么退烧药| 直接胆红素偏低是什么原因| 笑话是什么意思| 淋病是什么| 喝黄瓜汁有什么好处| dostinex是什么药| 真性情是什么意思| 为什么会心慌| 孕妇吃维生素c有什么好处| 鸡蛋过敏什么症状| hmo是什么意思| 太阳鱼吃什么食物| 血红蛋白低吃什么可以补起来| 开口腔诊所需要什么条件| 喉咙发炎不能吃什么| 4月5日是什么星座| 新白娘子传奇许仙为什么用女的演| 猪下水是什么| 唇炎用什么药| 五四运动的精神是什么| 什么人不适合做业务员| 养肝护肝喝什么茶最好| 中队长是什么级别| 补气补血吃什么药| 天赋异禀什么意思| 手汗症挂什么科| 梦见大青蛇是什么预兆| 癸水是什么意思| 花椒木有什么作用与功效| 术后吃什么水果| 孩子一直咳嗽不好是什么原因| 荔枝有什么功效| 脑萎缩挂什么科| 档案自由可投什么意思| 复方甘草酸苷片治什么病| 异淋是什么意思| 什么叫自私的人| 名什么什么实| 八月十七是什么星座| 待产是什么意思| 喉咙痛吃什么药好| 肌酐高吃什么食物| 为什么会紫外线过敏| 鬼压床是什么原因造成的| 人生只剩归途什么意思| 指甲很薄很软是为什么| 腰肌劳损是什么意思| 不知所云是什么意思| 1994年属狗的是什么命| baron是什么意思| 月经不调去医院挂什么科| 猴子属于什么类动物| 事例是什么意思| 加息是什么意思| 1998属什么| 梦见死猪是什么意思| 脚出汗用什么药| 阳历7月份是什么星座| 受贿是什么意思| 卜在姓氏里读什么| 孕妇用什么驱蚊最安全| 天降横财什么意思| 熬粥用什么锅好| 入殓师是干什么的| 外甥像舅舅有什么说法| 验光pd是什么意思| 什么是速写| 地接是什么意思| 脚心抽筋是什么原因引起的| 取缔役什么意思| low什么意思| 巳时属什么生肖| 6个月宝宝可以吃什么水果| 咳嗽一直不好是什么原因怎么治| 孕囊是什么意思| 脑膜瘤钙化意味着什么| 龟头流脓小便刺痛吃什么药| 姑婆的老公叫什么| 爷爷的妹妹叫什么| boy是什么品牌| 犯贱是什么意思| 子宫直肠窝积液是什么意思| 胃酸胃烧心吃什么药| 什么沐浴露好用| 什么什么不安| 卫生院院长是什么级别| 荤段子是什么意思| 什么是忧郁症| 蓝莓什么时候开花结果| 县委副书记是什么级别| 咽喉发炎吃什么药| 海棠依旧什么意思| 肝实质弥漫性回声改变什么意思| 胃炎吃什么食物好得快| 正直是什么意思| 白菜什么时候播种| 舌头鱼又叫什么鱼| 睡觉流口水是什么原因引起的| 咽炎挂什么科室| 可乐鸡翅用什么可乐| 睾丸疝气有什么症状| 觉悟是什么意思| 肺部有问题一般会出现什么症状| 不加要是什么字| 82年属什么的生肖| prg是什么意思| 失眠吃什么中药调理效果快| 姑姑家的儿子叫什么| 马甲是什么| 舌根起泡是什么原因| 七喜是什么饮料| 梦见自己的哥哥死了是什么意思| karen是什么意思| 胆切除后吃什么好| 2018是什么生肖| 12月20日是什么星座| 巾帼不让须眉什么意思| 与世隔绝的绝是什么意思| 须发早白吃什么中成药| 县委办公室主任是什么级别| 肝功能八项检查什么| 龙吃什么| 热射病是什么| 焦虑症吃什么中成药| 西红柿对人体有什么好处| 阴道杆菌是什么意思| 妇女是什么意思| 包皮什么年龄割最好| 蚕蛹是什么| 农历八月十五是什么节日| mt是什么单位| 喝什么饮料解酒最快最有效| 为什么精液是流出来的| 乌龟吃什么食物| 月经过后有褐色分泌物是什么原因| 女人喝蛇汤有什么好处| 柔软对什么| 抑郁症是什么症状| 心脏供血不足用什么药| 世界上最多笔画的字是什么字| 皮肤一块块白是什么病| 三伏天吃什么最好| 息斯敏又叫什么药名| 两个人在一起的意义是什么| 桂花是什么颜色的| 菊花代表什么象征意义| 三伏天要注意什么| 鸽子喜欢吃什么| 直系亲属为什么不能输血| 户口分户需要什么条件| 什么叫犹太人| 冰粉籽是什么植物| 天冬氨酸氨基转移酶高是什么原因| 门诊是什么意思| 小朋友手指脱皮是什么原因| 为什么一照相脸就歪了| 黑枸杞泡水喝有什么作用和功效| 鲜卑人是现在的什么人| 三焦湿热吃什么中成药| 什么东西晚上才生出尾巴| 六月十九是什么星座| 什么的树影| 长疖子用什么药| 玥字五行属什么| 男生进入是什么感觉| 英姿的动物是什么生肖| 45是什么生肖| 流口水是什么原因| 两胸中间疼是什么原因| 冬天手脚冰凉是什么原因怎么调理| 过期的酸奶有什么用| mid是什么意思| 原子序数等于什么| 炸油条用什么油最好| 女人为什么会来月经| 天官是什么意思| 口干吃什么药| 1211是什么星座| 下雨天适合穿什么衣服| 男孩子送什么礼物| sod是什么意思| 存脐带血有什么用| 颈椎病应该挂什么科| hl是什么意思| 车顶放饮料是什么意思| 庙祝是什么意思| 起床眼睛肿是什么原因| instagram是什么意思| 梦见吃螃蟹是什么预兆| 脖子疼是什么原因引起的| 农历十二月是什么月| 九浅一深是什么意思| 处女座男生喜欢什么样的女生| 撒尿分叉是什么原因| 小家碧玉是什么生肖| 吐血是什么原因| 咲是什么意思| 葬花是什么意思| 姑爹是什么意思| 南宁有什么好玩的地方| 经常犯困是什么原因| 他将是你的新郎是什么歌| 什么人不能喝蜂蜜| 6月26号是什么日子| 凤凰男什么意思| 麦冬是什么植物| 后背疼吃什么药| 樱桃有什么功效| 自汗是什么意思| 16岁属什么| 甲状腺功能挂什么科| 缘是什么生肖| 高血糖能吃什么水果| 百度Jump to content

车讯:内外皆升级 新款GT-R 11月18日国内首发

From Wikipedia, the free encyclopedia
百度 ·你无需担心攻击到阿特柔斯,他很善于闪避;·阿特柔斯偶尔会被怪物抓住,身为父亲的你请务必拯救他;·奎托斯只能运用战斧、盾牌与肉搏三种战斗方式,没有其他武器选项;·对一般魔物或大型头目依然有终结技设定;·只有在掷出战斧后才能使用肉搏,肉搏打击可使敌人晕眩条快速集满,玩家可更容易使出终结技;·奎托斯的战斧扔出后,可透过△键召回并造成伤害,就像《雷神索尔》一样;·没错,很帅,超帅!够二!·故事面侧重父与子之间的情感互动,不再强调过往的复仇为主轴;·战斗锁定第三成称过肩视角,运镜也不会像以往拉远或突然拉近;·经验值与银币可用来升级装备、武器及技能,重在培育奎托斯本身的能力;前期某段剧情:会出现一位不知名的北欧神祇莫名袭击奎托斯,其战斗力与奎托斯不相上下,甚至还差点将奎托斯打趴!从复仇到父子,急转直下的剧情转变最初我们在《战神》系列制作续作时,开发总监CoryBarlog便决心要订下全新的方向。

In mathematics and computer science, a higher-order function (HOF) is a function that does at least one of the following:

All other functions are first-order functions. In mathematics higher-order functions are also termed operators or functionals. The differential operator in calculus is a common example, since it maps a function to its derivative, also a function. Higher-order functions should not be confused with other uses of the word "functor" throughout mathematics, see Functor (disambiguation).

In the untyped lambda calculus, all functions are higher-order; in a typed lambda calculus, from which most functional programming languages are derived, higher-order functions that take one function as argument are values with types of the form .

General examples

[edit]
  • map function, found in many functional programming languages, is one example of a higher-order function. It takes arguments as a function f and a collection of elements, and as the result, returns a new collection with f applied to each element from the collection.
  • Sorting functions, which take a comparison function as a parameter, allowing the programmer to separate the sorting algorithm from the comparisons of the items being sorted. The C standard function qsort is an example of this.
  • filter
  • fold
  • scan
  • apply
  • Function composition
  • Integration
  • Callback
  • Tree traversal
  • Montague grammar, a semantic theory of natural language, uses higher-order functions

Support in programming languages

[edit]

Direct support

[edit]

The examples are not intended to compare and contrast programming languages, but to serve as examples of higher-order function syntax

In the following examples, the higher-order function twice takes a function, and applies the function to some value twice. If twice has to be applied several times for the same f it preferably should return a function rather than a value. This is in line with the "don't repeat yourself" principle.

APL

[edit]
      twice{?? ?? ?}

      plusthree{?+3}

      g{plusthree twice ?}
    
      g 7
13

Or in a tacit manner:

      twice?2

      plusthree+°3

      gplusthree twice
    
      g 7
13

C++

[edit]

Using std::function in C++11:

#include <iostream>
#include <functional>

auto twice = [](const std::function<int(int)>& f)
{
    return [f](int x) {
        return f(f(x));
    };
};

auto plus_three = [](int i)
{
    return i + 3;
};

int main()
{
    auto g = twice(plus_three);

    std::cout << g(7) << '\n'; // 13
}

Or, with generic lambdas provided by C++14:

#include <iostream>

auto twice = [](const auto& f)
{
    return [f](int x) {
        return f(f(x));
    };
};

auto plus_three = [](int i)
{
    return i + 3;
};

int main()
{
    auto g = twice(plus_three);

    std::cout << g(7) << '\n'; // 13
}

C#

[edit]

Using just delegates:

using System;

public class Program
{
    public static void Main(string[] args)
    {
        Func<Func<int, int>, Func<int, int>> twice = f => x => f(f(x));

        Func<int, int> plusThree = i => i + 3;

        var g = twice(plusThree);

        Console.WriteLine(g(7)); // 13
    }
}

Or equivalently, with static methods:

using System;

public class Program
{
    private static Func<int, int> Twice(Func<int, int> f)
    {
        return x => f(f(x));
    }

    private static int PlusThree(int i) => i + 3;

    public static void Main(string[] args)
    {
        var g = Twice(PlusThree);

        Console.WriteLine(g(7)); // 13
    }
}

Clojure

[edit]
(defn twice [f]
  (fn [x] (f (f x))))

(defn plus-three [i]
  (+ i 3))

(def g (twice plus-three))

(println (g 7)) ; 13

ColdFusion Markup Language (CFML)

[edit]
twice = function(f) {
    return function(x) {
        return f(f(x));
    };
};

plusThree = function(i) {
    return i + 3;
};

g = twice(plusThree);

writeOutput(g(7)); // 13

Common Lisp

[edit]
(defun twice (f)                                                                
  (lambda (x) (funcall f (funcall f x))))                                       
                                                                                
(defun plus-three (i)                                                           
  (+ i 3))                                                                      
                                                                                
(defvar g (twice #'plus-three))                                                 
                                                                                
(print (funcall g 7))

D

[edit]
import std.stdio : writeln;

alias twice = (f) => (int x) => f(f(x));

alias plusThree = (int i) => i + 3;

void main()
{
    auto g = twice(plusThree);

    writeln(g(7)); // 13
}

Dart

[edit]
int Function(int) twice(int Function(int) f) {
    return (x) {
        return f(f(x));
    };
}

int plusThree(int i) {
    return i + 3;
}

void main() {
    final g = twice(plusThree);
    
    print(g(7)); // 13
}

Elixir

[edit]

In Elixir, you can mix module definitions and anonymous functions

defmodule Hof do
    def twice(f) do
        fn(x) -> f.(f.(x)) end
    end
end

plus_three = fn(i) -> i + 3 end

g = Hof.twice(plus_three)

IO.puts g.(7) # 13

Alternatively, we can also compose using pure anonymous functions.

twice = fn(f) ->
    fn(x) -> f.(f.(x)) end
end

plus_three = fn(i) -> i + 3 end

g = twice.(plus_three)

IO.puts g.(7) # 13

Erlang

[edit]
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).

In this Erlang example, the higher-order function or_else/2 takes a list of functions (Fs) and argument (X). It evaluates the function F with the argument X as argument. If the function F returns false then the next function in Fs will be evaluated. If the function F returns {false, Y} then the next function in Fs with argument Y will be evaluated. If the function F returns R the higher-order function or_else/2 will return R. Note that X, Y, and R can be functions. The example returns false.

F#

[edit]
let twice f = f >> f

let plus_three = (+) 3

let g = twice plus_three

g 7 |> printf "%A" // 13

Go

[edit]
package main

import "fmt"

func twice(f func(int) int) func(int) int {
	return func(x int) int {
		return f(f(x))
	}
}

func main() {
	plusThree := func(i int) int {
		return i + 3
	}

	g := twice(plusThree)

	fmt.Println(g(7)) // 13
}

Notice a function literal can be defined either with an identifier (twice) or anonymously (assigned to variable plusThree).

Groovy

[edit]
def twice = { f, x -> f(f(x)) }
def plusThree = { it + 3 }
def g = twice.curry(plusThree) 
println g(7) // 13

Haskell

[edit]
twice :: (Int -> Int) -> (Int -> Int)
twice f = f . f

plusThree :: Int -> Int
plusThree = (+3)

main :: IO ()
main = print (g 7) -- 13
  where
    g = twice plusThree

J

[edit]

Explicitly,

   twice=.     adverb : 'u u y'

   plusthree=. verb   : 'y + 3'
   
   g=. plusthree twice
   
   g 7
13

or tacitly,

   twice=. ^:2

   plusthree=. +&3
   
   g=. plusthree twice
   
   g 7
13

Java (1.8+)

[edit]

Using just functional interfaces:

import java.util.function.*;

class Main {
    public static void main(String[] args) {
        Function<IntUnaryOperator, IntUnaryOperator> twice = f -> f.andThen(f);

        IntUnaryOperator plusThree = i -> i + 3;

        var g = twice.apply(plusThree);

        System.out.println(g.applyAsInt(7)); // 13
    }
}

Or equivalently, with static methods:

import java.util.function.*;

class Main {
    private static IntUnaryOperator twice(IntUnaryOperator f) {
        return f.andThen(f);
    }

    private static int plusThree(int i) {
        return i + 3;
    }

    public static void main(String[] args) {
        var g = twice(Main::plusThree);

        System.out.println(g.applyAsInt(7)); // 13
    }
}

JavaScript

[edit]

With arrow functions:

"use strict";

const twice = f => x => f(f(x));

const plusThree = i => i + 3;

const g = twice(plusThree);

console.log(g(7)); // 13

Or with classical syntax:

"use strict";

function twice(f) {
  return function (x) {
    return f(f(x));
  };
}

function plusThree(i) {
  return i + 3;
}

const g = twice(plusThree);

console.log(g(7)); // 13

Julia

[edit]
julia> function twice(f)
           function result(x)
               return f(f(x))
           end
           return result
       end
twice (generic function with 1 method)

julia> plusthree(i) = i + 3
plusthree (generic function with 1 method)

julia> g = twice(plusthree)
(::var"#result#3"{typeof(plusthree)}) (generic function with 1 method)

julia> g(7)
13

Kotlin

[edit]
fun twice(f: (Int) -> Int): (Int) -> Int {
    return { f(f(it)) }
}

fun plusThree(i: Int) = i + 3

fun main() {
    val g = twice(::plusThree)

    println(g(7)) // 13
}

Lua

[edit]
function twice(f)
  return function (x)
    return f(f(x))
  end
end

function plusThree(i)
  return i + 3
end

local g = twice(plusThree)

print(g(7)) -- 13

MATLAB

[edit]
function result = twice(f)
result = @(x) f(f(x));
end

plusthree = @(i) i + 3;

g = twice(plusthree)

disp(g(7)); % 13

OCaml

[edit]
let twice f x =
  f (f x)

let plus_three =
  (+) 3

let () =
  let g = twice plus_three in

  print_int (g 7); (* 13 *)
  print_newline ()

PHP

[edit]
<?php

declare(strict_types=1);

function twice(callable $f): Closure {
    return function (int $x) use ($f): int {
        return $f($f($x));
    };
}

function plusThree(int $i): int {
    return $i + 3;
}

$g = twice('plusThree');

echo $g(7), "\n"; // 13

or with all functions in variables:

<?php

declare(strict_types=1);

$twice = fn(callable $f): Closure => fn(int $x): int => $f($f($x));

$plusThree = fn(int $i): int => $i + 3;

$g = $twice($plusThree);

echo $g(7), "\n"; // 13

Note that arrow functions implicitly capture any variables that come from the parent scope,[1] whereas anonymous functions require the use keyword to do the same.

Perl

[edit]
use strict;
use warnings;

sub twice {
    my ($f) = @_;
    sub {
        $f->($f->(@_));
    };
}

sub plusThree {
    my ($i) = @_;
    $i + 3;
}

my $g = twice(\&plusThree);

print $g->(7), "\n"; # 13

or with all functions in variables:

use strict;
use warnings;

my $twice = sub {
    my ($f) = @_;
    sub {
        $f->($f->(@_));
    };
};

my $plusThree = sub {
    my ($i) = @_;
    $i + 3;
};

my $g = $twice->($plusThree);

print $g->(7), "\n"; # 13

Python

[edit]
>>> def twice(f):
...     def result(x):
...         return f(f(x))
...     return result

>>> plus_three = lambda i: i + 3

>>> g = twice(plus_three)
    
>>> g(7)
13

Python decorator syntax is often used to replace a function with the result of passing that function through a higher-order function. E.g., the function g could be implemented equivalently:

>>> @twice
... def g(i):
...     return i + 3

>>> g(7)
13

R

[edit]
twice <- \(f) \(x) f(f(x))

plusThree <- function(i) i + 3

g <- twice(plusThree)

> g(7)
[1] 13

Raku

[edit]
sub twice(Callable:D $f) {
    return sub { $f($f($^x)) };
}

sub plusThree(Int:D $i) {
    return $i + 3;
}

my $g = twice(&plusThree);

say $g(7); # 13

In Raku, all code objects are closures and therefore can reference inner "lexical" variables from an outer scope because the lexical variable is "closed" inside of the function. Raku also supports "pointy block" syntax for lambda expressions which can be assigned to a variable or invoked anonymously.

Ruby

[edit]
def twice(f)
  ->(x) { f.call(f.call(x)) }
end

plus_three = ->(i) { i + 3 }

g = twice(plus_three)

puts g.call(7) # 13

Rust

[edit]
fn twice(f: impl Fn(i32) -> i32) -> impl Fn(i32) -> i32 {
    move |x| f(f(x))
}

fn plus_three(i: i32) -> i32 {
    i + 3
}

fn main() {
    let g = twice(plus_three);

    println!("{}", g(7)) // 13
}

Scala

[edit]
object Main {
  def twice(f: Int => Int): Int => Int =
    f compose f

  def plusThree(i: Int): Int =
    i + 3

  def main(args: Array[String]): Unit = {
    val g = twice(plusThree)

    print(g(7)) // 13
  }
}

Scheme

[edit]
(define (compose f g) 
  (lambda (x) (f (g x))))

(define (twice f) 
  (compose f f))

(define (plus-three i)
  (+ i 3))

(define g (twice plus-three))

(display (g 7)) ; 13
(display "\n")

Swift

[edit]
func twice(_ f: @escaping (Int) -> Int) -> (Int) -> Int {
    return { f(f($0)) }
}

let plusThree = { $0 + 3 }

let g = twice(plusThree)

print(g(7)) // 13

Tcl

[edit]
set twice {{f x} {apply $f [apply $f $x]}}
set plusThree {{i} {return [expr $i + 3]}}

# result: 13
puts [apply $twice $plusThree 7]

Tcl uses apply command to apply an anonymous function (since 8.6).

XACML

[edit]

The XACML standard defines higher-order functions in the standard to apply a function to multiple values of attribute bags.

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

The list of higher-order functions in XACML can be found here.

XQuery

[edit]
declare function local:twice($f, $x) {
  $f($f($x))
};

declare function local:plusthree($i) {
  $i + 3
};

local:twice(local:plusthree#1, 7) (: 13 :)

Alternatives

[edit]

Function pointers

[edit]

Function pointers in languages such as C, C++, Fortran, and Pascal allow programmers to pass around references to functions. The following C code computes an approximation of the integral of an arbitrary function:

#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;
}

The qsort function from the C standard library uses a function pointer to emulate the behavior of a higher-order function.

Macros

[edit]

Macros can also be used to achieve some of the effects of higher-order functions. However, macros cannot easily avoid the problem of variable capture; they may also result in large amounts of duplicated code, which can be more difficult for a compiler to optimize. Macros are generally not strongly typed, although they may produce strongly typed code.

Dynamic code evaluation

[edit]

In other imperative programming languages, it is possible to achieve some of the same algorithmic results as are obtained via higher-order functions by dynamically executing code (sometimes called Eval or Execute operations) in the scope of evaluation. There can be significant drawbacks to this approach:

  • The argument code to be executed is usually not statically typed; these languages generally rely on dynamic typing to determine the well-formedness and safety of the code to be executed.
  • The argument is usually provided as a string, the value of which may not be known until run-time. This string must either be compiled during program execution (using just-in-time compilation) or evaluated by interpretation, causing some added overhead at run-time, and usually generating less efficient code.

Objects

[edit]

In object-oriented programming languages that do not support higher-order functions, objects can be an effective substitute. An object's methods act in essence like functions, and a method may accept objects as parameters and produce objects as return values. Objects often carry added run-time overhead compared to pure functions, however, and added boilerplate code for defining and instantiating an object and its method(s). Languages that permit stack-based (versus heap-based) objects or structs can provide more flexibility with this method.

An example of using a simple stack based record in Free Pascal with a function that returns a function:

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 10
end.

The function a() takes a Txy record as input and returns the integer value of the sum of the record's x and y fields (3 + 7).

Defunctionalization

[edit]

Defunctionalization can be used to implement higher-order functions in languages that lack first-class functions:

// Defunctionalized function data structures
template<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 implementations
template<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;
}

// Higher-order compose function
template<typename F, typename G>
Composition<F, G> compose(F f, G g) {
    return Composition<F, G> {f, g};
}

int main(int argc, const char* argv[]) {
    auto f = compose(DivBy<float>{ 2.0f }, Add<int>{ 5 });
    apply(f, 3); // 4.0f
    apply(f, 9); // 7.0f
    return 0;
}

In this case, different types are used to trigger different functions via function overloading. The overloaded function in this example has the signature auto apply.

See also

[edit]

References

[edit]
  1. ^ "PHP: Arrow Functions - Manual". www.php.net. Retrieved 2025-08-06.
ade是什么意思 合作医疗是什么 洋葱可以炒什么 甲状腺是什么意思 什么人不能爬泰山
88年属什么生肖 一案双查是什么意思 耐受性是什么意思 脚面疼是什么原因引起的 马上风为什么拔不出来
本田的高端品牌是什么 拔了牙吃什么消炎药 门客是什么意思 荞麦和苦荞有什么区别 为什么喝牛奶会长痘
原位癌是什么意思 龙跟什么生肖配对最好 tr什么意思 高危儿是什么意思 缺钾最忌讳吃什么
五险一金包括什么hcv7jop5ns2r.cn 麦芒是什么意思hcv9jop2ns3r.cn 大腿两侧疼痛什么原因hcv8jop5ns6r.cn 眼疲劳用什么眼药水hcv8jop8ns6r.cn 什么样的人容易得心梗hcv7jop9ns2r.cn
检测怀孕最准确的方法是什么hcv8jop6ns2r.cn 色令智昏是什么意思hcv9jop5ns6r.cn 一个巾一个童读什么hcv8jop4ns0r.cn 天山童姥练的什么武功hcv8jop2ns1r.cn 梦见刀是什么意思hcv9jop1ns4r.cn
水逆退散是什么意思cl108k.com 梦见游泳是什么预兆hcv8jop8ns8r.cn 法令纹是什么hcv9jop3ns1r.cn 大姨妈推迟什么原因helloaicloud.com 脸上长白斑是什么原因hcv8jop2ns2r.cn
为什么会上火hcv9jop2ns5r.cn 预防更年期提前应该吃点什么药wuhaiwuya.com 痔疮是什么症状hcv7jop5ns0r.cn 耳朵嗡嗡的响是什么原因hcv8jop9ns8r.cn 膀胱炎是什么症状表现hcv9jop5ns3r.cn
百度