I understand that to create objects with custom behavior with respect to existing code one uses upvalues. However, I'm at loss when it comes to guess what interface is expected from objects fed to built-in functions.

As a practical example, I would like to create a custom object DiskList that doesn't store its values in memory but dynamically reads them from a file. It is easy enough to write an upvalue for it so that the expression mydisklist[[3]] works as expected, but how do I make such object able to be passed to Map?

  • $\begingroup$ Why would you assume that Map uses iterators, in the first place? Help says it operates on the tree model of expressions, and makes a plenty of references to relevant concepts like levelspec and depth. So unless your "custom object" is a tree (and therefore nothing new to MMA), I would say Map is not the command for you. $\endgroup$ – The Vee Mar 18 '16 at 11:18
  • $\begingroup$ @TheVee I'm not assuming that, it's the point of the question. What does Map expect from and object? Does it use Length, or does it use some compiled magic that I'll never be able to fool with an upvalue? and is there a way to "enumerate" these requirements rather than going by trial and error? $\endgroup$ – Lorenzo Pistone Mar 18 '16 at 11:21
  • $\begingroup$ Well, Mathematica's only universal "interface" is a tree of objects. Map operates on this tree, and so do down/up/own/...value replacements. But the latter is an operation independent from the former. Map walks the actual structure (replacing (Map, f, (X, y, z)) by (X, (f, y), (f, z))), it does not ask for parts one-by-one. In fact, your object would not have parts, it just mimics being like that by transforming (Part, (DiskList, n)) (and presumably (Length, DiskList)) to something. But as pointed out above, you can write your own version of Map that would be based on For or Table. $\endgroup$ – The Vee Mar 18 '16 at 11:28
  • 1
    $\begingroup$ Or, since Map would return the whole list anyway, you would be just as well of defining another set of upvalues that would first cache all the contents and then pass it on as a list. It would be natural to extend Normal for this purpose. $\endgroup$ – The Vee Mar 18 '16 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.