add IterBy() functions

This commit is contained in:
Jeff Carr 2025-03-18 15:13:40 -05:00
parent 63a3d0dc24
commit ce1f320032
3 changed files with 27 additions and 4 deletions

View File

@ -119,11 +119,9 @@ func (pb *Fruits) printTable() {
func (pb *Fruits) sortTable(i int) {
var count int
all := pb.SortByBrand()
for all.Scan() {
tmp := all.Next()
for fruit := range pb.IterByBrand() {
count += 1
log.Printf("found %d %s %s\n", count, tmp.Brand, tmp.City)
log.Printf("found %d %s %s\n", count, fruit.Brand, fruit.City)
if count > i {
break
}

View File

@ -125,6 +125,13 @@ func (pb *Files) makeNewSortfile(pf *File) error {
selectName := "selectAll" + VARNAME
funcdef := pmsg.newSortBy(wSort, PARENT, s.VarType, sortname, sortby, selectName, v.VarName)
log.Printf("Adding %s\n", funcdef)
FRUITS := PARENT
FRUIT := s.VarType
SORTNAME := sortname
BRAND := v.VarName
funcdef = pmsg.addIterBy(wSort, FRUITS, FRUIT, BRAND, SORTNAME)
log.Printf("Adding %s\n", funcdef)
} else {
// deprecate this THIS DOES NOT MAKE SENSE TO DO
sortby = "Sort" + VARNAME + "By" + v.VarName

View File

@ -116,6 +116,24 @@ func (msg *MsgName) newSortBy(w io.Writer, STRUCT, ITER, SORTNAME, SORTBY, SELEC
return funcdef
}
func (msg *MsgName) addIterBy(w io.Writer, FRUITS, FRUIT, APPLE, SORTNAME string) string {
fmt.Fprintln(w, "// for range syntax using iter package")
fmt.Fprintln(w, "func (x *"+FRUITS+") IterBy"+APPLE+"() iter.Seq[*"+FRUIT+"] {")
fmt.Fprintln(w, " items := x.selectAll"+FRUITS+"()")
fmt.Fprintln(w, " sort.Sort("+SORTNAME+"(items))")
fmt.Fprintln(w, " log.Println(\"Made Iter.Seq[] with length\", len(items))")
fmt.Fprintln(w, " return func(yield func(*"+FRUIT+") bool) {")
fmt.Fprintln(w, " for _, v := range items {")
fmt.Fprintln(w, " if !yield(v) {")
fmt.Fprintln(w, " return")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, " }")
fmt.Fprintln(w, "}")
return ""
}
func (msg *MsgName) addAllFunc(w io.Writer, FRUIT, APPLE, APPLES string) string {
funcdef := "func (x *" + FRUIT + ") All() *" + APPLE + "Iterator {"