| 1 |
// Shred.ancestor() returns the top-level shred that is directly or |
| 2 |
// indirectly a parent shred of the calling shred; useful for getting |
| 3 |
// information relevant to top-level shreds; related: parent.ck |
| 4 |
|
| 5 |
// test me |
| 6 |
<<< "the top-level shred's ID is:", me.id() >>>; |
| 7 |
// my ancestor |
| 8 |
<<< "the top-level shred's ancestor ID is:", me.ancestor().id(), "\n" >>>; |
| 9 |
|
| 10 |
// recursive function to test finding ancestor from different |
| 11 |
// "generations" of shreds |
| 12 |
fun void findTheAncestor( int generation ) |
| 13 |
{ |
| 14 |
// stop recursing |
| 15 |
if( generation <= 0 ) return; |
| 16 |
|
| 17 |
// calling shred |
| 18 |
<<< "the sporked shred's ID is:", me.id() >>>; |
| 19 |
// this will always be the same as the top-level shred id |
| 20 |
<<< "the sporked shred's ancestor ID is:", me.ancestor().id(), "\n" >>>; |
| 21 |
|
| 22 |
// spork a child shred |
| 23 |
spork ~ findTheAncestor( generation-1 ); |
| 24 |
// wait a bit to give child shred a chance to run |
| 25 |
1::samp => now; |
| 26 |
} |
| 27 |
|
| 28 |
// recursively spork function this 10 times; me.ancestor() should always |
| 29 |
// point to the top-level shred |
| 30 |
spork ~ findTheAncestor(10); |
| 31 |
|
| 32 |
// wait to give children shreds a chance to run |
| 33 |
1::samp => now; |