Skip to main content

Questions tagged [meta-programming]

The tag has no usage guidance.

Filter by
Sorted by
Tagged with
2 votes
1 answer
40 views

Intercepting typos/misstyped symbols and automatically correcting them in the FrontEnd

I often mistype Print[] as PRint[] and Remove[] as REmove[]. I was wondering if its possible to use $NewSymbol to intercept ...
Tapiocaweasel's user avatar
0 votes
2 answers
58 views

Streamlining Dataset Select meta-programming

Imagine you are deep in studying the Powerball Winning Numbers starting from 2010: ...
Victor K.'s user avatar
  • 5,191
8 votes
1 answer
454 views

Rule rewriting behavior

This is kind of meta-programming attempt. Below are two snippets: Snippet 1: ...
Pavel Perikov's user avatar
1 vote
3 answers
132 views

Self modifying association (in a search of "THIS" keyword)

Imagine we have the following structure (like a module) ...
Kirill Vasin's user avatar
  • 1,245
3 votes
2 answers
475 views

Including literals into compiled C code

I have a function that I want to Compile into C for speed. Inside this function is a certain long expression called x, which has been computed earlier in a Mathematica session. However, if you call x ...
Bruce Bartlett's user avatar
19 votes
1 answer
461 views

Programming language prototyping in Mathematica

Are you aware of any projects using the Wolfram language resp. Mathematica as an environment to explore the design of programming languages - in particular languages with a focus on mathematics (...
Math Gaudium's user avatar
9 votes
1 answer
427 views

How to write a simple interpreter / DSL with Wolfram Language?

I am specifically trying to write an interpreter for Mathematica predicates to MongoDB aggregation stages, but I wanted to phrase the question a bit more generically because I can see many times one ...
Dan's user avatar
  • 870
9 votes
1 answer
155 views

How to define a version of Table[] which localizes iteration variable by Module[]?

For reusable code it's good practice to use Module[] to localize iteration variables used by Table[], as explained by this post. ...
felix's user avatar
  • 549
1 vote
1 answer
166 views

How to use Through by default on numeric functions?

I've used the following code to simulate defining operations between functions: ...
jjagmath's user avatar
  • 411
14 votes
4 answers
541 views

Can we make an "Outerable" Attribute?

We can declare a function to have the Listable Attribute so that it automatically threads over lists in a particular way. For functions of multiple arguments it ...
Tanner Legvold's user avatar
4 votes
0 answers
148 views

What is the best coding style for performance?

The problem of Mathematica is there are too many ways to perform one task. That leads to confusion for new users because the performances of these methods are so different. I'm learning the best ...
Nam Nguyen's user avatar
  • 1,761
5 votes
1 answer
116 views

Turn definitions into rules

I know that this seems to be unnecessary, but here I have a bunch of assignments, like around 50, and I want to create rules for all these to have the freedom to apply these rules only on certain ...
Ying Zhang's user avatar
3 votes
1 answer
106 views

How to do meta-programming inside Compile?

I'd like to do this: solve some linear systems, compiling the task of getting the building up the matrices from parameter values, and then I have a compiled implementation of LinearSolve. The problem ...
NathanRL's user avatar
  • 127
12 votes
4 answers
1k views

Defining a Function programmatically

I need to create a function programmatically. For example, suppose I've got: mon - a Symbol mons - a List of Symbols vars - another List of Symbols (same Length) and want to make a function as: <...
Chris K's user avatar
  • 20.3k
4 votes
1 answer
211 views

Functions that remember some arguments while not remembering other arguments

I would like to some programming that is very generic. Particularly I am interested in the following: Let's say I want to write a function ...
Michael's user avatar
  • 123
3 votes
1 answer
84 views

Reducing boilerplate with associations

I'm looking for advice either on alternative ways to approach my problem in general, or specifically on meta-programming (which seems to me to be a way around this). In my work I have databases of ...
Dan's user avatar
  • 870
2 votes
0 answers
102 views

Get matching types for pattern

I want to take a pattern and get a set of matching types. The set of types does not need to be complete, but it needs to satisfy, say, 90% of the cases. Here's is a (definitely not complete) set of ...
b3m2a1's user avatar
  • 46.9k
1 vote
3 answers
226 views

How to randomly generate Tree or Nested List of Function in Mathematica?

In detail, I want to generate randomly something like Divide[Add[X,Times[X,Y]],Times[X,3]] or {Divide,{Add,X,{Times{X,Y}}},{Times,X,3}} (Specified Function(# of argument is from 2 to 5)) + ...
Xminer's user avatar
  • 1
2 votes
2 answers
403 views

Plot in a loop?

I have a list of objects, which I can plot manually: ...
Adobe's user avatar
  • 215
3 votes
2 answers
120 views

How to rewrite a function in a more compact and generalized way?

Consider a function as ...
user36426's user avatar
  • 3,355
8 votes
5 answers
565 views

Mathematica function which formats Physics calculation

One small step toward automating homework :) My lecturer doesn't accept calculations done in Mathematica code as homework. I have 30 pages of small calculations such as the one below. I don't want to ...
Conor's user avatar
  • 7,449
2 votes
2 answers
85 views

How to block list of variable that is of string form?

For example, I have varList={"a1","a2",...} a1, a2,... are all variables, they maybe ...
matheorem's user avatar
  • 17.2k
10 votes
3 answers
308 views

How to improve the creation of tables of code and comments

While writing a response to a certain MSE question I made a function that tabulates code and comments. (See the definition below.) Here is an example: ...
Anton Antonov's user avatar
5 votes
2 answers
2k views

How to set all variables in function as local

Here is an example code: a = 1; b = 1; c = a + b; f[x_, y_] := Module[{a = 3, b = 2, c}, c = (x + y)*(a + b)]; a,b,c are ...
lio's user avatar
  • 2,396
5 votes
1 answer
278 views

Meta information about function parameters

I'm working on a project that allows for executing arbitrary, user-defined Mathematica scripts. It's an internal tool running on a trusted, local network. It's basically a web interface to Mathematica....
isanae's user avatar
  • 153
10 votes
2 answers
514 views

Which operators are missing from the official precedence table?

I often use the Operator Input Forms page as a reference for operator precedence as well as which (used or unused) operators exist. However, I just noticed that at least one operator is missing from ...
Martin Ender's user avatar
  • 8,774
8 votes
3 answers
621 views

Metaprogramming: creating compiled functions from inter-dependent code blocks

I want to create a compiled function from inter-dependent code blocks. Here is a minimal example showing the salient features based on strings: ...
mrupp's user avatar
  • 777
4 votes
2 answers
141 views

Handle Format when importing package file content unevaluated

This question and its answers explains two methods which allow to import the content of a package file as held expressions. That is very useful as one then can manipulate that imported held code as ...
Albert Retey's user avatar
  • 23.6k
7 votes
1 answer
137 views

Applying And to a list inside a Function

And has HoldAll attribute and test its argument by sequence. But doing And@@{} will lost ...
vapor's user avatar
  • 7,921
4 votes
1 answer
381 views

Generate assignment code to symbols from string

I want the function to take in string, then assign a value to its corresponding symbol. An implementation using ToExpression is ...
vapor's user avatar
  • 7,921
18 votes
1 answer
441 views

Macro functions in GeneralUtilities?

Questions have been asked about the Macros package before (e.g., (83815)), but I'm interested if anyone has some examples of using the macro related functions in GeneralUtilities: ...
M.R.'s user avatar
  • 31.5k
2 votes
0 answers
371 views

Best approach for 'manual' common subexpression elimination

I have working code, but am looking for ways to make it more elegant. I have a fairly large expression with plenty of repetition and structure. A shortened toy example is ...
Åsmund Hj's user avatar
8 votes
1 answer
243 views

How to efficiently write code with many functions that have variable number of iterators/arguments/

I am finding myself having to code functions like this: $$\text{answer} = \sum_{k_0 = 0}^{\color{Blue}n_0} \sum_{k_1=0}^{\color{Blue}n_1}\cdots\sum_{k_{N-1}=0}^{\color{Blue}n_{N-1}} \frac{P_{k_0,k_1,\...
QuantumDot's user avatar
  • 19.7k
4 votes
0 answers
290 views

Delimited Continuations: Easy? or Fundamentally Difficult?

Apologies in advance for the length of this question, but this is as short as I have been able to make it. The Prescription I'm looking at the following tutorial on delimited (or composable) ...
Reb.Cabin's user avatar
  • 8,661
7 votes
2 answers
399 views

Add semicolon at the end of code line programmatically

Suppose we have several lines of code in a cell: a b; Module[{}, c d] e How can we wrap them in a CompoundExpression i.e. ...
xzczd's user avatar
  • 66.7k
1 vote
1 answer
131 views

How to freeze and test a list of programs?

I have a list of problems for my students and I'm trying to use the new v10 feature TestResults like so: ...
M.R.'s user avatar
  • 31.5k
12 votes
1 answer
389 views

What's the goal of the Macros` package?

I see in the Mathematica install a Macros` package. In windows the paths is for example: ...
faysou's user avatar
  • 11k
13 votes
2 answers
1k views

How to programmatically covert a function-like rule to a pure function?

Is there a programmatic way to convert a Mathematica rule that acts like a typical mathematical function of one variable into the equivalent pure function? For example, the code should convert ...
ibeatty's user avatar
  • 2,543
16 votes
3 answers
799 views

How would I return a random Mathematica command?

I'm doing some metaprogramming. How would I make a Mathematica function that returns a random Mathematica command? Is there a list of command names that I could use ...
Alexander Gruber's user avatar
12 votes
1 answer
563 views

A smarter nested With?

I often find myself writing code that looks a bit like this: ...
Teake Nutma's user avatar
  • 5,981
6 votes
2 answers
423 views

How to write complex function definitions at run time?

(Posting as a self-answered question to share a technique I discovered) In a certain project, I have many function definitions in which all parameters should be optional and come with default values. ...
Reb.Cabin's user avatar
  • 8,661
5 votes
1 answer
494 views

How to use Mathematica for traditional programming

I am new to the structure of Mathematica. I understand that in the notebooks, one enters the symbolic expressions and evaluates them. But I need to generate some simulated data and do that a few ...
Hirek's user avatar
  • 483
12 votes
3 answers
442 views

Find all heads a function is defined on

So say I've got a function g defined as follows: g[x_Integer] := x + 1 g[s_String] := s <> "!!!" How would I write a ...
gallabytes's user avatar
27 votes
1 answer
750 views

Can code optimize code?

Are there functions, programs or packages that deal with optimizing mathematica code i.e. re-writing ineffecive or convoluted code. -This question has haunted me since I joined SE. The background for ...
MathLind's user avatar
  • 1,697
19 votes
2 answers
311 views

How to find out what files are touched when Get is called?

Is there a way to capture all package files touched whenever a package is called via Get, Needs, ...
István Zachar's user avatar
4 votes
1 answer
409 views

Nested NDSolve with WhenEvent: setting up new equations and discarding old ones

I am checking a complex situation where a function x1[t1] only exit in a finite time span, and after this a new function x2[t2] ...
saturasl's user avatar
  • 1,429
1 vote
1 answer
386 views

using a Mathematica function to define a new function

I'd like to define a function of three variables which produces a new, named function of a single variable, where this final variable is not a member of the first three. So I'd like something where I ...
user1451632's user avatar
9 votes
8 answers
3k views

Better code to find Narcissistic number

My code finding Narcissistic numbers is not that slow, but it's not in functional style and lacks flexibility: if $n \neq 7$, I have to rewrite my code. Could you give some good advice? ...
expression's user avatar
  • 5,652
1 vote
1 answer
208 views

Simple Macro or Meta Programming for Data Analysis [duplicate]

I have a large number of data variables that are represented in MMA as symbols set to lists. For example: x = {1, 2, 3}; y = {4, 5, 6}; I want to be able to ...
pjc42's user avatar
  • 733
43 votes
4 answers
3k views

Compiling more functions that don't call MainEvaluate

I would like to use Compile with functions defined outside Compile. For example if I have the two basic functions ...
faysou's user avatar
  • 11k