5 replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
source | link

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var];
SetAttributes[f, HoldAll];

f[x]
(* False *)

x = 10;    
f[x]
(* True *)

It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)

The problem is that Map evaluates x and pass the value of x to f, so ValueQ11 gives false. How to get the Map to give the right sequence {True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)

but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

EDIT II Somehow solutions provided by Mr.Wizard, Kuba and Algohi work fine in my case, but are not so easy as to add Unevaluated in the Map inside the f function, as suggested by kuglerkugler.

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var];
SetAttributes[f, HoldAll];

f[x]
(* False *)

x = 10;    
f[x]
(* True *)

It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)

The problem is that Map evaluates x and pass the value of x to f, so ValueQ1 gives false. How to get the Map to give the right sequence {True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)

but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

EDIT II Somehow solutions provided by Mr.Wizard, Kuba and Algohi work fine in my case, but are not so easy as to add Unevaluated in the Map inside the f function, as suggested by kugler.

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var];
SetAttributes[f, HoldAll];

f[x]
(* False *)

x = 10;    
f[x]
(* True *)

It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)

The problem is that Map evaluates x and pass the value of x to f, so ValueQ1 gives false. How to get the Map to give the right sequence {True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)

but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

EDIT II Somehow solutions provided by Mr.Wizard, Kuba and Algohi work fine in my case, but are not so easy as to add Unevaluated in the Map inside the f function, as suggested by kugler.

4 added 264 characters in body; edited tags
source | link

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var];
SetAttributes[f, HoldAll];

f[x]
(* False *)

x = 10;    
f[x]
(* True *)

It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)

The problem is that Map evaluates x and pass the value of x to f, so ValueQ[1]ValueQ1 gives false. How to get the Map to give the right sequence {True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)

but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

EDIT II Somehow solutions provided by Mr.Wizard, Kuba and Algohi work fine in my case, but are not so easy as to add Unevaluated in the Map inside the f function, as suggested by kugler.

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var];
SetAttributes[f, HoldAll];

f[x]
(* False *)

x = 10;    
f[x]
(* True *)

It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)

The problem is that Map evaluates x and pass the value of x to f, so ValueQ[1] gives false. How to get the Map to give the right sequence {True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)

but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var];
SetAttributes[f, HoldAll];

f[x]
(* False *)

x = 10;    
f[x]
(* True *)

It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)

The problem is that Map evaluates x and pass the value of x to f, so ValueQ1 gives false. How to get the Map to give the right sequence {True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)

but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

EDIT II Somehow solutions provided by Mr.Wizard, Kuba and Algohi work fine in my case, but are not so easy as to add Unevaluated in the Map inside the f function, as suggested by kugler.

    Tweeted twitter.com/#!/StackMma/status/573589802496495616
3 added 931 characters in body
source | link

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var]ValueQ[var];
SetAttributes[f, HoldAll]HoldAll];

f[x]
False
(* False *)

x = 10;    
f[x]
(* True *)
True

It works, as long as I call ff passing the name of variable. But, if I want to call ff on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)

{False, False, False}

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)

{False, False, False}

The problem is that MapMap evaluates xx and pass the value of xx to ff, so ValueQ[10]ValueQ[1] gives Falsefalse. How to get the MapMap to give the right resultsequence {True, True, True}{True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)

but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var]
SetAttributes[f, HoldAll]
f[x]
False
x = 10;
f[x]
True

It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]

{False, False, False}

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]

{False, False, False}

The problem is that Map evaluates x and pass the value of x to f, so ValueQ[10] gives False. How to get the Map to give the right result {True, True, True}.

First of all, I'm not sure about the title of this question, so please correct me if there is any title more sensible than mine. The problem is the following: I have a function f that checks if a variable as a value. For that, I need to pass the argument "by reference" that is I set HoldAll to f.

f[var_] := ValueQ[var];
SetAttributes[f, HoldAll];

f[x]
(* False *)

x = 10;    
f[x]
(* True *)

It works, as long as I call f passing the name of variable. But, if I want to call f on a list of variables, and I use something like a Map, it doesn't work

Map[f, {x, y, z}]
(* {False, False, False} *)

Note that x has a value and the result should be {True, False, False}.

Even more clear:

{x, y, z} = {1, 2, 3};
Map[f, {x, y, z}]
(* {False, False, False} *)

The problem is that Map evaluates x and pass the value of x to f, so ValueQ[1] gives false. How to get the Map to give the right sequence {True, True, True}. It could be a very stupid solution but today I'm not able to get it. Of course, the actual code is a little bit more complex, I tried to simplify it so to provide a clear example. Thanks for any help.

EDIT Thanks to kguler, using Unevaluated works for the example I provided above, but not always for my real case. As for the other answer, that using Listable, it works as well but even that is not suitable for my real code because actually my f does many other things and cannot be easily applied to a list (moreover it has many other arguments, not just the variable). I'll try to add here a more realistic example of what I have. Considering this variation:

f[vars : {(_) ..}] := Map[ValueQ, vars];
SetAttributes[f, HoldAllComplete];

f[{x}]
(* {False} *)

x = 10; f[{x}]
(* {False} *)

f[{Unevaluated@x, Unevaluated@y}]
(* {True, False} *)

but if I have many variables and I don't know the names, for instance if I have all names of variables in another variable, like the following

variables = {x, y, z, q, w, e, r, t}

How to call f using Unevaluated? The only solution I can think of is to apply manually the Unevaluated to all variables. Is there another way?

2 Routine clean-up
source | link
1
source | link