ConfirmBy

ConfirmBy[expr,f]

confirms that f[expr] returns True, otherwise throwing an error to the nearest surrounding Enclose.

ConfirmBy[expr,f,info]

evaluates info and includes its value in the thrown error if expr is not confirmed.

ConfirmBy[expr,f,info,tag]

uses the specified tag for any thrown errors.

Details

  • ConfirmBy[expr,] returns expr if expr is successfully confirmed.
  • ConfirmBy is dynamically scoped when given a tag. Otherwise, it is lexically scoped. »
  • The default value of info is Null.
  • Failure objects generated by ConfirmBy include the following fields:
  • "Information"info from ConfirmBy[expr,f,info,]
    "Expression"expr from ConfirmBy[expr,]
    "Function"f from ConfirmBy[expr,f,]
  • Values of these fields can be extracted using failure["prop"] or directly through Enclose[expr,"prop"].

Examples

open allclose all

Basic Examples  (2)

ConfirmBy[expr,f] gives expr if f[expr] evaluates to True:

Since "foo" is not a number, ConfirmBy stops evaluation and throws an error to the surrounding Enclose:

Create a function that checks its arguments:

Scope  (3)

ConfirmBy stops evaluation and returns to the matching Enclose when it throws an error:

info is only evaluated when expr cannot be confirmed:

Create a function that calls ConfirmBy with a tag so that it can be caught dynamically:

If no tag is given, ConfirmBy is lexically scoped:

Applications  (1)

Print a message when a failure is detected:

Properties & Relations  (1)

ConfirmBy can be emulated with ConfirmMatch:

Possible Issues  (3)

ConfirmBy must be given a tag or be within a lexical Enclose:

Untagged ConfirmBy works by replacing instances of the symbol ConfirmBy with a tagged variant:

Tagged ConfirmBy requires an Enclose with a matching tag:

Wolfram Research (2020), ConfirmBy, Wolfram Language function, https://reference.wolfram.com/language/ref/ConfirmBy.html.

Text

Wolfram Research (2020), ConfirmBy, Wolfram Language function, https://reference.wolfram.com/language/ref/ConfirmBy.html.

CMS

Wolfram Language. 2020. "ConfirmBy." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/ConfirmBy.html.

APA

Wolfram Language. (2020). ConfirmBy. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ConfirmBy.html

BibTeX

@misc{reference.wolfram_2024_confirmby, author="Wolfram Research", title="{ConfirmBy}", year="2020", howpublished="\url{https://reference.wolfram.com/language/ref/ConfirmBy.html}", note=[Accessed: 17-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_confirmby, organization={Wolfram Research}, title={ConfirmBy}, year={2020}, url={https://reference.wolfram.com/language/ref/ConfirmBy.html}, note=[Accessed: 17-November-2024 ]}