Skip to main content
Search type Search syntax
Tags [tag]
Exact "words here"
Author user:1234
user:me (yours)
Score score:3 (3+)
score:0 (none)
Answers answers:3 (3+)
answers:0 (none)
isaccepted:yes
hasaccepted:no
inquestion:1234
Views views:250
Code code:"if (foo != bar)"
Sections title:apples
body:"apples oranges"
URL url:"*.example.com"
Saves in:saves
Status closed:yes
duplicate:no
migrated:no
wiki:no
Types is:question
is:answer
Exclude -[tag]
-apples
For more details on advanced search visit our help page
Results for injector pattern
Search options not deleted
13 votes
Accepted

How to set Block local variables by code?

The two standard methods are SlotSequence, and the "injector pattern." Related question on StackOverflow: How to Block Symbols without evaluating them? … {x_} :> x) myBlock[{a, b, c, d}] {1, 2, 3, 4} Injector pattern ClearAll[myBlock] SetAttributes[myBlock, HoldAll] varList = {"a", "b", "c", "d"}; myBlock[args_] := (MapIndexed[Set, Join @@ MakeExpression …
Mr.Wizard's user avatar
  • 275k
1 vote

How to properly inject iterators into table?

step and injector pattern First method, load my step function and use the injector pattern: z2 := {{a, {1, 2, 3}}, {b, Complement[{1, 2, 3}, {a}]}, {c, Complement[{1, 2, 3}, {a, b}]}} …
Mr.Wizard's user avatar
  • 275k
8 votes

How to pass a list of arguments into HoldAll

Nested injector pattern The essence of nested injector pattern is that sometimes we need to inject more than one different piece of code in a single go. … But a simple injector pattern does not allow it. The nested injector pattern allows one to accumulate several such expressions by using nested rules. …
Leonid Shifrin's user avatar
8 votes

How to pass a symbol name to a function with any of the Hold attributes?

Injector pattern: list = {1, 2, 3}; MakeExpression["list"] /. …
Mr.Wizard's user avatar
  • 275k
7 votes
Accepted

Put local variables for Block in a variable

This could be another case for the injector pattern: var = Hold@{x = 2, y = 3, z = 4} var /. Hold[inj_] :> Block[inj, x*y*z] (*24*) ?x (*Global`x --- so we did not leak*) …
Ajasja's user avatar
  • 13.8k
2 votes
3 answers
183 views

Dependency injection - use a specific implementation of one function within another

I know I can do this using Block wrapped around the call to f and g but I was curious if some cunning extension of UpValues would work; more like a dependency injector pattern. … My experience of using UpValues is very limited and reading the documentation for UpValues it would appear to be appropriate only when the precise pattern of h within f or g is known. …
Ymareth's user avatar
  • 4,811
7 votes
Accepted

Generating compiled functions from inactive expressions

Hold[code_] :> Compile @@ Hold[{y}, code] which is, using injector pattern. …
Leonid Shifrin's user avatar
20 votes
Accepted

Constructing symbol definitions for With

Here is my version using injector pattern: ClearAll[myWith]; SetAttributes[myWith,HoldAll]; myWith[pars_=vals_,body_]:= Apply[Set,Hold[Evaluate[Transpose[{pars,vals}]]],{2}]/. …
Leonid Shifrin's user avatar
0 votes

How to Block built-in functions conditionally in a single Block statement?

Here are few alternatives, using what I think I've seen called an injector pattern (which I just found in this answer by @Mr.Wizard and might be considered a duplicate): $condition = True; If[$condition …
Goofy's user avatar
  • 3,762
14 votes
Accepted

Displaying a series obtained by evaluating a Taylor series

/3 - 1/4 + 1/5 - 1/6 One may simplify this method using the undocumented function RuleCondition as WReach shows: Replace[hf, x_ :> RuleCondition[x], {2}] 1 - 1/2 + 1/3 - 1/4 + 1/5 - 1/6 #2 Injectorpattern: {1, -(1/2), 1/3, -(1/4), 1/5, -(1/6)} /. …
Mr.Wizard's user avatar
  • 275k
4 votes

function definition with a given list of parameters: how to use Evaluate[] properly?

While Kuba's answer is simpler, what you asked for can be accomplished also rather easily with the help of the nested injector pattern: variables /. … {vars__} :> (Map[Pattern[#, Blank[]] &, {vars}] /. {patts__} :> (f[{patts}, x_] := g[vars, x])) …
Leonid Shifrin's user avatar
3 votes
1 answer
110 views

Prevent argument substitution in held expression when injecting into unevaluated code

Can it be solved with the injector pattern or the Trott-Strzebonski in-place evaluation? …
István Zachar's user avatar
12 votes
3 answers
2k views

Pattern matching a pattern with patterns

To check b against a pattern based on a, I switch out a collection of heads inject new patterns and condition the pattern on the switched out heads: myPatternPatternA=(a/. … pattern I construct, in the new pattern {f,x,y,g,h} can all take arbitrary values since I'm injecting a new pattern for them after removing {HoldPattern,Pattern,Blank,RuleDelayed}. …
jVincent's user avatar
  • 15k
11 votes

Function in Table

Injector pattern to the rescue: Range@3 /. i_Integer :> (a[[i]] Sin[#] &) Replace[Range@3, i_ :> (a[[i]] Sin[#] &), 1] Table[j /. i_ :> (a[[i]] Sin[#] &), {j, 3}] Or using \[Function] and Array: …
Mr.Wizard's user avatar
  • 275k
4 votes

Generate list of strings from a list of assigned variables

Here is another alternative, in which the values of the variables are temporarily cleared using Block and an injector pattern. …
Michael E2's user avatar
  • 259k

1
2 3 4 5
7
15 30 50 per page