Function Repository Resource:

IfSatisfies

Source Notebook

Pass a value to a function if the value satisfies a condition

Contributed by: Roy Sianez

ResourceFunction["IfSatisfies"][expr,condition,f,o]

applies f to expr if condition[expr] evaluates to True; otherwise, applies o to expr.

Details

expr can be any expression.
condition, f and o must all be functions or names of functions.

Examples

Basic Examples (1) 

Produce a String telling whether a number is even or odd:

In[1]:=
ResourceFunction["IfSatisfies"][7, EvenQ, ToString[#] <> " is even" &,
  ToString[#] <> " is odd" &]
Out[1]=

Scope (3) 

The condition, as well as f and o, can be the name of a function:

In[2]:=
ResourceFunction[
 "IfSatisfies"]["Hello", DictionaryWordQ, ToUpperCase, SpellingCorrectionList]
Out[2]=
In[3]:=
ResourceFunction[
 "IfSatisfies"]["Heyyyy", DictionaryWordQ, ToUpperCase, SpellingCorrectionList]
Out[3]=

Or they can be pure functions, or a mixture of both names of functions and pure functions:

In[4]:=
ResourceFunction["IfSatisfies"]["Hello", ContainsAll[{"hey", "hi", "hello"}, {ToLowerCase[#]}] &, # <> " there!" &, # &]
Out[4]=

Symbolic expressions are allowed. This example applies f to the expression if it has two items; otherwise, it applies g:

In[5]:=
ResourceFunction["IfSatisfies"][{x, y}, MatchQ[{_, _}], f, g]
Out[5]=

Applications (1) 

If a word has conjugations or forms other than itself, return them; otherwise, return Missing[]:

In[6]:=
(* Evaluate this cell to get the example input *) CloudGet["https://www.wolframcloud.com/obj/ae335569-d367-46f6-919a-65fb2e763e95"]
In[7]:=
Column[# -> wordForms[#] & /@ {"skateboard", "the", "disambiguate"}]
Out[7]=

Properties and Relations (1) 

If the condition evaluates to something other than True or False, the "otherwise" function (the last argument) is evaluated. Here, the condition returns an image of a hot dog:

In[8]:=
ResourceFunction["IfSatisfies"][expr, \!\(\*
GraphicsBox[
TagBox[RasterBox[CompressedData["
1:eJwVlIlTk2kSxq3VdUcQkhCSkISEkITkyx1yQG5yEMDcJxBjhECC3GIOQTwW
uVEYQG6VHRVH1/FAUEZFRYFVEQS13B2ZcURrRlccXY/5C/a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yASEYgEODIBxJKJLBrABags0DCRkARQmRwWj8cVigUSWapcKVVplHpZ
mo0vcSQxMxjcDLk6T5hWwOCZOQIdwJBhsUlwOJaAA1gAG40iglgMOhFcGZcj
lIjkoDhMPoVEx6DwCXgSmUSjJNFpdDaHzeezeeBawY+oFCYEAoNGQ1BIPDwW
Dd46CoULTiIRWDgMvwbEUJLITDaDRwdY5ESARmXSaWwmbW0GjyP+H1+tn54=

"], {{0, 20.}, {30., 0}}, {0, 255},
ColorFunction->RGBColor,
ImageResolution->{72, 72}],
BoxForm`ImageTag[
      "Byte", ColorSpace -> ColorProfileData[CompressedData["
1:eJxTTMoPSmJkYGAQYAISDEwCOcm5xUwCDAy5eSVFQe5OChGRUQrsdxgYGSQZ
mBk0GSwTk4sLHAMCfBhwgm/XGEAGMlzWBZmFWx1WwJWSWpwMpP8AcUpyQVEJ
AwNjApDNXV5SAGLnANkiSdlgdgOIXQR0IJA9BcROh7BXgNVA2HvAakKCnIHs
C0C2QzoSOwmJDbUXBJiTjWDOYWZIZjBioBYoSa0oAdGeEQwMoHCFiCLCCyHG
LAbExsD4WIIQy1/EwGDxFSg+ASGWNJOBYXsrA4PELYSYygIGBv4WBoZt55NL
i8qgVksB8WnGk8zJrJM4srm/CdiLBkqbKH7UnGAkYT3JjTWwPPZtdkEVa+fG
WTVrMvfXXj780uD/fwADIFYV
"], "RGB", "XYZ"], Interleaving -> True, MetaInformation -> <|"Exif" -> <|"XResolution" -> 72, "YResolution" -> 72, "ResolutionUnit" -> "Inch", "DateTime" -> DateObject[{2021, 1, 27, 16, 55, 58.}, "Instant", "Gregorian", None], "ExifTag" -> 110, "ColorSpace" -> "Uncalibrated", "PixelXDimension" -> 30, "PixelYDimension" -> 20|>, "XMP" -> <|"PhotoshopSchema" -> <|"ColorMode" -> 3, "ICCProfile" -> "c2"|>, "XMPBasicSchema" -> <|"ModifyDate" -> DateObject[{2021, 1, 27, 16, 55, 58.}, "Instant", "Gregorian", -5.], "MetadataDate" -> DateObject[{2021, 1, 27, 16, 55, 58.}, "Instant", "Gregorian", -5.]|>, "XMPMediaManagementSchema" -> <|"History[1]" -> <|"Event" -> <|"Action" -> "produced", "SoftwareAgent" -> "Affinity Photo (Nov  6 2020)", "When" -> DateObject[{2021, 1, 27, 16, 55, 58.}, "Instant", "Gregorian", -5.]|>|>|>|>|>],
Selectable->False],
DefaultBaseStyle->"ImageGraphics",
ImageSizeRaw->{30., 20.},
PlotRange->{{0, 30.}, {0, 20.}}]\) &, "Condition evaluated to True" &,
  "Condition did not evaluate to True" &]
Out[8]=

Possible Issues (2) 

All arguments to this function except the first are required to be functions. Therefore, the following will not work:

In[9]:=
ResourceFunction[
 "IfSatisfies"][7, EvenQ, "The number is even", "The number is odd"]
Out[9]=

Rather, make the arguments into pure functions like so:

In[10]:=
ResourceFunction["IfSatisfies"][7, EvenQ, "The number is even" &, "The number is odd" &]
Out[10]=

Publisher

Roy Sianez

Version History

  • 1.0.0 – 03 February 2021

Author Notes

Inspired by the "if let" construct in the Swift programming language.

License Information