Group Abstract Group Abstract

Message Boards Message Boards

Manipulate an array calculated outside Manipulate?

Posted 6 years ago
Attachments:
POSTED BY: Werner Geiger
8 Replies
Posted 6 years ago
POSTED BY: Mike Besso
Posted 6 years ago

Werner:

You are welcome. Thanks for the great question. I'm still learning Manipulate. Working through this example taught me a great deal.

And thank you for explaining why you are using Print. And I agree, that Print does provide a solution to multiple outputs.

I try to avoid Print for anything but debugging. When I need multiple outputs, I usually return either an association, list, grid, column, or row. In my opinion, handling multiple outputs this way helps me solve some other challenges:

  • Testing. I believe in Test Driven Development as much as possible. By returning values instead of printing them, I can utilize Wolframs unit test framework.

  • Reuse. By returning an association, list, etc., I can reuse the function in many places.

  • Pipelines. More and more I am getting into the pipeline paradigm. For me, a pipeline is a set of functions that each take an association that serves as a payload. I then use composition to pass the payload through all of the functions. This helps me keep focus when coding by separating presentation from data prep from analytics.

Have a great summer

POSTED BY: Mike Besso
Posted 6 years ago
POSTED BY: Werner Geiger
Posted 6 years ago

I forgot to mention: For debugging I use conditional Echo only, never Print.

BTW: WL/Mathematica is a wonderful language, but I think it's the only one I have used during the last 30 or 40 years which does not have a debugger. This is totally incomprehensible for me and really stone-aged. (I know of course that there is something that calls itself a "debugger" but this is useless and obviously abandoned. And I know that higher-level software developing environments (like Eclipse and some others) can be used, at least with professional WL-licences.)

POSTED BY: Werner Geiger
Posted 6 years ago

The Wolfram language supports many (perhaps too many) different programming paradigms and patterns. This often means that there are an infinite number of good solutions to every challenge.

In cases like this, when we do not have to use a global variable, I usually prefer to keep things local by passing them in with something like:

myApp[graphs_List] := Manipulate[
   graphs[[i]],
   {
    {i, 1, "Step"},
    1,
    Length[graphs],
    1,
    Appearance -> "Open"
    },
   LabelStyle -> Directive[Black, Bold, 11],
   ControlPlacement -> Bottom
   ];

myGraphs = {1, 20, 30, 40};
myApp[myGraphs]

I believe this solution:

  • Keeps things simple
  • Makes it clear that visualization never updates the input data
  • Allows the visualization to be reused

Of course, there are other simple solutions. And sometimes, keeping your data global is the best solution. In this case, I got the following to work:

graphs = {1, 20, 30, 40};

Manipulate[
 graphs[[i]],
 {
  {i, 1, "Step"},
  1,
  Length[graphs],
  1,
  Appearance -> "Open"
  },
 LabelStyle -> Directive[Black, Bold, 11],
 ControlPlacement -> Bottom
 ]

Also, I am curious as to why you are printing the Manipulate instead of just returning it?

POSTED BY: Mike Besso
Posted 6 years ago

Thanks very much, Mike. Your solution, packing the Manipulate into a function (myApp in your example) is exactly what does the job.

Concerning your question why I Print the Manipulate: The Manipulate-output is just part of some more output (a grid actually).

POSTED BY: Werner Geiger
Posted 6 years ago

I think that using "With" rather than "Block" to localize your "graphs" variable in Case 1 will give the desired result. (Of course, it's possible that I don't understand what your desired result is...)

POSTED BY: Richard Hewens
Posted 6 years ago

"With" implements local constants, i.e. read-only lexical variables. In my case "graphs" is not a constant but calculated within the Block/Module before being displayed. Hence this does not help.

POSTED BY: Werner Geiger
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard