File: setiterator.go

package info (click to toggle)
delve 1.24.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 14,092 kB
  • sloc: ansic: 111,943; sh: 169; asm: 141; makefile: 43; python: 23
file content (51 lines) | stat: -rw-r--r-- 830 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main

import (
	"fmt"
	"iter"
	"strconv"
	"time"
)

func main() {
	set := New[string]()
	for i := 10; i < 100; i++ {
		set.Add(strconv.Itoa(i))
	}
	PrintAllElements[string](set)
}

// Set holds a set of elements.
type Set[E comparable] struct {
	m map[E]struct{}
}

// New returns a new [Set].
func New[E comparable]() *Set[E] {
	return &Set[E]{m: make(map[E]struct{})}
}

// All is an iterator over the elements of s.
func (s *Set[E]) All() iter.Seq[E] {
	return func(yield func(E) bool) {
		for v := range s.m {
			tmp := make([]byte, 1024)
			str := string(tmp)
			if !yield(v) {
				return
			}
			go func() { println(str) }()
		}
	}
}

func (s *Set[E]) Add(v E) {
	s.m[v] = struct{}{}
}

func PrintAllElements[E comparable](s *Set[E]) {
	for v := range s.All() {
		time.Sleep(100 * time.Second)
		fmt.Println(v)
	}
}