4
$\begingroup$

This Query fail, due to divide by zero error message...

<|"a" -> 1, "b" -> 0|> // Query[#a/#b &]

Missing["Failed"]

...can be prevented by wrapping Quiet around the division:

<|"a" -> 1, "b" -> 0|> // Query[Quiet[#a/#b] &]

ComplexInfinity

But will not work if wrapped outside Query.

Is there a way to wrap Quiet around operators - eg it's handy to Apply[...] some operation around a list:

<|"a" -> 1, "b" -> 0, "c" -> 2|> // 
 Query[{{"a", "c"}, "c"} /* Apply[Divide]]

<|a->1/2,c->1|>

But

<|"a" -> 1, "b" -> 0, "c" -> 2|> // 
 Query[{{"a", "c"}, "b"} /* Apply[Divide]]

Missing["Failed"]

Of course if the operator is deprecated to a function, Quiet can be used, but that's not the point here:

<|"a" -> 1, "b" -> 0, "c" -> 2|> // 
 Query[{{"a", "c"}, "b"} /* Apply[Quiet[Divide[##]] &]]

<|"a" -> ComplexInfinity, "c" -> ComplexInfinity|>

$\endgroup$
3
$\begingroup$

I was going to delete the Q as I realized 2 min after posting that composing with Quiet works: maybe others will find it interesting:

<|"a" -> 1, "b" -> 0, "c" -> 2|> // 
 Query[{{"a", "c"}, "b"} /* Apply[Divide /* Quiet]]

<|"a" -> ComplexInfinity, "c" -> ComplexInfinity|>

$\endgroup$
2
$\begingroup$

Or FailureAction -> None + wrap with Quiet:

<|"a" -> 1, "b" -> 0|> //  Query[#a/#b &, FailureAction -> None] // Quiet
ComplexInfinity
<|"a" -> 1, "b" -> 0, "c" -> 2|> // 
   Query[{{"a", "c"}, "b"} /* Apply[Divide], FailureAction -> None] //
   Quiet
<|"a" -> ComplexInfinity, "c" -> ComplexInfinity|>
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.