1

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?

Shane
  • 1,003
  • 1
  • 6
  • 18

3 Answers3

3
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"}
eldo
  • 67,911
  • 5
  • 60
  • 168
2

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"}
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
  • 2
    System`Private`HasAnyCodesQ might be better if the OP cares about things with *Values other than OwnValues. – b3m2a1 Aug 05 '17 at 18:27
2

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}]
Mr.Wizard
  • 271,378
  • 34
  • 587
  • 1,371