3 added 2 characters in body
source | link

Don't know where this should fit. Playing around, this could be a more general approach to the traversal problem. It's probably not best for those traversals that can be done in other ways. Specifically, it has to create the list of indices all at once so it's not memory efficient as Scan

Module[{tag}, 
 generalScan[fun_, expr_, sortingFun_: RandomSample] := 
  Extract[Unevaluated@expr, If[# === {}, fun[expr]]; #, fun] &~Scan~
   sortingFun[
    Reap[MapIndexed[tag, Hold[expr], Infinity, Heads -> True] /. 
       tag[_, {1, in___}] /; Sow[{in}, tag] :> Null, tag][[-1, 1]]]]

This takes a third argument that makes you sort the list of indices to traverse. Defaults to RandomSample (hihihi)

expr = {{{1, {2}}}, {3, {4}}};

(* Random *)
generalScan[Print, expr]

(* BredthBreadth-first *)
generalScan[Print, expr, Sort]

(* BredthBreadth-first right-left *)
generalScan[Print, expr, SortBy[#, Minus] &]

(* Depth-first preorder *)
generalScan[Print, expr, Part[#, Ordering[PadRight[#]]] &]

...

It could be used for other purposes such as traversing a part of a tree. This would only traverse the leftmost branch

generalScan[Print, expr, Cases[#, {1 ..}] &]

Don't know where this should fit. Playing around, this could be a more general approach to the traversal problem. It's probably not best for those traversals that can be done in other ways. Specifically, it has to create the list of indices all at once so it's not memory efficient as Scan

Module[{tag}, 
 generalScan[fun_, expr_, sortingFun_: RandomSample] := 
  Extract[Unevaluated@expr, If[# === {}, fun[expr]]; #, fun] &~Scan~
   sortingFun[
    Reap[MapIndexed[tag, Hold[expr], Infinity, Heads -> True] /. 
       tag[_, {1, in___}] /; Sow[{in}, tag] :> Null, tag][[-1, 1]]]]

This takes a third argument that makes you sort the list of indices to traverse. Defaults to RandomSample (hihihi)

expr = {{{1, {2}}}, {3, {4}}};

(* Random *)
generalScan[Print, expr]

(* Bredth-first *)
generalScan[Print, expr, Sort]

(* Bredth-first right-left *)
generalScan[Print, expr, SortBy[#, Minus] &]

(* Depth-first preorder *)
generalScan[Print, expr, Part[#, Ordering[PadRight[#]]] &]

...

It could be used for other purposes such as traversing a part of a tree. This would only traverse the leftmost branch

generalScan[Print, expr, Cases[#, {1 ..}] &]

Don't know where this should fit. Playing around, this could be a more general approach to the traversal problem. It's probably not best for those traversals that can be done in other ways. Specifically, it has to create the list of indices all at once so it's not memory efficient as Scan

Module[{tag}, 
 generalScan[fun_, expr_, sortingFun_: RandomSample] := 
  Extract[Unevaluated@expr, If[# === {}, fun[expr]]; #, fun] &~Scan~
   sortingFun[
    Reap[MapIndexed[tag, Hold[expr], Infinity, Heads -> True] /. 
       tag[_, {1, in___}] /; Sow[{in}, tag] :> Null, tag][[-1, 1]]]]

This takes a third argument that makes you sort the list of indices to traverse. Defaults to RandomSample (hihihi)

expr = {{{1, {2}}}, {3, {4}}};

(* Random *)
generalScan[Print, expr]

(* Breadth-first *)
generalScan[Print, expr, Sort]

(* Breadth-first right-left *)
generalScan[Print, expr, SortBy[#, Minus] &]

(* Depth-first preorder *)
generalScan[Print, expr, Part[#, Ordering[PadRight[#]]] &]

...

It could be used for other purposes such as traversing a part of a tree. This would only traverse the leftmost branch

generalScan[Print, expr, Cases[#, {1 ..}] &]
2 added 188 characters in body
source | link

Don't know where this should fit. Playing around, this could be a more general approach to the traversal problem. It's probably not best for those traversals that can be done in other ways. Specifically, it has to create the list of indices all at once so it's not memory efficient as Scan

Module[{tag}, 
 generalScan[fun_, expr_, sortingFun_: RandomSample] := 
  Extract[Hold[expr]Extract[Unevaluated@expr, If[# === {}, fun[expr]]; #, fun] &~Scan~
   sortingFun[
    Rest@Reap[
       MapIndexed[tagReap[MapIndexed[tag, Hold[expr], Infinity, Heads -> True] /. 
        tag[_, in_]{1, in___}] /; Sow[inSow[{in}, tag] :> Null, tag][[-1, 1]]]
 ]1]]]]

This takes a third argument that makes you sort the list of indices to traverse. Defaults to RandomSample (hihihi)

expr = {{{1, {2}}}, {3, {4}}};

(* Random *)
generalScan[Print, expr]

(* Bredth-first *)
generalScan[Print, expr, Sort]

(* Bredth-first right-left *)
generalScan[Print, expr, SortBy[#, Minus] &]

(* Depth-first preorder *)
generalScan[Print, expr, Part[#, Ordering[PadRight[#]]] &]

...

It could be used for other purposes such as traversing a part of a tree. This would only traverse the leftmost branch

generalScan[Print, expr, Cases[#, {1 ..}] &]

Don't know where this should fit. Playing around, this could be a more general approach to the traversal problem. It's probably not best for those traversals that can be done in other ways. Specifically, it has to create the list of indices all at once so it's not memory efficient as Scan

Module[{tag},
 generalScan[fun_, expr_, sortingFun_: RandomSample] := 
  Extract[Hold[expr], #, fun] &~Scan~
   sortingFun[
    Rest@Reap[
       MapIndexed[tag, Hold[expr], Infinity, Heads -> True] /. 
        tag[_, in_] /; Sow[in, tag] :> Null, tag][[-1, 1]]]
 ]

This takes a third argument that makes you sort the list of indices to traverse. Defaults to RandomSample (hihihi)

expr = {{{1, {2}}}, {3, {4}}};

(* Random *)
generalScan[Print, expr]

(* Bredth-first *)
generalScan[Print, expr, Sort]

(* Bredth-first right-left *)
generalScan[Print, expr, SortBy[#, Minus] &]

(* Depth-first preorder *)
generalScan[Print, expr, Part[#, Ordering[PadRight[#]]] &]

...

Don't know where this should fit. Playing around, this could be a more general approach to the traversal problem. It's probably not best for those traversals that can be done in other ways. Specifically, it has to create the list of indices all at once so it's not memory efficient as Scan

Module[{tag}, 
 generalScan[fun_, expr_, sortingFun_: RandomSample] := 
  Extract[Unevaluated@expr, If[# === {}, fun[expr]]; #, fun] &~Scan~
   sortingFun[
    Reap[MapIndexed[tag, Hold[expr], Infinity, Heads -> True] /. 
       tag[_, {1, in___}] /; Sow[{in}, tag] :> Null, tag][[-1, 1]]]]

This takes a third argument that makes you sort the list of indices to traverse. Defaults to RandomSample (hihihi)

expr = {{{1, {2}}}, {3, {4}}};

(* Random *)
generalScan[Print, expr]

(* Bredth-first *)
generalScan[Print, expr, Sort]

(* Bredth-first right-left *)
generalScan[Print, expr, SortBy[#, Minus] &]

(* Depth-first preorder *)
generalScan[Print, expr, Part[#, Ordering[PadRight[#]]] &]

...

It could be used for other purposes such as traversing a part of a tree. This would only traverse the leftmost branch

generalScan[Print, expr, Cases[#, {1 ..}] &]
1
source | link

Don't know where this should fit. Playing around, this could be a more general approach to the traversal problem. It's probably not best for those traversals that can be done in other ways. Specifically, it has to create the list of indices all at once so it's not memory efficient as Scan

Module[{tag},
 generalScan[fun_, expr_, sortingFun_: RandomSample] := 
  Extract[Hold[expr], #, fun] &~Scan~
   sortingFun[
    Rest@Reap[
       MapIndexed[tag, Hold[expr], Infinity, Heads -> True] /. 
        tag[_, in_] /; Sow[in, tag] :> Null, tag][[-1, 1]]]
 ]

This takes a third argument that makes you sort the list of indices to traverse. Defaults to RandomSample (hihihi)

expr = {{{1, {2}}}, {3, {4}}};

(* Random *)
generalScan[Print, expr]

(* Bredth-first *)
generalScan[Print, expr, Sort]

(* Bredth-first right-left *)
generalScan[Print, expr, SortBy[#, Minus] &]

(* Depth-first preorder *)
generalScan[Print, expr, Part[#, Ordering[PadRight[#]]] &]

...