Skip to main content
punctuation
Source Link
Reb.Cabin
  • 8.8k
  • 1
  • 34
  • 63

I need to create a user defined Block-defined Block function where the Block variablesBlock variables' values are defined by code. For example, imagine I have:

SetAttributes[myBlock1,HoldAll]
myBlock1[args_]:=Block[{a=1,b=2,c=3,d=4},args]
myBlock1[{a,b,c,d}]

{1, 2, 3, 4}

Now, what I need is something like:

SetAttributes[myBlock2,HoldAll]
varList={"a","b","c","d"};

myBlock2[args_]:=Module[{varArgs},
    varArgs=MapIndexed[ToString@Row[{#1,"=",#2[[1]]}]&,varList];
    ToExpression@ToString@Row[{"Block[",varArgs,",",ToString@args,"]"}]
]

myBlock2[{a,b,c,d}]

{1, 2, 3, 4}

The above function works, but it's very clumsy (using ToExpression) and error susceptible-susceptible. I tried something like:

SetAttributes[myBlock3,HoldAll]
varList={"a","b","c","d"};

myBlock3[args_]:=ReleaseHold[Hold@Block[varDef,args]/.varDef:>MapIndexed[(Evaluate@Symbol[#1]=#2[[1]])&,varList]]
myBlock3[{a,b,c,d}]

But without success in the "variables injection"injection." One important point is that aa, bb, cc and dd should not scapeescape from the block scope. How can I do that?

I need to create a user defined Block function where the Block variables values are defined by code. For example, imagine I have:

SetAttributes[myBlock1,HoldAll]
myBlock1[args_]:=Block[{a=1,b=2,c=3,d=4},args]
myBlock1[{a,b,c,d}]

{1, 2, 3, 4}

Now, what I need is something like:

SetAttributes[myBlock2,HoldAll]
varList={"a","b","c","d"};

myBlock2[args_]:=Module[{varArgs},
    varArgs=MapIndexed[ToString@Row[{#1,"=",#2[[1]]}]&,varList];
    ToExpression@ToString@Row[{"Block[",varArgs,",",ToString@args,"]"}]
]

myBlock2[{a,b,c,d}]

{1, 2, 3, 4}

The above function works, but it's very clumsy (using ToExpression) and error susceptible. I tried something like:

SetAttributes[myBlock3,HoldAll]
varList={"a","b","c","d"};

myBlock3[args_]:=ReleaseHold[Hold@Block[varDef,args]/.varDef:>MapIndexed[(Evaluate@Symbol[#1]=#2[[1]])&,varList]]
myBlock3[{a,b,c,d}]

But without success in the "variables injection". One important point is that a, b, c and d should not scape from the block scope. How can I do that?

I need to create a user-defined Block function where the Block variables' values are defined by code. For example, imagine I have:

SetAttributes[myBlock1,HoldAll]
myBlock1[args_]:=Block[{a=1,b=2,c=3,d=4},args]
myBlock1[{a,b,c,d}]

{1, 2, 3, 4}

Now, what I need is something like:

SetAttributes[myBlock2,HoldAll]
varList={"a","b","c","d"};

myBlock2[args_]:=Module[{varArgs},
    varArgs=MapIndexed[ToString@Row[{#1,"=",#2[[1]]}]&,varList];
    ToExpression@ToString@Row[{"Block[",varArgs,",",ToString@args,"]"}]
]

myBlock2[{a,b,c,d}]

{1, 2, 3, 4}

The above function works, but it's very clumsy (using ToExpression) and error-susceptible. I tried something like:

SetAttributes[myBlock3,HoldAll]
varList={"a","b","c","d"};

myBlock3[args_]:=ReleaseHold[Hold@Block[varDef,args]/.varDef:>MapIndexed[(Evaluate@Symbol[#1]=#2[[1]])&,varList]]
myBlock3[{a,b,c,d}]

But without success in the "variables injection." One important point is that a, b, c and d should not escape from the block scope. How can I do that?

edited tags
Link
Leonid Shifrin
  • 114.9k
  • 16
  • 333
  • 424
Tweeted twitter.com/#!/StackMma/status/303358011622174720
added 12 characters in body
Source Link
Murta
  • 26.4k
  • 6
  • 77
  • 168

I need to create a user defined Block function where the Block variables values are defined by code. For example, imagine I have:

SetAttributes[withSetAttributes[myBlock1,HoldAll]
myBlock[args_]myBlock1[args_]:=Block[{a=1,b=2,c=3,d=4},args]
myBlock[myBlock1[{a,b,c,d}]

{1, 2, 3, 4}

Now, what I need is something like:

SetAttributes[myBlockSetAttributes[myBlock2,HoldAll]
varList={"a","b","c","d"};

myBlock[args_]myBlock2[args_]:=Module[{varArgs},
    varArgs=MapIndexed[ToString@Row[{#1,"=",#2[[1]]}]&,varList];
    ToExpression@ToString@Row[{"Block[",varArgs,",",ToString@args,"]"}]
]

myBlock[myBlock2[{a,b,c,d}]

{1, 2, 3, 4}

The above function works, but it's very clumsy (using ToExpression) and error susceptible. I tried something like:

SetAttributes[myBlockSetAttributes[myBlock3,HoldAll]
varList={"a","b","c","d"};

myBlock[args_]myBlock3[args_]:=ReleaseHold[Hold@Block[varDef,args]/.varDef:>MapIndexed[(Evaluate@Symbol[#1]=#2[[1]])&,varList]]
myBlock[myBlock3[{a,b,c,d}]

But without success in the "variables injection". One important point is that a, b, c and d should not scape from the block scope. How can I do that?

I need to create a user defined Block function where the Block variables values are defined by code. For example, imagine I have:

SetAttributes[with,HoldAll]
myBlock[args_]:=Block[{a=1,b=2,c=3,d=4},args]
myBlock[{a,b,c,d}]

{1, 2, 3, 4}

Now, what I need is something like:

SetAttributes[myBlock,HoldAll]
varList={"a","b","c","d"};

myBlock[args_]:=Module[{varArgs},
    varArgs=MapIndexed[ToString@Row[{#1,"=",#2[[1]]}]&,varList];
    ToExpression@ToString@Row[{"Block[",varArgs,",",ToString@args,"]"}]
]

myBlock[{a,b,c,d}]

{1, 2, 3, 4}

The above function works, but it's very clumsy (using ToExpression) and error susceptible. I tried something like:

SetAttributes[myBlock,HoldAll]
varList={"a","b","c","d"};

myBlock[args_]:=ReleaseHold[Hold@Block[varDef,args]/.varDef:>MapIndexed[(Evaluate@Symbol[#1]=#2[[1]])&,varList]]
myBlock[{a,b,c,d}]

But without success in the "variables injection". One important point is that a, b, c and d should not scape from the block scope. How can I do that?

I need to create a user defined Block function where the Block variables values are defined by code. For example, imagine I have:

SetAttributes[myBlock1,HoldAll]
myBlock1[args_]:=Block[{a=1,b=2,c=3,d=4},args]
myBlock1[{a,b,c,d}]

{1, 2, 3, 4}

Now, what I need is something like:

SetAttributes[myBlock2,HoldAll]
varList={"a","b","c","d"};

myBlock2[args_]:=Module[{varArgs},
    varArgs=MapIndexed[ToString@Row[{#1,"=",#2[[1]]}]&,varList];
    ToExpression@ToString@Row[{"Block[",varArgs,",",ToString@args,"]"}]
]

myBlock2[{a,b,c,d}]

{1, 2, 3, 4}

The above function works, but it's very clumsy (using ToExpression) and error susceptible. I tried something like:

SetAttributes[myBlock3,HoldAll]
varList={"a","b","c","d"};

myBlock3[args_]:=ReleaseHold[Hold@Block[varDef,args]/.varDef:>MapIndexed[(Evaluate@Symbol[#1]=#2[[1]])&,varList]]
myBlock3[{a,b,c,d}]

But without success in the "variables injection". One important point is that a, b, c and d should not scape from the block scope. How can I do that?

Source Link
Murta
  • 26.4k
  • 6
  • 77
  • 168
Loading