阴道口痒是什么原因| 高血糖吃什么| 舌头两边有齿痕是什么原因| 单元剧是什么意思| 与其让你在我怀中枯萎是什么歌| 飞机杯是什么东西| 失眠吃什么食物最有效| 荣辱与共是什么意思| 肚脐眼发炎是什么原因| 什么叫野鸡大学| 宫外孕破裂有什么症状| 智商什么意思| 皮肤擦伤用什么药最好| mtt什么意思| 梦见和婆婆吵架是什么意思| 低置胎盘有什么危险| 中指戴戒指代表什么| 经常的近义词是什么| 老公梦见蛇是什么预兆| 干涸是什么意思| 梭是什么意思| essence是什么意思| 海绵体修复吃什么药| 大腿前侧是什么经络| 养神经的药是什么药最好| ojbk 是什么意思| 缅怀是什么意思| 小麦淀粉可以做什么| 区团委书记是什么级别| 灵芝煮水喝有什么功效| hello中文是什么意思| 虎头蜂泡酒有什么功效| 曹仁和曹操什么关系| 老烂腿用什么药最好| 坎坷是什么意思| 纳甲是什么意思| 釜底抽薪是什么意思| 鲤鱼吃什么| 胃窦肠化是什么意思| 机遇什么意思| 肺部积水是什么原因引起的| 破太岁是什么意思| 什么是贫血| 春天都有什么花开| 脱水是什么意思| 难耐是什么意思| 为什么老是头晕| 姑爷是什么意思| 查脂肪肝挂什么科室| 四查十对的内容是什么| 槐花蜜是什么颜色| 吃什么可以让阴茎变硬| 羡慕是什么意思| 螺旋藻是什么东西| 补血吃什么食物最好| 九头身什么意思| head是什么牌子| 文化底蕴是什么意思| 耳洞发炎流脓用什么药| 脾的作用和功能是什么| 最近有什么病毒| 风邪是什么| 什么的天安门| 吸顶灯什么牌子的好| 寄生茶在什么树上最好| 走路腰疼是什么原因| 小拇指长痣代表什么| 人乳头瘤病毒是什么| 5月2日是什么星座| 胚根发育成什么| 男人左眼跳是什么预兆| 鑫字属于五行属什么| 过敏了吃什么药好| 玄米是什么米| tel是什么意思啊| 转归是什么意思| 大肠杆菌吃什么药| 饕餮长什么样| 儿童过敏性鼻炎吃什么药| 2000年为什么叫千禧年| 举措前面搭配什么| 中午十一点是什么时辰| 什么是物理防晒| 乌龙茶是什么茶| 凉栀是什么意思| 孩子积食吃什么药| 心肌缺血做什么检查能查出来| 陌路人是什么意思| 眼睛干涩模糊用什么眼药水| 梦见下雨是什么预兆| 东面墙适合挂什么画| 拉稀水是什么原因| 高血脂是什么意思| 肠癌便血和痔疮便血有什么区别| 肽是什么东西| 炒米是什么米做的| 土豆是什么科| 一进门见到什么植物好| 蜜蜂糖冲开水有什么好处| 五月天主唱叫什么名字| 奔是什么生肖| 忽悠什么意思| hpv是什么病毒| 处女座男生喜欢什么样的女生| 为什么每天晚上睡觉都做梦| 鼻烟是什么| 国防科技大学毕业是什么军衔| 桑寄生有什么功效| 太监是什么生肖| 低血糖有什么症状表现| 拔智齿第二天可以吃什么| 冒泡是什么意思| 软组织感染是什么意思| 什么是小奶狗| 阴蒂长什么样| 如何看五行缺什么| 多巴胺是什么| 助产专业是干什么的| 淋巴结肿大看什么科室最好| 小孩子流鼻血是什么原因| 雷什么风什么成语| 许嵩的粉丝叫什么| 杏花什么颜色| 潜血是什么意思| ygk是什么牌子| 鱼白是什么东西| 心率用什么字母表示| 湿疹有什么症状和图| 忻字五行属什么| rash什么意思| 用什么挠脚心最痒| 头发干燥是什么原因| 心脏搭桥和支架有什么区别| 心脏造影是什么| 丝丝入扣是什么意思| eagle是什么牌子| 甲沟炎医院挂什么科| 不可翻转干燥是什么意思| 手麻抽筋是什么原因引起的| 美人鱼是什么动物| 狗皮肤溃烂用什么药| 真数是什么| 入肉是什么字| bni是什么意思| 多五行属什么| 2月17日是什么星座| 苯丙氨酸是什么| cr是什么检查| 妇科炎症用什么药最好| 小腿肚抽筋是什么原因| 鸿运当头是什么菜| 梅雨季节是什么意思| 小孩子黑眼圈重是什么原因| 十月二十二什么星座| 什么的滋味| 臭鼬是什么动物| 什么叫贵妃镯| 1981属什么| 黑户是什么| 肚子疼想吐是什么原因| 乙肝表面抗体弱阳性什么意思| 憋尿有什么危害| 狗消化不良吃什么药| 吉人自有天相什么意思| 头痒是什么原因| 孕妇什么时候做nt| 脚肿吃什么药消肿| 胃有灼热感是什么原因| 资治通鉴讲的是什么| 众望所归是什么意思| 什么地飞| 没脑子是什么意思| 不怕流氓什么就怕流氓有文化| 眼睛淤青用什么方法能快点消除| 做梦梦到鸡是什么意思| 分娩是什么意思| 黑便是什么原因| 牙周炎吃什么药最好| 金生水是什么意思| 什么叫根管治疗牙齿| 吃什么可以散结节| 草莓什么时候种植最好| 925银什么意思| 8岁属什么| 动态心电图能检查出什么病| 大姨妈期间适合吃什么| 芡实适合什么人吃| bic是什么意思| 8月28日什么星座| 冬瓜有什么功效和作用| sin是什么边比什么边| 三个降号是什么调| 胃炎可以吃什么| 有没有什么| 石灰的主要成分是什么| 贴士是什么意思| 曲安奈德针治疗什么| 尿血是什么病的征兆| 孕妇梦见猪是什么意思| 敲锣打鼓是什么生肖| 煮玉米为什么要放盐| 绕梁三日是什么意思| 毛囊炎用什么药膏好| 芒果不能和什么食物一起吃| 伊朗是什么派| 经常吃豆腐有什么好处和坏处| 喝什么解辣| 身份证最后四位数代表什么| 白塞氏吃什么药| 圣是什么生肖| 医保定点是什么意思| 修复胃粘膜吃什么药| 间歇性跛行是什么意思| york是什么牌子| 头部ct挂什么科| 官杀旺是什么意思| 气化是什么意思| 海棠什么时候开花| 早上八点多是什么时辰| 11月9号是什么日子| ph什么意思| 持之以恒的恒是什么意思| 血糖高喝什么豆浆好| jackjones是什么品牌| 女性血热吃什么好得快| 面红耳赤是什么生肖| 拜阿司匹林什么时间吃最好| 浪琴名匠系列什么档次| 按摩脚底有什么好处| 狐臭用什么药| 国家栋梁指的是什么官| 随餐服用是什么意思| 为什么会有湿气| prg是什么意思| 运气是什么意思| 癸水的根是什么| 前脚底板痛是什么原因| 猫怕什么动物| 服装属于五行什么行业| 八仙过海是什么生肖| 胸口疼应该挂什么科| 升是什么意思| 霉菌性阴炎用什么药止痒效果好| mandy英文名什么意思| 什么是小针刀治疗| 脂肪肝吃什么药治疗| 郭敬明为什么叫小四| 参加白事回来注意什么| 车辆购置税什么时候交| 掮客是什么意思| 一条条什么| 烫伤涂什么| 小孩尖叫是什么原因| 感恩节为什么要吃火鸡| 离岸是什么意思| 经常放臭屁是什么原因| 为什么老是肚子疼| 中医治未病是什么意思| 十二月二十号是什么星座| 食道不舒服挂什么科| 行房时硬度不够是什么原因| 欲壑难填是什么意思| 脑血栓适合吃什么水果| 做空是什么意思啊| 百度Jump to content

江西建立全省统一的电子化母子健康手册

From Wikipedia, the free encyclopedia
(Redirected from Lambda (programming))
百度 据称,该潜水器的速度可能数倍于包括美国濒海战斗舰在内的世界现役水面舰艇。

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-06.
  5. ^ "Bash lambda". GitHub. 2025-08-06.
  6. ^ BillWagner. "Lambda expressions - C# reference". docs.microsoft.com. Retrieved 2025-08-06.
  7. ^ "Closure support". Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  8. ^ "Whats new in ColdFusion 10". Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  9. ^ "Clojure - Higher Order Functions". clojure.org. Retrieved 2025-08-06.
  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-06.
  12. ^ "A tour of the Dart language". dart.dev. Retrieved 2025-08-06.
  13. ^ "Anonymous Methods in Delphi - RAD Studio". docwiki.embarcadero.com. Retrieved 2025-08-06.
  14. ^ "Functions — Dylan Programming". opendylan.org. Retrieved 2025-08-06.
  15. ^ "docs/syntax". elm-lang.org. Retrieved 2025-08-06.
  16. ^ "Erlang/Elixir Syntax: A Crash Course". elixir-lang.github.com. Retrieved 2025-08-06.
  17. ^ "Erlang -- Funs". erlang.org. Retrieved 2025-08-06.
  18. ^ cartermp. "Lambda Expressions: The fun Keyword - F#". docs.microsoft.com. Retrieved 2025-08-06.
  19. ^ "LAMBDA: The ultimate Excel worksheet function". microsoft.com. 25 January 2021. Retrieved 2025-08-06.
  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-06.
  22. ^ "Anonymous Functions in GoLang". GoLang Docs. 9 January 2020. Retrieved 2025-08-06.
  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-06.
  26. ^ "Lambda". Haxe - The Cross-platform Toolkit. Retrieved 2025-08-06.
  27. ^ "Functions - JavaScript | MDN". developer.mozilla.org. Retrieved 2025-08-06.
  28. ^ "Functions · The Julia Language". docs.julialang.org. Retrieved 2025-08-06.
  29. ^ "Higher-Order Functions and Lambdas - Kotlin Programming Language". Kotlin. Retrieved 2025-08-06.
  30. ^ "Programming in Lua : 6". www.lua.org. Retrieved 2025-08-06.
  31. ^ "Maple Programming: 1.6: Anonymous functions and expressions - Application Center". www.maplesoft.com. Retrieved 2025-08-06.
  32. ^ "Anonymous Functions - MATLAB & Simulink". www.mathworks.com. Retrieved 2025-08-06.
  33. ^ "Maxima 5.17.1 Manual: 39. Function Definition". maths.cnam.fr. Retrieved 2025-08-06.
  34. ^ "Nim Manual". nim-lang.github.io.
  35. ^ "Code Examples – OCaml". ocaml.org. Retrieved 2025-08-06.
  36. ^ "GNU Octave: Anonymous Functions". octave.org. Retrieved 2025-08-06.
  37. ^ "Function Literals". OpenSCAD User Manual. Wikibooks. Retrieved 22 February 2021.
  38. ^ "perlsub - Perl subroutines - Perldoc Browser". perldoc.perl.org. Retrieved 2025-08-06.
  39. ^ "PHP: Anonymous functions - Manual". www.php.net. Retrieved 2025-08-06.
  40. ^ "6. Expressions — Python 3.9.0 documentation". docs.python.org. Retrieved 2025-08-06.
  41. ^ "4.4 Functions: lambda". docs.racket-lang.org. Retrieved 2025-08-06.
  42. ^ "Functions". docs.raku.org. Retrieved 2025-08-06.
  43. ^ Sosinski, Robert (2025-08-06). "Understanding Ruby Blocks, Procs and Lambdas". Reactive.IO. Archived from the original on 2025-08-06. Retrieved 2025-08-06.
  44. ^ "Closures: Anonymous Functions that Can Capture Their Environment - The Rust Programming Language". doc.rust-lang.org. Retrieved 2025-08-06.
  45. ^ "Anonymous Functions". Scala Documentation. Retrieved 2025-08-06.
  46. ^ "Recitation 3: Higher order functions". www.cs.cornell.edu. Retrieved 2025-08-06.
  47. ^ "Closures — The Swift Programming Language (Swift 5.5)". docs.swift.org.
  48. ^ "Documentation - Everyday Types". www.typescriptlang.org. Retrieved 2025-08-06.
  49. ^ "Function Type - Typst Documentation". typst.app. Retrieved 2025-08-06.
  50. ^ a b "Projects/Vala/Tutorial - GNOME Wiki!". wiki.gnome.org. Retrieved 2025-08-06.
  51. ^ KathleenDollard (15 September 2021). "Lambda Expressions - Visual Basic". docs.microsoft.com. Retrieved 2025-08-06.
  52. ^ "Language Reference/Terms/Anonymous Predicates - wiki.visual-prolog.com". wiki.visual-prolog.com. Retrieved 2025-08-06.
  53. ^ "Pure Anonymous Function: Elementary Introduction to the Wolfram Language". www.wolfram.com. Retrieved 2025-08-06.
  54. ^ "Lambdas, Closures and everything in between · Issue #1048 · ziglang/zig". GitHub. Retrieved 2025-08-06.
[edit]
耷拉是什么意思 sb是什么元素符号 pw是什么意思 mu是什么单位 夏枯草治什么病
属鸡的什么命 梦见头发长长了是什么意思 七月二十六是什么星座 一个火一个斤念什么 八字华盖是什么意思
脾虚吃什么药 神什么气什么 尿蛋白微量是什么意思 蒙脱石散是什么成分 鲸属于什么类动物
otg线是什么 至多是什么意思 碱性磷酸酶偏低是什么意思 乙肝五项25阳性是什么意思 什么叫词牌名
什么的小朋友填词语hcv8jop5ns7r.cn 吃枸杞有什么功效zsyouku.com 王字旁一个行念什么hcv8jop4ns3r.cn 盆腔积液是什么引起的hcv7jop6ns6r.cn 月牙是什么hcv9jop1ns3r.cn
东南方五行属什么hcv7jop6ns2r.cn 三伏的伏是什么意思hcv7jop5ns4r.cn 海葡萄是什么东西hcv8jop7ns8r.cn 盆腔钙化灶是什么意思hcv9jop2ns2r.cn s1隐裂是什么意思hcv7jop6ns6r.cn
生津是什么意思hcv9jop0ns2r.cn 各生欢喜什么意思hcv8jop1ns9r.cn 梦见亲嘴是什么意思hcv8jop7ns0r.cn 腺肌症是什么hcv8jop2ns3r.cn 血细胞分析能查出什么weuuu.com
丙氨酸氨基转移酶高是什么意思hcv8jop1ns8r.cn 什么是象声词hcv7jop7ns1r.cn 耳浴10分钟什么意思imcecn.com coach是什么意思hcv8jop4ns8r.cn 云南简称是什么hcv8jop4ns3r.cn
百度