Is there a built-in function which will apply a function f
to x
when test
yields true? A typical usage scenario would be the application of some transformation function depending on the options:
wrapif[Log, mydata, OptionValue["ReturnLogValues"]]
(*instead of*)
If[TrueQ@OptionValue["ReturnLogValues"], Log@mydata, mydata]
It is just a small piece of code to implement, but I need it quite often and wondered why I never found anything built-in. Example implementations:
wrapif[f_, x_, True] := f[x];
wrapif[f_, x_, _] := x;
Or:
wrapif2[f_, x_, test_] := Nest[f, x, Boole[TrueQ@condition]]
"Filter" -> Indentity
options and use it likeOptionValue["Filter"] @ mydata
. No you can easily passLog
there. $\endgroup$myData // If[..., Log, Identity]
$\endgroup$wrapif3[f_, x_, test_] := Boole[test] f[x]+(1-Boole[test])x
orwrapif4[f_, x_, test_] := {f, Identity}[[2 - Boole[test]]]@x
? $\endgroup$wrapif5[f_, x_, test_] := First[RotateRight[{ Identity, f}, Boole[test]]]@x
? $\endgroup$GeneralUtilities`MapIf[f_ test_]
? $\endgroup$