| 1 |
// Author: Jim Idle - [email protected] / [email protected] |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package jwz |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
) |
| 9 |
|
| 10 |
func ExampleSort() { |
| 11 |
// Emails := loadEmails() - your function to load emails into a slice |
| 12 |
// |
| 13 |
|
| 14 |
// Create a threader and thread using the slice of Threadable in the slice called Emails |
| 15 |
// |
| 16 |
threader := NewThreader() |
| 17 |
sliceRoot, err := threader.ThreadSlice(Emails) |
| 18 |
if err != nil { |
| 19 |
fmt.Printf("func ThreadSlice() error = %#v", err) |
| 20 |
} |
| 21 |
|
| 22 |
// Count with no dummies |
| 23 |
// |
| 24 |
var n, ns int |
| 25 |
Count(sliceRoot, &n) |
| 26 |
|
| 27 |
// Sort it Rodney! |
| 28 |
// |
| 29 |
x := Sort(sliceRoot, func(t1 Threadable, t2 Threadable) bool { |
| 30 |
// Sort by date, inline function |
| 31 |
// |
| 32 |
return t1.GetDate().Before(t2.GetDate()) |
| 33 |
}) |
| 34 |
|
| 35 |
Count(x, &ns) |
| 36 |
if n != ns { |
| 37 |
fmt.Printf("Count before sort: %d, Count after sort: %d\n", n, ns) |
| 38 |
} |
| 39 |
|
| 40 |
fmt.Printf("First node subject: %s\n", x.Subject()) |
| 41 |
|
| 42 |
// Output: First node subject: 2002-08-22 11:46:18 +0000 UTC : [zzzzteana] RE: Alexander |
| 43 |
} |
| 44 |
|
| 45 |
func ExampleWalk_depth() { |
| 46 |
// Emails := loadEmails() - your function to load emails into a slice |
| 47 |
// |
| 48 |
|
| 49 |
// Create a threader and thread using the slice of Threadable in the slice called Emails |
| 50 |
// |
| 51 |
threader := NewThreader() |
| 52 |
sliceRoot, err := threader.ThreadSlice(Emails) |
| 53 |
if err != nil { |
| 54 |
fmt.Printf("func ThreadSlice() error = %#v", err) |
| 55 |
} |
| 56 |
var c int |
| 57 |
_ = Walk(true, sliceRoot, func(t Threadable, u any) (bool, error) { |
| 58 |
c := u.(*int) |
| 59 |
if !t.IsDummy() { |
| 60 |
*c++ |
| 61 |
} |
| 62 |
return false, nil |
| 63 |
}, |
| 64 |
&c) |
| 65 |
|
| 66 |
fmt.Printf("Walker walked %d depth first\n", c) |
| 67 |
|
| 68 |
// Output: Walker walked 93 depth first |
| 69 |
} |
| 70 |
|
| 71 |
func ExampleWalk_breadth() { |
| 72 |
// Emails := loadEmails() - your function to load emails into a slice |
| 73 |
// |
| 74 |
|
| 75 |
// Create a threader and thread using the slice of Threadable in the slice called Emails |
| 76 |
// |
| 77 |
threader := NewThreader() |
| 78 |
sliceRoot, err := threader.ThreadSlice(Emails) |
| 79 |
if err != nil { |
| 80 |
fmt.Printf("func ThreadSlice() error = %#v", err) |
| 81 |
} |
| 82 |
var c int |
| 83 |
|
| 84 |
// Walk the tree breadth first and call our anonymous function on each Threadable |
| 85 |
// |
| 86 |
_ = Walk(false, sliceRoot, func(t Threadable, u any) (bool, error) { |
| 87 |
c := u.(*int) |
| 88 |
if !t.IsDummy() { |
| 89 |
*c++ |
| 90 |
} |
| 91 |
return false, nil |
| 92 |
}, |
| 93 |
&c) |
| 94 |
|
| 95 |
fmt.Printf("Walker walked %d depth first\n", c) |
| 96 |
|
| 97 |
// Output: Walker walked 93 depth first |
| 98 |
} |
| 99 |
|
| 100 |
type searcher struct { |
| 101 |
messageID string |
| 102 |
e Threadable |
| 103 |
} |
| 104 |
|
| 105 |
func ExampleWalk_search() { |
| 106 |
// Emails := loadEmails() - your function to load emails into a slice |
| 107 |
// |
| 108 |
|
| 109 |
// Create a threader and thread using the slice of Threadable in the slice called Emails |
| 110 |
// |
| 111 |
threader := NewThreader() |
| 112 |
sliceRoot, err := threader.ThreadSlice(Emails) |
| 113 |
if err != nil { |
| 114 |
fmt.Printf("func ThreadSlice() error = %#v", err) |
| 115 |
} |
| 116 |
|
| 117 |
// Construct our search |
| 118 |
// |
| 119 |
param := &searcher{ |
| 120 |
messageID: "<008701c24a16$e3443830$0200a8c0@JMHALL>", |
| 121 |
} |
| 122 |
|
| 123 |
// Walk the tree breadth first and call our anonymous function on each Threadable |
| 124 |
// |
| 125 |
_ = Walk(false, sliceRoot, |
| 126 |
func(t Threadable, u any) (bool, error) { |
| 127 |
params := u.(*searcher) |
| 128 |
if !t.IsDummy() { |
| 129 |
if t.MessageThreadID() == params.messageID { |
| 130 |
|
| 131 |
// We found the email we wanted, so we can stop here |
| 132 |
// |
| 133 |
params.e = t |
| 134 |
return true, nil |
| 135 |
} |
| 136 |
} |
| 137 |
return false, nil |
| 138 |
}, |
| 139 |
param) |
| 140 |
|
| 141 |
fmt.Printf("Walker found the email %s with subject %s\n", param.messageID, param.e.Subject()) |
| 142 |
|
| 143 |
// Walker found the email <008701c24a16$e3443830$0200a8c0@JMHALL> with subject 2002-08-22 20:02:45 +0000 UTC : Property rights in the 3rd World (De Soto's Mystery of Capital) |
| 144 |
} |
| 145 |
|
| 146 |
func ExampleCount() { |
| 147 |
// Emails := loadEmails() - your function to load emails into a slice |
| 148 |
// |
| 149 |
|
| 150 |
// Create a threader and thread using the slice of Threadable in the slice called Emails |
| 151 |
// |
| 152 |
threader := NewThreader() |
| 153 |
sliceRoot, err := threader.ThreadSlice(Emails) |
| 154 |
if err != nil { |
| 155 |
fmt.Printf("func ThreadSlice() error = %#v", err) |
| 156 |
return |
| 157 |
} |
| 158 |
|
| 159 |
// Find out how many non dummy Threadables are in the tree - in other words, how many |
| 160 |
// actual emails are there in the tree? |
| 161 |
// |
| 162 |
var nc int |
| 163 |
Count(sliceRoot, &nc) |
| 164 |
fmt.Printf("There are %d test emails", nc) |
| 165 |
// Output: There are 93 test emails |
| 166 |
} |