Skip to main content
deleted 91 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Due to confusion of terminology on my part I asked the wrong question before.
Since it has already received other answers I shall not edit it, but instead ask the question I meant to in the first place.

Mathematica provides functions that perform a depth-first postorder traversal, or which use such a traversal, including: Scan, Count, Cases, Replace, and Position. It is also the standard evaluation order therefore functions Mapped (Map, MapAll) will evaluate in a depth-first-postorder.

It is quite direct to do this:

expr = {{1, {2, 3}}, {4, 5}}; Scan[Print, expr, {0, -1}] 

1

2

3

{2,3}

{1,{2,3}}

4

5

{4,5}

{{1,{2,3}},{4,5}}

It is not as obvious how to do a depth-first preorder scan. (Simply storing then reordering the output is not adequate as it doesn't change the order in which expressions are visited.)

Scan has the property that it does not build an output expression the way that e.g. Map does, and conserves memory.

How can one do a Scan-type operation in depth-first preorder?


Related:

Due to confusion of terminology on my part I asked the wrong question before.
Since it has already received other answers I shall not edit it, but instead ask the question I meant to in the first place.

Mathematica provides functions that perform a depth-first postorder traversal, or which use such a traversal, including: Scan, Count, Cases, Replace, and Position. It is also the standard evaluation order therefore functions Mapped (Map, MapAll) will evaluate in a depth-first-postorder.

It is quite direct to do this:

expr = {{1, {2, 3}}, {4, 5}}; Scan[Print, expr, {0, -1}] 

1

2

3

{2,3}

{1,{2,3}}

4

5

{4,5}

{{1,{2,3}},{4,5}}

It is not as obvious how to do a depth-first preorder scan. (Simply storing then reordering the output is not adequate as it doesn't change the order in which expressions are visited.)

Scan has the property that it does not build an output expression the way that e.g. Map does, and conserves memory.

How can one do a Scan-type operation in depth-first preorder?

Mathematica provides functions that perform a depth-first postorder traversal, or which use such a traversal, including: Scan, Count, Cases, Replace, and Position. It is also the standard evaluation order therefore functions Mapped (Map, MapAll) will evaluate in a depth-first-postorder.

It is quite direct to do this:

expr = {{1, {2, 3}}, {4, 5}}; Scan[Print, expr, {0, -1}] 

1

2

3

{2,3}

{1,{2,3}}

4

5

{4,5}

{{1,{2,3}},{4,5}}

It is not as obvious how to do a depth-first preorder scan. (Simply storing then reordering the output is not adequate as it doesn't change the order in which expressions are visited.)

Scan has the property that it does not build an output expression the way that e.g. Map does, and conserves memory.

How can one do a Scan-type operation in depth-first preorder?


Related:

replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

Due to confusion of terminology on my part I asked the wrong question before.before.
Since it has already received other answers I shall not edit it, but instead ask the question I meant to in the first place.

Mathematica provides functions that perform a depth-first postorder traversal, or which use such a traversal, including: Scan, Count, Cases, Replace, and Position. It is also the standard evaluation order therefore functions Mapped (Map, MapAll) will evaluate in a depth-first-postorder.

It is quite direct to do this:

expr = {{1, {2, 3}}, {4, 5}}; Scan[Print, expr, {0, -1}] 

1

2

3

{2,3}

{1,{2,3}}

4

5

{4,5}

{{1,{2,3}},{4,5}}

It is not as obvious how to do a depth-first preorder scan. (Simply storing then reordering the output is not adequate as it doesn't change the order in which expressions are visited.)

Scan has the property that it does not build an output expression the way that e.g. Map does, and conserves memory.

How can one do a Scan-type operation in depth-first preorder?

Due to confusion of terminology on my part I asked the wrong question before.
Since it has already received other answers I shall not edit it, but instead ask the question I meant to in the first place.

Mathematica provides functions that perform a depth-first postorder traversal, or which use such a traversal, including: Scan, Count, Cases, Replace, and Position. It is also the standard evaluation order therefore functions Mapped (Map, MapAll) will evaluate in a depth-first-postorder.

It is quite direct to do this:

expr = {{1, {2, 3}}, {4, 5}}; Scan[Print, expr, {0, -1}] 

1

2

3

{2,3}

{1,{2,3}}

4

5

{4,5}

{{1,{2,3}},{4,5}}

It is not as obvious how to do a depth-first preorder scan. (Simply storing then reordering the output is not adequate as it doesn't change the order in which expressions are visited.)

Scan has the property that it does not build an output expression the way that e.g. Map does, and conserves memory.

How can one do a Scan-type operation in depth-first preorder?

Due to confusion of terminology on my part I asked the wrong question before.
Since it has already received other answers I shall not edit it, but instead ask the question I meant to in the first place.

Mathematica provides functions that perform a depth-first postorder traversal, or which use such a traversal, including: Scan, Count, Cases, Replace, and Position. It is also the standard evaluation order therefore functions Mapped (Map, MapAll) will evaluate in a depth-first-postorder.

It is quite direct to do this:

expr = {{1, {2, 3}}, {4, 5}}; Scan[Print, expr, {0, -1}] 

1

2

3

{2,3}

{1,{2,3}}

4

5

{4,5}

{{1,{2,3}},{4,5}}

It is not as obvious how to do a depth-first preorder scan. (Simply storing then reordering the output is not adequate as it doesn't change the order in which expressions are visited.)

Scan has the property that it does not build an output expression the way that e.g. Map does, and conserves memory.

How can one do a Scan-type operation in depth-first preorder?

edited tags
Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
added 58 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
added 77 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
edited title
Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
Tweeted twitter.com/#!/StackMma/status/233338864125747200
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading