血型b型rh阳性是什么意思| 甲磺酸倍他司汀片治什么病| 人流后可以吃什么水果| 爽是什么结构| 肝不好吃什么水果| 看肺子要挂什么科| 消防队属于什么编制| 成五行属性是什么| 可乐加味精女人喝了什么效果| 孙权为什么不北伐| 百事可乐和可口可乐有什么区别| 黄果树是什么树| 哺乳期妈妈感冒了可以吃什么药| 腋下有异味是什么原因| 糖尿病人不能吃什么水果| 三月十七是什么星座| 考护士证需要什么条件| 冬至下雨有什么说法| 血压低是什么原因造成的| 生蒜头吃了有什么好处和坏处| 芒果和什么相克| 虫草吃了有什么好处| 吃什么东西补血| 敬请是什么意思| 九月二十二是什么星座| 党参长什么样| 嗓子疼吃什么消炎药| 夏天可以种什么蔬菜| 红曲米是什么米| 为什么有的女人欲太强| 感冒怕冷吃什么药| 泄气是什么意思| 吃维生素b6有什么好处和副作用| 气血亏虚吃什么中成药| 送长辈什么礼物合适| 心脏骤停是什么原因引起的| 胰腺分泌什么| 什么是童子| 撮鸟是什么意思| 男性更年期吃什么药| 土命适合什么颜色| 踢馆什么意思| 来月经喝什么茶好| 挥霍是什么意思| sga是什么意思| 子宫肌瘤是什么原因造成的| 老人越来越瘦是什么原因| 为什么爱放屁| 87岁属什么生肖| 蛋白尿吃什么药| 小米长什么样| 测测你天生靠什么吃饭| 水火既济是什么意思| 女人梦见蛇缠身是什么预兆| 氢键是什么| 孕酮低有什么影响| 教学相长是什么意思| 甲状旁腺是什么意思| 石女是什么样子的| 去医院查怀孕挂什么科| 柠檬酸钠是什么| 一路长虹什么意思| 脑彩超能检查出什么| 75c是什么罩杯| 舌头溃疡吃什么水果| 美国为什么打朝鲜| 女金片的功效与作用是什么| 什么的双手| 姓袁女孩叫什么名字好听| 拔完智齿第三天可以吃什么| 脑供血不足做什么检查能查出来| 入坑是什么意思| 粉红的什么| 霉菌感染用什么药最好| 抑郁症是什么症状| 什么时候有胎心胎芽| 喉咙有异物感挂什么科| 为什么会蛀牙| 恍惚什么意思| 大专是什么意思| 一只脚心疼是什么原因| hazzys是什么牌子价格| c13阴性是什么意思| 四面受敌是什么动物| 阴险是什么意思| 心脏跳得快是什么原因| 蜜蜡属于什么五行属性| 化疗什么意思| 免疫组织化学染色诊断是什么| 八月二十八是什么星座| pettm和pe有什么区别| 肺部玻璃结节是什么病| 甲亢是什么症状| 子宫囊肿是什么原因引起的| 对策是什么意思| 不孕不育有什么症状女| 孕酮低吃什么好提高| tf口红什么牌子| 黄柏的功效与作用是什么| blingbling什么意思| 年薪12万什么水平| 拔罐有什么好处| 抑郁看病看什么科| 浆果是什么| 脑萎缩是什么症状| 糖尿病人能喝什么饮料| 眼睛晶体是什么| 酸菜鱼里面加什么配菜好吃| pbr是什么意思| 女人绝经一般在什么年龄段| 痛什么什么痛| 老枞水仙属于什么茶| 戒指戴左手中指是什么意思| 咽喉炎是什么原因引起的| 疮痈是什么意思| cps是什么意思啊| 肉桂属于什么茶类| 红薯什么时候成熟| 什么地指挥| 双肺纤维条索是什么意思| 六月初六是什么日子| 漏尿女性吃什么药最好| 牙肿了吃什么消炎药| 早年晚岁总无长是什么意思| KTV服务员主要做什么| pcm是什么意思| 修成正果是什么意思| 难以启齿什么意思| 血糖什么时候最高| emba是什么| pg是什么意思| yuri是什么意思| 总胆红素高是什么意思| 阴灵是什么意思| hcv是什么病毒| 鸟衣念什么| 一毛三是什么军衔| 为什么贫血| 为什么会有耳石症| 什么是脑卒中| 山海经是什么| 尿检阳性是什么意思| 水痘可以吃什么水果| 水滴石穿是什么变化| tct是检查什么的| 20岁属什么| 死了妻子的男人叫什么| 兽中之王是什么动物| 三点水加一个心读什么| 多囊是什么原因造成的| 3月6号是什么星座| 舌头起泡吃什么药| 环球中心有什么好玩的| 屈光和近视有什么区别| 什么是先兆流产| 女人做什么好| 橘子什么时候成熟| 男人眉心有痣代表什么| 真菌镜检阴性是什么意思| 专科考研需要什么条件| 新生儿黄疸是什么原因引起的| 梦见梯子是什么意思| 月经不规律吃什么药调理| 尿蛋白可疑阳性是什么意思| 勾芡用什么淀粉| senda是什么牌子| 什么时候洗头最好| 含羞草能治什么病| 利有攸往是什么意思| 尖锐湿疣是什么样的| 地贫吃什么补血最快| fl是胎儿的什么意思| 老巫婆是什么意思| 脸上黑色的小点是什么| lucas是什么意思| 县公安局长什么级别| 心脏缺血吃什么药好| 麦芽是什么| 脑多普勒检查什么| 夏天吃什么| 湿热带下是什么意思| 风言风语是什么意思| 什么是性瘾| 什么是abo| 我们是什么意思| 黄水晶五行属什么| 胎儿生物物理评分8分什么意思| 血小板分布宽度是什么意思| 惊弓之鸟告诉我们什么道理| 36朵玫瑰花代表什么意思| 毒奶粉是什么游戏| 今天过生日是什么星座| 堂号是什么意思| 护士资格证什么时候考| 乔治白属于什么档次| 夜盲症是什么| 左腰疼是什么原因| 40岁男人性功能减退是什么原因| 上帝叫什么名字| 梦到鹦鹉预示着什么| 痛风有什么不能吃| 鸭肉和什么不能一起吃| 出汗太多吃什么药好| 肌肉拉伤是什么感觉| 10岁属什么| 尿酸高吃什么可以降下去| 仰仗是什么意思| 早上起来后背疼是什么原因| 尿hcg阴性是什么意思| 喉咙长溃疡是什么原因| 乙肝15阳性是什么意思| 褒义词和贬义词是什么意思| 身份证带x是什么意思| 情趣什么意思| 为什么喝水血糖也会高| 大腿外侧疼痛是什么原因| 10月是什么星座| 松果体是什么| 兔跟什么生肖配对最好| 骑马挥杆是什么牌子| 必迈跑鞋什么档次| 吃什么能增强免疫力和抵抗力| romantic是什么意思| 来姨妈不能吃什么| 户口本丢了有什么危害| 紧急避孕药吃了有什么副作用| 负利率是什么意思| 睾丸为什么会痛| 气血不足吃什么中成药| 贺喜是什么意思| 吃什么可以健脾养胃| 为什么屁股上会长痘| 淋巴吃什么药可以消除| 浪琴手表什么档次| 脚气用什么洗脚| 银行卡开户名是什么| 牛大力有什么功效| 为什么缺钾| 什么东西可以去口臭呀| 脑供血不足什么症状| 见到黑猫代表什么预兆| 泌乳素高是什么意思| 宝宝出急疹要注意什么| 0点是什么时辰| 什么如镜| 子婴是秦始皇什么人| 房颤是什么原因引起的| 正规医院减肥挂什么科| 磷高吃什么药| 大头鱼吃什么食物| offer是什么意思| 啼笑皆非的意思是什么| 土克什么| 做饼用什么面粉| 足跟痛挂什么科| 米酒不甜是什么原因| 中将是什么级别的干部| 喝红茶有什么好处和坏处| 纷扰是什么意思| 心率过缓有什么危害| 每天熬夜有什么危害| 喝茶失眠是什么原因| 最高法院院长什么级别| 感冒吃什么食物好得快| 百度Jump to content

河北新闻网:2016年廊坊围棋甲级联赛拉开战幕(图)

From Wikipedia, the free encyclopedia
(Redirected from Anonymous functions)
百度 显然,生活方式是最主要的因素。

In computer programming, an anonymous function (function literal, expression or block) is a function definition that is not bound to an identifier. Anonymous functions are often arguments being passed to higher-order functions or used for constructing the result of a higher-order function that needs to return a function.[1] If the function is only used once, or a limited number of times, an anonymous function may be syntactically lighter than using a named function. Anonymous functions are ubiquitous in functional programming languages and other languages with first-class functions, where they fulfil the same role for the function type as literals do for other data types.

Anonymous functions originate in the work of Alonzo Church in his invention of the lambda calculus, in which all functions are anonymous, in 1936, before electronic computers.[2] In several programming languages, anonymous functions are introduced using the keyword lambda, and anonymous functions are often referred to as lambdas or lambda abstractions. Anonymous functions have been a feature of programming languages since Lisp in 1958, and a growing number of modern programming languages support anonymous functions.

Names

[edit]

The names "lambda abstraction", "lambda function", and "lambda expression" refer to the notation of function abstraction in lambda calculus, where the usual function f(x) = M would be written x.M), and where M is an expression that uses x. Compare to the Python syntax of lambda x: M.

The name "arrow function" refers to the mathematical "maps to" symbol, x ? M. Compare to the JavaScript syntax of x => M.[3]

Uses

[edit]

Anonymous functions can encapsulate functionality that does not require naming and is intended for short-term or localized use. Some notable examples include closures and currying.

The use of anonymous functions is a matter of style. Using them is never the only way to solve a problem; each anonymous function could instead be defined as a named function and called by name. Anonymous functions often provide a briefer notation than defining named functions. In languages that do not permit the definition of named functions in local scopes, anonymous functions may provide encapsulation via localized scope, however the code in the body of such anonymous function may not be re-usable, or amenable to separate testing. Short/simple anonymous functions used in expressions may be easier to read and understand than separately defined named functions, though lacking a descriptive name may reduce code readability.

In some programming languages, anonymous functions are commonly implemented for very specific purposes such as binding events to callbacks or instantiating the function for particular values, which may be more efficient in a Dynamic programming language, more readable, and less error-prone than calling a named function.

The following examples are written in Python 3.

Sorting

[edit]

When attempting to sort in a non-standard way, it may be easier to contain the sorting logic as an anonymous function instead of creating a named function. Most languages provide a generic sort function that implements a sort algorithm that will sort arbitrary objects. This function usually accepts an arbitrary function that determines how to compare whether two elements are equal or if one is greater or less than the other.

Consider this Python code sorting a list of strings by length of the string:

>>> a = ['house', 'car', 'bike']
>>> a.sort(key=lambda x: len(x))
>>> a
['car', 'bike', 'house']

The anonymous function in this example is the lambda expression:

lambda x: len(x)

The anonymous function accepts one argument, x, and returns the length of its argument, which is then used by the sort() method as the criteria for sorting.

Basic syntax of a lambda function in Python is

lambda arg1, arg2, arg3, ...: <operation on the arguments returning a value>

The expression returned by the lambda function can be assigned to a variable and used in the code at multiple places.

>>> add = lambda a: a + a
>>> add(20)
40

Another example would be sorting items in a list by the name of their class (in Python, everything has a class):

>>> a = [10, 'number', 11.2]
>>> a.sort(key=lambda x: x.__class__.__name__)
>>> a
[11.2, 10, 'number']

Note that 11.2 has class name "float", 10 has class name "int", and 'number' has class name "str". The sorted order is "float", "int", then "str".

Closures

[edit]

Closures are functions evaluated in an environment containing bound variables. The following example binds the variable "threshold" within an anonymous function that compares input values to this threshold.

def comp(threshold):
    return lambda x: x < threshold

This can be used as a sort of generator of comparison functions:

>>> func_a = comp(10)
>>> func_b = comp(20)

