Here's a MWE of what I want to do:
a = 1;
If[ValueQ[#], #, "Null"] & /@ {a, b, c}
(*Actual: {Null, Null, Null}*)
(*Expected: {1, Null, Null}*)
What am I not understanding about mapping pure functions? Or is my confusion elsewhere?
Here's a MWE of what I want to do:
a = 1;
If[ValueQ[#], #, "Null"] & /@ {a, b, c}
(*Actual: {Null, Null, Null}*)
(*Expected: {1, Null, Null}*)
What am I not understanding about mapping pure functions? Or is my confusion elsewhere?
Clear[a, b, c]
a = 1;
If[NumericQ[#], #, "Null"] & /@ {a, b, c}
{1, "Null", "Null"}
Or more generally
a = {5, 5};
If[! SymbolQ[#], #, "Null"] & /@ {a, b, c}
{{5, 5}, "Null", "Null"}
Using a method similar to the one here:
a = 1;
Reap[Scan[Function[Null, Sow[If[ValueQ[#], #, "Null"]], HoldFirst],
Hold[a, b, c]]][[-1, 1]]
{1, "Null", "Null"}
System`Private`HasAnyCodesQ might be better if the OP cares about things with *Values other than OwnValues.
– b3m2a1
Aug 05 '17 at 18:27
You did say "replace" so here is a Replace solution:
a = 1;
List @@ Replace[Hold[a, b, c], x_ /; ! ValueQ[x] :> "Null", {1}]
{1, "Null", "Null"}
Other Replace methods, should you prefer one of them:
Replace[Unevaluated[{a, b, c}], x_ /; ! ValueQ[x] :> "Null", {1}]
List @@ Replace[Hold[a, b, c], {x_?ValueQ :> x, _ -> "Null"}, {1}]
ValueQ? I'd like to use it in cases whereamight also be a list. So I basically want to return the variable in all cases except those in which it is undefined, and I'm not sure I can do that withNumericQ. – Shane Aug 05 '17 at 17:53{a, b, c}already turns it into{1, b, c}when run. – J. M.'s missing motivation Aug 05 '17 at 17:56If[NumericQ[#]||ListQ[#]...– Shane Aug 05 '17 at 17:57aas a list – eldo Aug 05 '17 at 18:06