Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #11010
| From | Bill Rowe <readnews@sbcglobal.net> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Re: can one make local symbol in a pure function? |
| Date | 2012-07-21 03:46 +0000 |
| Message-ID | <jud8n8$j33$1@smc.vnet.net> (permalink) |
| Organization | Time-Warner Telecom |
On 7/20/12 at 3:51 AM, nma@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
Back to comp.soft-sys.math.mathematica | Previous | Next — Next in thread | Find similar | Unroll thread
Re: can one make local symbol in a pure function? Bill Rowe <readnews@sbcglobal.net> - 2012-07-21 03:46 +0000 Re: can one make local symbol in a pure function? "Nasser M. Abbasi" <nma@12000.org> - 2012-07-22 08:35 +0000
csiph-web