曹操原名叫什么| 血沉是查什么病的| 连云港有什么好吃的| 内痔疮用什么药治最好效果最快| 甲状腺素低吃什么能补| 维生素b6治什么病| 慢性心肌炎有什么症状| 小青龙是什么龙虾| 梦见捡手机是什么意思| 打破伤风针挂什么科| 黑松露是什么东西| 病退需要什么条件| 梦见卖鱼是什么意思| 报销是什么意思| 清凉的什么| hpv是什么检查| 世界上最长的单词是什么| 老鼠疮是什么病| 白带呈绿色是什么原因| gdp是什么意思啊| 为什么会得水痘| 织女是什么意思| 神经损伤吃什么药| 双肾实质回声增强是什么意思| 龟头起红点用什么药| 三月14号是什么星座| 手腕疼去医院挂什么科| 什么叫哮喘| 用劲的近义词是什么| 天干指的是什么| 肾构错瘤要注意什么| 什么叫口腔溃疡| 纳差是什么意思| 白细胞偏高什么原因| 心内科全称叫什么| 九里香什么时候开花| 四维什么时候做最佳| 猫打什么疫苗| 月经期后是什么期| 阴茎硬度不够吃什么好| 衣服38码相当于什么码| 候和侯有什么区别| 月经来有血块是什么原因| 温州人为什么会做生意| 手麻木吃什么药好| 三公是什么意思| 糖尿病的诊断标准是什么| 农历10月份是什么星座| 2月19日是什么星座| 风风火火是什么生肖| 两只小船儿孤孤零零是什么歌| 星期六打喷嚏代表什么| 女人手心脚心发热是什么原因| 三点水一个分读什么| 夸加包念什么| 吃什么消除肺部结节| 读书是为了什么| 精油是什么| 烹饪是什么意思| 古今内衣是什么档次| 猫贫血吃什么补血最快| 清炖鸡放什么调料| 梭边鱼是什么鱼| 耳朵发烫是什么征兆| 月办念什么| 梦见两条蛇是什么预兆| 12月13日是什么纪念日| 女性阴部痒是什么原因| 量贩式ktv是什么意思| 景气是什么意思| 身上起红疹是什么原因| 眼睛双重影什么原因| 五粮液是什么香型的酒| 黑加京念什么| 腔梗吃什么药| 为什么嘴巴老是干| 1月27日是什么星座| 苯是什么| 苏慧伦为什么不老| 江团鱼又叫什么鱼| 隶属什么意思| 包茎是什么意思| 皮蛋不能和什么一起吃| 劳改犯是什么意思| 睾丸炎用什么药| 6月1是什么星座| 机不可失的下一句是什么| 6月15是什么星座| 心电图异常q波什么意思| 马卡龙是什么| 外甥像舅舅有什么说法| 医生是什么生肖| 手腕长痣代表什么意思| 错构瘤是什么病| 唾液酸是什么| 电视为什么打不开| 日什么月什么的成语| 梦见死人复活是什么意思| 上火吃什么药好| 荷兰机场叫什么| 细胞是什么| 大腿麻木是什么原因| 脸部麻木是什么原因引起的| 高处不胜寒什么意思| 放屁不臭是什么原因| 胃息肉吃什么药治疗| 六味地黄丸起什么作用| 绿豆汤为什么是红色的| 98年属什么的| 雍正为什么不杀十阿哥| 紫癜是一种什么病| 六月二十四是什么星座| 健身有什么好处| 夜明珠是什么东西| 韩国人为什么叫棒子| 七六年属什么| 爱做梦是什么原因| 遇难呈祥是什么生肖| 女性夜尿多吃什么调理| 手上有红点是什么原因| 精分什么意思| 肾结石是什么原因导致的| 子宫腺肌症是什么原因引起的| nsfw什么意思| 月经期间可以喝什么茶| 农历五月的别称是什么| 蛋白尿吃什么药| 儿童腮腺炎挂什么科| 四十属什么| 蛇标志的车是什么牌子| 为什么脸上会长痘痘| 草莓是什么季节的| 人为什么会胡思乱想| 屁股痒是什么原因| 交警罚款不交有什么后果| 九牧王男装是什么档次| 76年属什么的生肖| 柔顺剂有什么用| 基尼是什么货币| 什么是音程| 2月7日是什么星座| 久坐脚肿是什么原因| 单核细胞是什么意思| 龙和什么生肖最配| 四离日是什么意思| 周围神经病是什么意思| 儿童枕头用什么枕芯好| 占有欲强什么意思| 鸡与什么生肖相合| 33朵玫瑰花代表什么| 什么是天眼| 一个令一个瓦念什么| 四月十一日是什么星座| 白眼球有红血丝是什么原因| cheese什么意思| 身披枷锁是什么生肖| 公司监事是干什么的| 交会是什么意思| 气滞血瘀吃什么中成药| 痛风吃什么药好| aq是什么| 激素六项是查什么的| 冠周炎吃什么药| 岳绮罗是什么来历| 药石是什么意思| 什么叫服务贸易| 小便有血是什么原因| covu是什么药| 小孩积食发烧吃什么药| 左胸隐隐作痛是什么原因| 为什么不建议治疗幽门螺杆菌| 喝什么可以解酒| 早晨起床口干口苦是什么原因| 什么叫体位性低血压| 心脏跳的快是什么原因| primark是什么牌子| 什么是虚拟币| 慢性结膜炎用什么眼药水| 走水是什么意思| 鸟牌是什么牌子的衣服| 景页读什么| 婴儿大便隐血阳性是什么意思| 梦见掉了一颗牙齿是什么征兆| 新陈代谢慢是什么原因| 犀利的眼神是什么意思| 三月14号是什么星座| 眼缘是什么意思| 左肾囊性灶是什么意思| 降压药什么时间吃最好| 血压高压高低压正常是什么原因| 白醋泡脚有什么效果| 溢脂性皮炎用什么药| 莳字五行属什么| 什么猫好看| 婴儿大便绿色是什么原因| 有什么办法| 不可或缺是什么意思| 升学宴选什么日子好| 宫颈息肉不切除有什么危害| 肚子胀恶心想吐是什么原因| 脊髓病变是什么病| 灌注是什么意思| scr医学上是什么意思| 脖子上长小肉粒是什么| 粗钢是什么| 秋田狐鱼钩适合钓什么鱼| 呼吸内科主要看什么病| 心脏跳动过快吃什么药| 808什么意思| 法尔如是是什么意思| 梦见车丢了是什么意思| 75年属什么的生肖| 阴虚阳亢吃什么中成药| 天是什么结构的字| 工科和理科有什么区别| 缠腰龙是什么病| 香茅是什么东西| 脚疼是什么原因引起的| 蜻蜓点水是什么行为| 杨梅泡酒有什么功效| 清酒是什么酒| 脸颊两边长痘痘是什么原因引起的| 五福是什么生肖| 小便浑浊是什么原因| 什么是幽门螺杆菌| 怀孕吃叶酸有什么用| 眉毛中间叫什么部位| 5月7日什么星座| 灰指甲有什么危害| 丁毒豆泡酒能治什么病| 6月份出生是什么星座| 马为什么站着睡觉| 眼窝凹陷是什么原因| 口腔扁平苔藓吃什么药好得快| 男人补身体吃什么好| 鱼肝油什么时候吃最好| 什么的亮光| 黑色皮肤适合什么颜色的衣服| 羊水偏少对胎儿有什么影响| 高炮是什么| 脚趾头麻木是什么原因| 鹌鹑蛋是什么动物的蛋| 头发变黄是什么原因| 双红出彩是什么生肖| 再生纤维是什么面料| 1999属什么生肖| 左手臂发麻是什么原因| 黄加蓝色是什么颜色| proof是什么意思| 乙状结肠管状腺瘤是什么意思| 胎儿窘迫什么意思| 值是什么意思| 罡什么意思| 疾控中心是做什么的| 梦见自己吃肉是什么预兆| 跑得最快的是什么生肖| 9月26号是什么星座| 东京热是什么| 家庭主妇是什么意思| 全可以加什么偏旁| 思维是什么| 什么是春天的什么| 什么饮料可以解酒| 感冒咳嗽吃什么水果好| 百度Jump to content

大学生用圆珠笔作画走红:校内画展告别大学生活

From Wikipedia, the free encyclopedia
(Redirected from Pattern guard)
百度 法里斯说,“沙姆自由人组织”之所以选择撤离,是受民意所迫,持续不断的空袭致使居民连续三个月不敢外出。

In computer programming, a guard is a Boolean expression that must evaluate to true if the execution of the program is to continue in the branch in question. Regardless of which programming language is used, a guard clause, guard code, or guard statement is a check of integrity preconditions used to avoid errors during execution.

The term guard clause is a Software design pattern attributed to Kent Beck who codified many often unnamed coding practices into named software design patterns, the practice of using this technique dates back to at least the early 1960's. The guard clause most commonly is added at the beginning of a procedure and is said to "guard" the rest of the procedure by handling edgecases upfront.

Uses

[edit]

A typical example is checking that a reference about to be processed is not null, which avoids null-pointer failures.

Other uses include using a Boolean field for idempotence (so subsequent calls are nops), as in the dispose pattern.

public String foo(String username) {
    if (username == null) {
        throw new IllegalArgumentException("Username is null.");
    }

    // Rest of the method code follows here...
}

Flatter code with less nesting

[edit]

The guard provides an early exit from a subroutine, and is a commonly used deviation from structured programming, removing one level of nesting and resulting in flatter code:[1] replacing if guard { ... } with if not guard: return; ....