>>> print(func_a(5), func_a(8), func_a(13), func_a(21))
True True False False

>>> print(func_b(5), func_b(8), func_b(13), func_b(21))
True True True False

It would be impractical to create a function for every possible comparison function and may be too inconvenient to keep the threshold around for further use. Regardless of the reason why a closure is used, the anonymous function is the entity that contains the functionality that does the comparing.

Currying

[edit]

Currying transforms a function that takes multiple arguments into a sequence of functions each accepting a single argument. In this example, a function that performs division by any integer is transformed into one that performs division by a set integer.

>>> def divide(x, y):
...     return x / y

>>> def divisor(d):
...     return lambda x: divide(x, d)

>>> half = divisor(2)
>>> third = divisor(3)

>>> print(half(32), third(32))
16.0 10.666666666666666

>>> print(half(40), third(40))
20.0 13.333333333333334

While the use of anonymous functions is perhaps not common with currying, it still can be used. In the above example, the function divisor generates functions with a specified divisor. The functions half and third curry the divide function with a fixed divisor.

The divisor function also forms a closure by binding the variable d.

Higher-order functions

[edit]

A higher-order function is a function that takes a function as an argument or returns one as a result. This technique is frequently employed to tailor the behavior of a generically defined function, such as a loop or recursion pattern. Anonymous functions are a convenient way to specify such function arguments. The following examples are in Python 3.

Map

[edit]

The map function performs a function call on each element of a list. The following example squares every element in an array with an anonymous function.

>>> a = [1, 2, 3, 4, 5, 6]
>>> list(map(lambda x: x * x, a))
[1, 4, 9, 16, 25, 36]

The anonymous function takes an argument and returns its square. The above form is discouraged by the creators of the language, who maintain that the form presented below has the same meaning and is more aligned with the philosophy of the language:

>>> a = [1, 2, 3, 4, 5, 6]
>>> [x * x for x in a]
[1, 4, 9, 16, 25, 36]

Filter

[edit]

The filter function returns all elements from a list that evaluate True when passed to a certain function.

>>> a = [1, 2, 3, 4, 5, 6]
>>> list(filter(lambda x: x % 2 == 0, a))
[2, 4, 6]

The anonymous function checks if the argument passed to it is even. The same as with map, the form below is considered more appropriate:

>>> a = [1, 2, 3, 4, 5, 6]
>>> [x for x in a if x % 2 == 0]
[2, 4, 6]

Fold

[edit]

A fold function runs over all elements in a structure (for lists usually left-to-right, a "left fold", called reduce in Python), accumulating a value as it goes. This can be used to combine all elements of a structure into one value, for example:

>>> from functools import reduce
>>> a = [1, 2, 3, 4, 5]
>>> reduce(lambda x, y: x * y, a)
120

This performs

The anonymous function here is the multiplication of the two arguments.

A fold does not necessarily produce a single scalar value; it can also generate structured results such as lists. Instead, both map and filter can be created using fold. In map, the value that is accumulated is a new list, containing the results of applying a function to each element of the original list. In filter, the value that is accumulated is a new list containing only those elements that match the given condition.

List of languages

[edit]

The following is a list of programming languages that support unnamed anonymous functions fully, or partly as some variant, or not at all.

The following table illustrates several common patterns. Notably, languages like C, Pascal, and Object Pascal—which traditionally do not support anonymous functions—are all statically typed languages. However, statically typed languages can support anonymous functions. For example, the ML languages are statically typed and fundamentally include anonymous functions, and Delphi, a dialect of Object Pascal, has been extended to support anonymous functions, as has C++ (by the C++11 standard). Second, the languages that treat functions as first-class functions (Dylan, Haskell, JavaScript, Lisp, ML, Perl, Python, Ruby, Scheme) generally have anonymous function support so that functions can be defined and passed around as easily as other data types.

Examples of anonymous functions

[edit]

See also

[edit]

References

