Is it possible to have an expression x such that its elements are only evaluated when they are individually accessed, for example when evaluating statements like x[[1]] or f/@x? The basic idea is that x is a list of Get commands, each of which loads a large expression (gigabytes), so that x can be terabytes in size yet it remains fairly manipulable within Mathematica.
What I thought would work was something like:
x={Unevaluated[Get[...]],Unevaluated[Get[...]],...}
The problem with that however is that Unevaluated doesn't get stripped when I need it to, i.e. when calling something like f[x[[1]]], because of Mathematica's evaluator semantics. The only workable alternative is to use Hold instead of Unevaluated, but that requires that I manually call ReleaseHold every time, which is ugly. I was hoping for something entirely transparent.
Partand related functions, and thus are not general in the sense that I have to know a priori which functions will operate on the expression? If that is the case then it doesn't help me, as the purpose of my question was to have a solution that would work transparently. I may have misunderstood your solutions however. – Mohammed AlQuraishi Jul 02 '13 at 22:42Sort, etc). Making lazy streams an integral and transparent part of the language is no small task. – Leonid Shifrin Jul 02 '13 at 22:51HoldorHoldCompleteas your container, instead ofList:x= Hold[Get[...],...,Get[...]]. Perhaps this is as transparent as it gets, but I am not really sure what this buys you that can not be totally covered by overloading functions such asPart. – Leonid Shifrin Jul 02 '13 at 22:55Hold/ReleaseHold? – Mr.Wizard Jul 03 '13 at 20:37x={Hold[Get[...]],Hold[Get[...]],...}and then when mapping a function or extracting a part I just make sure to wrap it withReleaseHold. Nothing fancy.Coincidentally, I noticed that I can almost solve the problem using
– Mohammed AlQuraishi Jul 04 '13 at 01:30Wrapper /: h_[a___,Wrapper[g_],b___] /; h=!=List := h[a,g,b]which has slightly nicer stripping behavior thanUnevaluated, alas it doesn't really work. It works when say mapping a function, but not when extracting a part, becauseParttakes the whole list as an argument and so doesn't stripWrapper.Partdo you mean when extracting a sub-part of the Wrapped object? – Mr.Wizard Jul 04 '13 at 07:03Wrappergets stripped (and thus whatever is inside it gets evaluated, presumably aGetexpression) whenever it is extracted as a single element. Having said that, for all the use cases I can think of, it actually does what I want. For examplef[{a, Wrapper[b], c}[[2]]]returnsf[b], andx = {a, Wrapper[b], c}[[2]]assignsbtox, so maybe I'm being picky. The only time it doesn't work is with{a, Wrapper[b], c}[[2]], which returnsWrapper[b]instead of justb. – Mohammed AlQuraishi Jul 04 '13 at 17:35Wrapper[x_] /; StackInhibit[Quiet@Stack[][[-2]] =!= List] := x; _Wrapper /; Update[] := NullUpValue by DownValue) – Rojo Jul 05 '13 at 03:21