Questions on how to write code in a better or different style, using Mathematica's capabilities for coding in multiple styles.

learn more… | top users | synonyms

54
votes
3answers
4k views

Alternatives to procedural loops and iterating over lists in Mathematica

While there are some cases where a For loop might be reasonable, it's a general mantra – one I subscribe to myself – that "if you are using a ...
52
votes
7answers
4k views

Struct equivalent in Mathematica?

I really miss having something like a struct in Mathematica. I know of (and regularly use) a couple of programming techniques which feel like a ...
5
votes
1answer
190 views

Alternative to overloading Set

In the ideas shared in my answer to this post Struct equivalent in Mathematica? at the end I propose a solution that changes the way Set works in a particular case that can be expressed in plain words ...
22
votes
4answers
399 views

Placement of Condition /; expressions

It is my practice to place Condition expressions on the left side of := and :> in almost ...
13
votes
4answers
293 views

Short syntax for accessing System`Utilities`HashTableAdd and System`Utilities`HashTableGet

I have a need for a struct analogue in Mathematica. After reading this post I came to the conclusion that System`Utilities`HashTable is one of the best options. But ...
19
votes
2answers
693 views

Extracting values from nested rules in JSON data

I have been using Mathematica to analyse some data from the StackExchange API. It is conveniently available in JSON form, which Mathematica interprets as replacement rules. However, some of the rules ...
34
votes
7answers
976 views

Can one identify the design patterns of Mathematica?

... or are they unnecessary in such a high-level language? I've been thinking about programming style, coding standards and the like quite a bit lately, the result of my current work on a mixed ...
11
votes
2answers
221 views

Notebook formatting - easier descriptions for equations and results?

When I do computations in mathematica, I generally try to make it readable for myself-in-the-future. This can be a cumbersome task in mathematica, where it often requires me to switch betwee a text ...
11
votes
2answers
563 views

Speeding up random walk for many particles

I am trying to speed up this code for many particles to take a random walk. I'm not sure why it is so slow for such a simple task. I got a few hints from colleagues to reduce the precision of the ...
6
votes
4answers
405 views

Alternatives to While Loops?

I am using Mathematica to run a probabilistic simulation. Essentially, I have a list of members of a population (they only have one, numerical, attribute, so it's just implemented as a list of ...
15
votes
1answer
222 views

Are there advantages to using additional arguments of common functions rather than alternative ways of calculating with lists?

(Apologies for the long question title.) One of the interesting, if sometimes confusing, things about Mathematica is that there is always more than one way to do things. Even intermediate users can ...
11
votes
2answers
310 views

What's the most “functional” way to do Cholesky decomposition?

I can do Cholesky in a procedural style, such as: ...
9
votes
5answers
277 views

An elegant way to plot a numeric function that returns a list, and have each element in a different color

I have a function that takes a numeric argument and returns a list of numbers. I want to plot each element of the list in a different color. If I use this command, ...
5
votes
1answer
128 views

How to find the name of the current function

I would like to know the name of the current function from within that function. For example, consider the following code ...
8
votes
3answers
316 views

Can this code be written in a more functional style

Referencing this question, I wonder if the following code can be written more concisely using a functional style, i.e. without For loops or ...