Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Wrapping Begin["Context`"], ..., End[] into a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg106093] Re: Wrapping Begin["Context`"], ..., End[] into a function
  • From: dh <dh at metrohm.com>
  • Date: Fri, 1 Jan 2010 05:35:26 -0500 (EST)
  • References: <hhhmj3$o5o$1@smc.vnet.net>

Hi Leo,
to prevent defining symbols before the correct context has been
established, you may e.g. give your expression as a string:

Remove[f, x];

make[con_, myExp_] := (
  BeginPackage[con];
  Print["context=", $Context];
  ToExpression@myExp;
  EndPackage[];
  )

make["con1`", "f[x_]:=x^2"]

Daniel

On 31 Dez., 09:15, Leo Alekseyev <dnqu... at gmail.com> wrote:
> Dear Mathgroup,
>
> I was wondering if it's possible to write a function that evaluates an
> expression in a particular context.  In other words, instead of
> writing this:
>
> cpsave = $ContextPath;
> Begin["Materials1`"];
> $ContextPath = {"Materials1`", "System`"};
> myExpression;
> End[];
> $ContextPath = Global`cpsave;
>
> I would like to be able to make a function call, e.g.
> evaluateInContext["Materials1`",myExpression]
>
> The naive solution of
>  evaluateInContext[cont_,expr_]:=(
> cpsave = $ContextPath;
> Begin[cont];
> ...
> )
>
> doesn't work because any symbol in parentheses is bound to the current
> context before any evaluation takes place.  Is there a trick to make
> this work?
>
> Thanks, and happy New Year to all!
>
> --Leo



  • Prev by Date: Re: Re: More /.{I->-1} craziness
  • Next by Date: Re: Replace and ReplaceAll -- simple application
  • Previous by thread: Re: Wrapping Begin["Context`"], ..., End[] into a function
  • Next by thread: overimpose graphics on an imported image