Function Repository Resource:

DeleteFailures

Source Notebook

Delete failures in an expression

Contributed by: Michael Sollami

ResourceFunction["DeleteFailures"][expr]

deletes the failure cases in the given expression expr.

ResourceFunction["DeleteFailures"][expr,level]

deletes the failures in expr at the given level.

Details and Options

A failure is defined as anything that passes FailureQ. This includes $Failed, $Aborted and Failure objects.
ResourceFunction["DeleteFailures"] uses Cases, and so accepts the standard level specifications.

Examples

Basic Examples (3) 

Delete any failures in a list:

In[1]:=
ResourceFunction[
 "DeleteFailures"][{$Failed, $Aborted, "NotAFailure", Failure["", <||>]}]
Out[1]=

Delete any failures in an association:

In[2]:=
ResourceFunction[
 "DeleteFailures"] @ <|"a" -> 1, "b" -> $Failed, "c" -> Failure["", <||>], "d" -> 4, "e" -> $Aborted|>
Out[2]=

Delete any failures in a nested expression:

In[3]:=
ResourceFunction[
 "DeleteFailures"][{1, $Failed, 3, Failure[1, 1], <|"a" -> $Failed, "b" -> {1, 2, 3, $Failed}, "c" -> Failure[1, 1], "d" -> 4|>}]
Out[3]=

Scope (2) 

DeleteFailures works with level specifications:

In[4]:=
ResourceFunction["DeleteFailures"][{1, {2, $Failed}, $Failed}, {2}]
Out[4]=

DeleteFailures can be mapped as well:

In[5]:=
ResourceFunction[
 "DeleteFailures"] /@ {1, 2, {"a", "b", $Failed}, Failure["", <||>]}
Out[5]=

Applications (1) 

Failures arise many times when using Interpreter:

In[6]:=
ResourceFunction["DeleteFailures"][
 Interpreter["Integer"] /@ {"1", "2", "notnumber", 3}]
Out[6]=

Possible Issues (2) 

Just like DeleteMissing, DeleteFailures operates on the values of an Association:

In[7]:=
DeleteMissing[<|"a" -> "x", "b" -> "y", Missing -> "c"|>]
Out[7]=
In[8]:=
ResourceFunction[
 "DeleteFailures"][<|"a" -> "x", "b" -> "y", $Failed -> "c"|>]
Out[8]=

Using DeleteFailures on a Dataset can create ragged data:

In[9]:=
d = Dataset[<|"r1" -> <|"a" -> 1, "b" -> $Failed, "c" -> 3, "d" -> 4|>,
    "r2" -> <|"a" -> 5, "b" -> 6, "c" -> 7, "d" -> $Failed|>|>]
Out[9]=
In[10]:=
ResourceFunction["DeleteFailures"][d]
Out[10]=

Alternatively, use Normal to use the data directly:

In[11]:=
ResourceFunction["DeleteFailures"]@Normal@d
Out[11]=

Publisher

Michael Sollami

Version History

  • 1.0.0 – 27 February 2020

Related Resources

License Information