[Date Index]
[Thread Index]
[Author Index]
Re: can one make local symbol in a pure function?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg127392] Re: can one make local symbol in a pure function?
*From*: Bill Rowe <readnews at sbcglobal.net>
*Date*: Fri, 20 Jul 2012 23:43:40 -0400 (EDT)
*Delivered-to*: l-mathgroup@mail-archive0.wolfram.com
*Delivered-to*: mathgroup-newout@smc.vnet.net
*Delivered-to*: mathgroup-newsend@smc.vnet.net
On 7/20/12 at 3:51 AM, nma at 12000.org (Nasser M. Abbasi) wrote:
>I was learning a bit about pure functions. Suppose we have this toy
>example:
>Clear[f, x]
>f[x_] := Module[{a = 2}, x^a]
>lst = {1, 2, 3};
>Map[f[#] &, lst]
>{1, 4, 9}
>Now I'd like to define the pure function in-line, using the Function[]
>syntax, but at the same time be able to make a local symbol 'a' like I
>did when using Module. But the syntax does not allow one to do that.
>There is no place to define it:
>lst = {1, 2, 3};
>Map[Function[{x}, x^2], lst]
>{1, 4, 9}
I really don't understand your motivation here. What is the
problem with using Module itself as part of the inline function
definition? That is,
Map[Function[{x}, Module[{a = 2}, x^a]], Range@3]
seems to satisfy all of your requirements other than perhaps an
implied requirement not to use Module.
Or you could use Block instead of Module as in
Map[Function[{x}, Block[{a = 2}, x^a]], Range@3]
>So if someone wants to make a temporary symbol to use for temporary
>calculation inside the pure function, like 'a' in this example, and at
>the same time not have it be global symbol, then one must use a Module?
This makes it sound like you want to have a local variable in
your function that does not create a new global symbol. If this
is your goal, you should be aware Module does not do this. If
you start a fresh session and do
?Global`*
f[x_]:=Module[{a=2},x^a]
f[Range@3];
?Global`*
you will find that new symbols a, a$ appear in the Global
context. They simply don't have values in the Global context
Prev by Date:
**Re: can one make local symbol in a pure function?**
Next by Date:
**Re: Options to know shape of functions**
Previous by thread:
**Re: can one make local symbol in a pure function?**
Next by thread:
**Re: can one make local symbol in a pure function?**
| |