Function Repository Resource:

ConfirmedMapping

Source Notebook

Map functions over data, stopping when an error is generated

Contributed by: Sjoerd Smit

ResourceFunction["ConfirmedMapping"][mapper, f, data, ]

is a modified version of a mapping operation such as Map that short-circuits when a failure is generated.

ResourceFunction["ConfirmedMapping"][mapper, ftest, data, ]

uses a test function test to check if values returned by f should be considered an error.

ResourceFunction["ConfirmedMapping"][mapper]

represents a mapping operation with error checking.

ResourceFunction["ConfirmedMapping"][mapper[f]]

represents the operator mapper[f] with added error checking.

ResourceFunction["ConfirmedMapping"][mapper][f]

is the same as ResourceFunction["ConfirmedMapping"][mapper[f]].

Details

The mapper can be any mapping function that takes a function as the first argument, data as the second, and any additional arguments after that. Common examples include Map, AssociationMap, KeyValueMap, MapAt, MapApply, MapThread and MapIndexed.
Even functions that aren't strictly mapping operations can work as long as they take arguments in the same way as the mapping functions. Apply and Fold, for example, will also work.

Examples

Basic Examples (2) 

Map OpenRead over a list of files, aborting when it fails:

In[1]:=
ResourceFunction[
 "ConfirmedMapping"][Map, OpenRead, {"ExampleData/USConstitution.txt",
   "thisfiledoesnotexist.png"}]
Out[1]=

Use a custom test function to determine whether there was a failure:

In[2]:=
ResourceFunction["ConfirmedMapping"][Map, 0/Echo@# & -> NumericQ, {1, 0, -1}]
Out[2]=

Scope (4) 

Define a function that returns a failure if its argument is not numeric:

In[3]:=
f[x_] := Enclose[ConfirmBy[x, NumericQ]^2]

Map the function over numbers:

In[4]:=
ResourceFunction["ConfirmedMapping"][Map, f, {1, 2, 3}]
Out[4]=

If f returns a Failure for any element, a failure will be thrown to the top level:

In[5]:=
ResourceFunction["ConfirmedMapping"][Map, f, {1, "x", 3}]
Out[5]=

Map at a deeper level:

In[6]:=
ResourceFunction["ConfirmedMapping"][Map, f, {{1}, {2}, {3}}, {2}]
Out[6]=
In[7]:=
ResourceFunction["ConfirmedMapping"][Map, f, {{1}, {"x"}, {3}}, {2}]
Out[7]=

Use AssociationMap as the mapper instead:

In[8]:=
ResourceFunction["ConfirmedMapping"][AssociationMap, f, {1, 2, 3}]
Out[8]=
In[9]:=
ResourceFunction["ConfirmedMapping"][AssociationMap, f, {1, "x", 3}]
Out[9]=

Use MapAt:

In[10]:=
ResourceFunction["ConfirmedMapping"][MapAt, f, {1, "x", 3}, {3}]
Out[10]=
In[11]:=
ResourceFunction["ConfirmedMapping"][MapAt, f, {1, "x", 3}, {2}]
Out[11]=

Use Confirm to throw the returned Failure up to the nearest Enclose:

In[12]:=
Enclose[
 1 + Confirm @ ResourceFunction["ConfirmedMapping"][Map, f, {1, "x", 3}]
 ]
Out[12]=

Specify a test for value returned by the function:

In[13]:=
ResourceFunction[
 "ConfirmedMapping"][Map, # + 1 & -> PrimeQ, {2, 4, 6}]
Out[13]=
In[14]:=
ResourceFunction[
 "ConfirmedMapping"][Map, # + 1 & -> PrimeQ, {2, 4, 5}]
Out[14]=
In[15]:=
ResourceFunction["ConfirmedMapping"][Apply, Plus -> PrimeQ, {1, 2}]
Out[15]=
In[16]:=
ResourceFunction["ConfirmedMapping"][Apply, Plus -> PrimeQ, {1, 2, 3}]
Out[16]=

Use the operator form to define a modified version of Map:

In[17]:=
f[x_] := Enclose[ConfirmBy[x, NumericQ]^2];
confirmMap = ResourceFunction["ConfirmedMapping"][Map];
In[18]:=
confirmMap[f, {1, 2, 3}]
Out[18]=
In[19]:=
confirmMap[f, {1, 2, "x"}]
Out[19]=

The new function supports operator forms as well:

In[20]:=
confirmMap[f]@{1, 2, 3}
Out[20]=
In[21]:=
confirmMap[f]@{1, 2, "x"}
Out[21]=

Alternatively, define the mapping of f as an operator directly:

In[22]:=
mapF = ResourceFunction["ConfirmedMapping"][Map[f]];
In[23]:=
mapF@{1, 2, 3}
Out[23]=
In[24]:=
mapF@{1, 2, "x"}
Out[24]=

Operator forms of MapAt:

In[25]:=
f[x_] := Enclose[ConfirmBy[x, NumericQ]^2]
In[26]:=
ResourceFunction["ConfirmedMapping"][MapAt][f, {1, 2, 3}, {2}]
Out[26]=
In[27]:=
ResourceFunction["ConfirmedMapping"][MapAt][f, {1, x, 3}, {2}]
Out[27]=
In[28]:=
ResourceFunction["ConfirmedMapping"][MapAt][f, {2}] @ {1, 2, 3}
Out[28]=
In[29]:=
ResourceFunction["ConfirmedMapping"][MapAt][f, {2}] @ {1, x, 3}
Out[29]=
In[30]:=
ResourceFunction["ConfirmedMapping"][MapAt[f, {2}]] @ {1, 2, 3}
Out[30]=
In[31]:=
ResourceFunction["ConfirmedMapping"][MapAt[f, {2}]] @ {1, x, 3}
Out[31]=

Publisher

Sjoerd Smit

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 03 May 2024

Related Resources

Author Notes

ConfirmedMapping uses the tagged version of Enclose and Confirm because they are much faster than the untagged versions when you need to map over a large amount of data.

License Information