Skip to main content
replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable tablemy implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time). Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

EDIT

Per request, a simple abortable Map (which only maps on the first level):

abortableMap[f_, expr_] := Module[{sowTag}, Head[expr] @@ If[# === {}, #, First@#] &[ Last[Reap[ CheckAbort[Do[Sow[f[part], sowTag], {part, List @@ expr}],Null]]]]] 

The usage is, for example:

abortableMap[(Pause[0.1*#];f[#])&,Range[10]] (* {f[1],f[2],f[3]} *) 

(again, I aborted manually soon after it started to compute).

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time). Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

EDIT

Per request, a simple abortable Map (which only maps on the first level):

abortableMap[f_, expr_] := Module[{sowTag}, Head[expr] @@ If[# === {}, #, First@#] &[ Last[Reap[ CheckAbort[Do[Sow[f[part], sowTag], {part, List @@ expr}],Null]]]]] 

The usage is, for example:

abortableMap[(Pause[0.1*#];f[#])&,Range[10]] (* {f[1],f[2],f[3]} *) 

(again, I aborted manually soon after it started to compute).

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time). Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

EDIT

Per request, a simple abortable Map (which only maps on the first level):

abortableMap[f_, expr_] := Module[{sowTag}, Head[expr] @@ If[# === {}, #, First@#] &[ Last[Reap[ CheckAbort[Do[Sow[f[part], sowTag], {part, List @@ expr}],Null]]]]] 

The usage is, for example:

abortableMap[(Pause[0.1*#];f[#])&,Range[10]] (* {f[1],f[2],f[3]} *) 

(again, I aborted manually soon after it started to compute).

Added a simple abortable Map
Source Link
Leonid Shifrin
  • 115.8k
  • 16
  • 341
  • 435

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time). Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

EDIT

Per request, a simple abortable Map (which only maps on the first level):

abortableMap[f_, expr_] := Module[{sowTag}, Head[expr] @@ If[# === {}, #, First@#] &[ Last[Reap[ CheckAbort[Do[Sow[f[part], sowTag], {part, List @@ expr}],Null]]]]] 

The usage is, for example:

abortableMap[(Pause[0.1*#];f[#])&,Range[10]] (* {f[1],f[2],f[3]} *) 

(again, I aborted manually soon after it started to compute).

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time). Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time). Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

EDIT

Per request, a simple abortable Map (which only maps on the first level):

abortableMap[f_, expr_] := Module[{sowTag}, Head[expr] @@ If[# === {}, #, First@#] &[ Last[Reap[ CheckAbort[Do[Sow[f[part], sowTag], {part, List @@ expr}],Null]]]]] 

The usage is, for example:

abortableMap[(Pause[0.1*#];f[#])&,Range[10]] (* {f[1],f[2],f[3]} *) 

(again, I aborted manually soon after it started to compute).

removed redundant statement
Source Link
rcollyer
  • 34.3k
  • 7
  • 95
  • 198

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable table my implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time).Generally Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable table my implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time).Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

Since I was corrected and this is indeed not an exact duplicate of the parallel computations question, I will reproduce here my implementation of abortable table, to have it here on SE:

ClearAll[abortableTable]; SetAttributes[abortableTable, HoldAll]; abortableTable[expr_, iter : {_Symbol, __} ..] := Module[{indices, indexedRes, sowTag, depth = Length[Hold[iter]] - 1}, Hold[iter] /. {sym_Symbol, __} :> sym /. Hold[syms__] :> (indices := {syms}); indexedRes = Replace[#, {x_} :> x] &@ Last@Reap[ CheckAbort[Do[Sow[{expr, indices}, sowTag], iter], Null],sowTag]; AbortProtect[ SplitBy[indexedRes, Array[Function[x, #[[2, x]] &], {depth}]][[##,1]] & @@ Table[All, {depth + 1}] ]]; 

the usage is

tt=abortableTable[Pause[0.1*i];i,{i,50}] (* {1,2,3,4,5,6,7,8} *) 

(I aborted the computation after some time). Generally, abortableTable accepts the same iterator syntax as Table, and can work with multiple dimensions. The details on how this works can be found in the linked discussion.

Source Link
Leonid Shifrin
  • 115.8k
  • 16
  • 341
  • 435
Loading