Mathematica has a number of built-in functions that generate lists, such as Permutations. Is there is universal way to generate only portions of these lists at a time?

My specific situation: I can find the solution to a particular problem by running a loop through all outcomes of Permutations[Range[13],{10}]. However, the size of that list is big enough that it brings my machine to a crawl.

Since Mathematica generates that list algorithmically (somehow), is it possible instead to have it generate, say, only the first 10,000 elements of the list, then the next 10,000, etc., reducing the amount that is stored in memory?

Permutations[Range[13],{10}][[Range[10000]]] would, in my mind, first generate the entire list then pull off the first 10,000 elements, not what I want to do.

Ultimately, I don't care about the Permutations function specifically. Rather, my question is as stated in the second sentence: is there a way to only generate portions of a list?

  • $\begingroup$ Lazy evaluation, then? These two are related; you might want to peruse the code in Combinatorica to see how to generate things a few at a time. $\endgroup$
    – J. M.'s torpor
    Dec 22 '16 at 12:10
  • $\begingroup$ @J.M. Was unfamiliar with the concept of a lazy list. It seems that 1) such a construct is possible, 2) it isn't naturally built into Mathematica, and 3) (sigh) I don't understand how others create their solutions. I'll do some reading to see what I can figure out. Thanks. $\endgroup$
    – GregH
    Dec 22 '16 at 13:06
  • $\begingroup$ I strongly doubt there is a universal method for computing subsets of the elements of a long list since there are so many fundamental methods for creating lists (iteration, recursion, sorting, ...). You might parallelize your code and read from the output of one thread, but that will work in only certain cases, not others. Imagine Sort; No way to pick off the first $n$ elements unless you use certain (inefficient) sorting routines. $\endgroup$ Dec 22 '16 at 21:58

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.