I have two ingredients here:
- a big dataset contained in a list, with ~ 20M values.
- a function that takes as each element of the list as input and yields True or False
I want to save somewhere the elements of the list that yielded True. Usually I would do something like that:
list = Range[10];
fun = PrimeQ;
Reap[
If[fun[#]&,Sow[#]]& /@ list
]
This works perfectly, except the fact that we have to wait the end of the computation in order to be able to see all the results. When dealing with such huge lists sometimes waiting for the computation to finish is not an option.
This is what I am doing now to split the computation is more chunks,
SaveResult[list_, partitions_, fun_] := Table[
Print["doing iteration ", i];
If[
#[[2]] =!= {},
#[[2]] >>> NotebookDirectory[] <> "/Data/" <> ToString[i]
] &@
Reap[(If[fun[#], Sow[#]]) & /@ Partition[list, partitions][[i]]],
{i, 1, 9}
];
My question is: is there a better way to saving partial results or dealing with huge datasets?
Select
instead ofReap[If[..,Sow[.. ]
. $\endgroup$Partition
makes a copy of the whole thing. You may be better off to dolist[[1+partitionlength*(i-1);;partitionlength*i]]
$\endgroup$