Using guard clauses can be a refactoring technique to improve code. In general, less nesting is good, as it simplifies the code and reduces cognitive burden.

For example, in Python:

# This function has no guard clause
def f_noguard(x):
    if isinstance(x, int):
        #code
        #code
        #code
        return x + 1
    else:
        return None

# Equivalent function with a guard clause. Note that most of the code is less indented, which makes it easier to read and reason about
def f_guard(x):
    if not isinstance(x, int):
        return None
    #code
    #code
    #code
    return x + 1

Another example, written in C:

// This function has no guard clause
int funcNoGuard(int x) {
  if (x >= 0) {
    //code
    //code
    //code
    return x + 1; 
  } else {
    return 0;
  }
}

// Equivalent function with a guard clause
int funcGuard(int x) {
  if (x < 0) {
    return 0;
  }

  //code
  //code
  //code
  return x + 1; 
}

Terminology

[edit]

The term is used with specific meaning in APL, Haskell, Clean, Erlang, occam, Promela, OCaml, Swift,[2] Python from version 3.10, and Scala programming languages.[citation needed] In Mathematica, guards are called constraints. Guards are the fundamental concept in Guarded Command Language, a language in formal methods. Guards can be used to augment pattern matching with the possibility to skip a pattern even if the structure matches. Boolean expressions in conditional statements usually also fit this definition of a guard although they are called conditions.

Mathematics

[edit]

In the following Haskell example, the guards occur between each pair of "|" and "=":

f x
 | x > 0 = 1
 | otherwise = 0

This is similar to the respective mathematical notation:

In this case the guards are in the "if" and "otherwise" clauses.

Multiple guards

[edit]

If there are several parallel guards, they are normally tried in a top-to-bottom order, and the branch of the first to pass is chosen. Guards in a list of cases are typically parallel.

However, in Haskell list comprehensions the guards are in series, and if any of them fails, the list element is not produced. This would be the same as combining the separate guards with logical AND, except that there can be other list comprehension clauses among the guards.

Evolution

[edit]

A simple conditional expression, already present in CPL in 1963, has a guard on first sub-expression, and another sub-expression to use in case the first one cannot be used. Some common ways to write this:

(x>0) -> 1/x; 0
x>0 ? 1/x : 0