[edit]
  1. ^ "Higher order functions". learnyouahaskell.com. Retrieved 3 December 2014.
  2. ^ Fernandez, Maribel (2009), Models of Computation: An Introduction to Computability Theory, Undergraduate Topics in Computer Science, Springer Science & Business Media, p. 33, ISBN 9781848824348, The Lambda calculus ... was introduced by Alonzo Church in the 1930s as a precise notation for a theory of anonymous functions
  3. ^ "Arrow function expressions - JavaScript". MDN. Retrieved August 21, 2019.
  4. ^ "Access Types". www.adaic.org. Retrieved 2025-08-05.
  5. ^ "Bash lambda". GitHub. 2025-08-05.
  6. ^ BillWagner. "Lambda expressions - C# reference". docs.microsoft.com. Retrieved 2025-08-05.
  7. ^ "Closure support". Archived from the original on 2025-08-05. Retrieved 2025-08-05.
  8. ^ "Whats new in ColdFusion 10". Archived from the original on 2025-08-05. Retrieved 2025-08-05.
  9. ^ "Clojure - Higher Order Functions". clojure.org. Retrieved 2025-08-05.
  10. ^ "Managed COBOL Reference". Micro Focus Documentation. Micro Focus. Archived from the original on 25 February 2014. Retrieved 25 February 2014.
  11. ^ "Functions - D Programming Language". dlang.org. Retrieved 2025-08-05.
  12. ^ "A tour of the Dart language". dart.dev. Retrieved 2025-08-05.
  13. ^ "Anonymous Methods in Delphi - RAD Studio". docwiki.embarcadero.com. Retrieved 2025-08-05.
  14. ^ "Functions — Dylan Programming". opendylan.org. Retrieved 2025-08-05.
  15. ^ "docs/syntax". elm-lang.org. Retrieved 2025-08-05.
  16. ^ "Erlang/Elixir Syntax: A Crash Course". elixir-lang.github.com. Retrieved 2025-08-05.
  17. ^ "Erlang -- Funs". erlang.org. Retrieved 2025-08-05.
  18. ^ cartermp. "Lambda Expressions: The fun Keyword - F#". docs.microsoft.com. Retrieved 2025-08-05.
  19. ^ "LAMBDA: The ultimate Excel worksheet function". microsoft.com. 25 January 2021. Retrieved 2025-08-05.
  20. ^ "Quotations - Factor Documentation". Retrieved 26 December 2015. A quotation is an anonymous function (a value denoting a snippet of code) which can be used as a value and called using the Fundamental combinators.
  21. ^ "Frink". frinklang.org. Retrieved 2025-08-05.
  22. ^ "Anonymous Functions in GoLang". GoLang Docs. 9 January 2020. Retrieved 2025-08-05.
  23. ^ "Gosu Documentation" (PDF). Retrieved 4 March 2013.
  24. ^ "Groovy Documentation". Archived from the original on 22 May 2012. Retrieved 29 May 2012.
  25. ^ "Anonymous function - HaskellWiki". wiki.haskell.org. Retrieved 2025-08-05.
  26. ^ "Lambda". Haxe - The Cross-platform Toolkit. Retrieved 2025-08-05.
  27. ^ "Functions - JavaScript | MDN". developer.mozilla.org. Retrieved 2025-08-05.
  28. ^ "Functions · The Julia Language". docs.julialang.org. Retrieved 2025-08-05.
  29. ^ "Higher-Order Functions and Lambdas - Kotlin Programming Language". Kotlin. Retrieved 2025-08-05.
  30. ^ "Programming in Lua : 6". www.lua.org. Retrieved 2025-08-05.
  31. ^ "Maple Programming: 1.6: Anonymous functions and expressions - Application Center". www.maplesoft.com. Retrieved 2025-08-05.
  32. ^ "Anonymous Functions - MATLAB & Simulink". www.mathworks.com. Retrieved 2025-08-05.
  33. ^ "Maxima 5.17.1 Manual: 39. Function Definition". maths.cnam.fr. Retrieved 2025-08-05.
  34. ^ "Nim Manual". nim-lang.github.io.
  35. ^ "Code Examples – OCaml". ocaml.org. Retrieved 2025-08-05.
  36. ^ "GNU Octave: Anonymous Functions". octave.org. Retrieved 2025-08-05.
  37. ^ "Function Literals". OpenSCAD User Manual. Wikibooks. Retrieved 22 February 2021.
  38. ^ "perlsub - Perl subroutines - Perldoc Browser". perldoc.perl.org. Retrieved 2025-08-05.
  39. ^ "PHP: Anonymous functions - Manual". www.php.net. Retrieved 2025-08-05.
  40. ^ "6. Expressions — Python 3.9.0 documentation". docs.python.org. Retrieved 2025-08-05.
  41. ^ "4.4 Functions: lambda". docs.racket-lang.org. Retrieved 2025-08-05.
  42. ^ "Functions". docs.raku.org. Retrieved 2025-08-05.
  43. ^ Sosinski, Robert (2025-08-05). "Understanding Ruby Blocks, Procs and Lambdas". Reactive.IO. Archived from the original on 2025-08-05. Retrieved 2025-08-05.
  44. ^ "Closures: Anonymous Functions that Can Capture Their Environment - The Rust Programming Language". doc.rust-lang.org. Retrieved 2025-08-05.
  45. ^ "Anonymous Functions". Scala Documentation. Retrieved 2025-08-05.
  46. ^ "Recitation 3: Higher order functions". www.cs.cornell.edu. Retrieved 2025-08-05.
  47. ^ "Closures — The Swift Programming Language (Swift 5.5)". docs.swift.org.
  48. ^ "Documentation - Everyday Types". www.typescriptlang.org. Retrieved 2025-08-05.
  49. ^ "Function Type - Typst Documentation". typst.app. Retrieved 2025-08-05.
  50. ^ a b "Projects/Vala/Tutorial - GNOME Wiki!". wiki.gnome.org. Retrieved 2025-08-05.
  51. ^ KathleenDollard (15 September 2021). "Lambda Expressions - Visual Basic". docs.microsoft.com. Retrieved 2025-08-05.
  52. ^ "Language Reference/Terms/Anonymous Predicates - wiki.visual-prolog.com". wiki.visual-prolog.com. Retrieved 2025-08-05.
  53. ^ "Pure Anonymous Function: Elementary Introduction to the Wolfram Language". www.wolfram.com. Retrieved 2025-08-05.
  54. ^ "Lambdas, Closures and everything in between · Issue #1048 · ziglang/zig". GitHub. Retrieved 2025-08-05.
