A class of functions that I would like to suggest is not a new one, but a extension of Scan function. Here are the function:
ScanOuter
ScanThread
ScanInner
ScanIndexed
Each one would have the same syntax use of their equivalent (Outer, Thread, …), but with the same behavior as Scan, with much less memory consumption, due the fact that Scan don’t have to “Save” all it results. Other advantage would be to use of Return inside, that opens the possibility to break computation at some moment.As an application example, sometimes I need to apply a function into all combination of to big groups, to take just some specific itens.
Let’s simulate the lists using a RandomInteger, and Outer to simulate ScanOuter.
list1 = RandomInteger[1000, {10000, 2}];
list2 = RandomInteger[1000, {10000, 2}];
Reap[Outer[If[N@Norm@{##} > 1700, Sow[{#1, #2}]] &, list1, list2, 1]][[2]]
I believe that this kind of calculation would use much lass memory with the equivalent Scan function, that do not need to store each result.
The calculation above simple don’t run in my computer (in less then 5 minutes), and should be almost instantly in ScanOuter.
Another nice application is ScanIndexed.
Imagine you want to know the position of the element, when it’s > 0.8 in this list.
list={0.0314503,0.168573,0.291282,0.319608,0.426414,0.430967,0.626601,0.897033,0.923966,0.944242}
pos=ScanIndexed[If[# > 0.8, Return[First@#2]] &, list]
To to things like this today, you have to create a index variable, or use a While loop, what makes me very incomfortable inside WL paradigm.
ScanIndexed could some this in a more elegant way.