If the second sub-expression can be a further simple conditional expression, we can give more alternatives to try before the last fall-through:

(x>0) -> 1/x; (x<0) -> -1/x; 0

In 1966 ISWIM had a form of conditional expression without an obligatory fall-through case, thus separating guard from the concept of choosing either-or. In the case of ISWIM, if none of the alternatives could be used, the value was to be undefined, which was defined to never compute into a value.

KRC, a "miniaturized version"[3] of SASL (1976), was one of the first programming languages to use the term "guard". Its function definitions could have several clauses, and the one to apply was chosen based on the guards that followed each clause:

 fac n = 1,               n = 0
       = n * fac (n-1),   n > 0

Use of guard clauses, and the term "guard clause", dates at least to Smalltalk practice in the 1990s, as codified by Kent Beck.[1]

In 1996, Dyalog APL adopted an alternative pure functional style in which the guard is the only control structure.[4] This example, in APL, computes the parity of the input number:

parity{
        2? : 'odd'
              'even'
        }

Pattern guard

[edit]

In addition to a guard attached to a pattern, pattern guard can refer to the use of pattern matching in the context of a guard. In effect, a match of the pattern is taken to mean pass. This meaning was introduced in a proposal for Haskell by Simon Peyton Jones titled A new view of guards in April 1997 and was used in the implementation of the proposal. The feature provides the ability to use patterns in the guards of a pattern.

An example in extended Haskell:

 clunky env var1 var2
 | Just val1 <- lookup env var1
 , Just val2 <- lookup env var2
 = val1 + val2
 -- ...other equations for clunky...

This would read: "Clunky for an environment and two variables, in case the lookups of the variables from the environment produce values, is the sum of the values. ..." As in list comprehensions, the guards are in series, and if any of them fails the branch is not taken.

See also

[edit]

References

[edit]
  1. ^ a b Beck, Kent (1997). "Guard Clause". Smalltalk Best Practice Patterns,. pp. 178–179.
  2. ^ Cook, Nate. "guard & defer". NSHipster. Retrieved 2025-08-06.
  3. ^ Turner, D. A. "Some History of Functional Programming Languages" (PDF).
  4. ^ Scholes, John. "Direct Functions in Dyalog APL" (PDF).
[edit]
扁桃体长什么样 诸事顺遂是什么意思 嘴角发黑是什么原因 梨子什么季节成熟 打完升白针有什么反应
早上起床腰疼是什么原因 今年72岁属什么生肖 喝什么可以降血压 夜盲症是什么 流莺是什么意思
药物制剂是干什么的 口是心非是什么动物 b族维生素什么时候吃效果最好 张家界地貌属于什么地貌 什么泡茶好喝
大校军衔相当于什么官 补钙什么时间段最好 什么对什么 吃什么食品减肥 推迟月经吃什么药
特殊情况是什么意思hcv9jop0ns5r.cn 铜绿是什么hcv9jop4ns3r.cn 什么是负氧离子hcv8jop2ns9r.cn 睾丸痛挂什么科hcv9jop5ns9r.cn 吃茄子有什么坏处hcv8jop4ns8r.cn
fion属于什么档次的包hcv9jop7ns1r.cn 监制是干什么的hcv8jop9ns5r.cn 脚疼挂什么科hcv8jop8ns1r.cn 晚上睡觉盗汗是什么原因hcv7jop6ns7r.cn 三月什么星座hcv8jop8ns7r.cn
唏嘘不已的意思是什么hcv9jop1ns4r.cn 什么叫小三阳hcv8jop8ns1r.cn 太白金星叫什么hcv9jop5ns7r.cn dna里面有什么dayuxmw.com 荆棘是什么意思gysmod.com
取次是什么意思hcv8jop6ns1r.cn 左手大拇指抖动是什么原因hcv7jop5ns3r.cn 什么原因导致卵巢早衰hcv9jop2ns0r.cn 天乙贵人什么意思hcv8jop7ns6r.cn 平板电脑与笔记本电脑有什么区别hcv8jop6ns8r.cn
百度