[edit]
父母是o型血孩子是什么血型 狗奴是什么意思 凝血四项是查什么的 ch什么意思 孕妇能喝什么茶
睡几个小时就醒了是什么原因 怀孕是什么症状 省政协主席什么级别 银为什么会变黑 做梦梦到对象出轨是什么意思
头油是什么原因引起的 佛法无边是什么生肖 five是什么意思 7月24日是什么日子 血症是什么病
什么的蜡烛 乡和镇的区别是什么 大年初一是什么生肖 等回声结节是什么意思 冰点脱毛的原理是什么
player是什么意思hcv8jop6ns9r.cn 什么身是胆hcv8jop7ns7r.cn 人中长痘痘什么原因hcv9jop3ns5r.cn 重力是什么wuhaiwuya.com 属什么生肖bfb118.com
姹紫嫣红是什么意思hcv8jop6ns6r.cn 12月生日是什么星座hcv8jop1ns2r.cn 0是什么数hcv8jop1ns9r.cn 端午节吃什么菜呢hcv9jop0ns6r.cn 升米恩斗米仇是什么意思hcv7jop5ns5r.cn
月经推迟量少是什么原因hcv8jop8ns5r.cn 梦到分手了是什么征兆hcv8jop2ns5r.cn 执着什么意思hcv8jop1ns3r.cn 脉弦是什么意思clwhiglsz.com 什么通便效果最快最好chuanglingweilai.com
为什么偏偏喜欢你1949doufunao.com 老鼠跟什么属相最配hcv9jop4ns2r.cn 天秤座什么象hcv9jop2ns2r.cn 核磁dwi是什么意思hcv8jop2ns0r.cn 梦见弟媳妇是什么预兆hcv9jop7ns5r.cn
百度