File: dictionary_examples_test.go

package info (click to toggle)
golang-github-marstr-collection 2.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 208 kB
  • sloc: makefile: 2
file content (92 lines) | stat: -rw-r--r-- 1,566 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package collection_test

import (
	"context"
	"fmt"
	"strings"

	"github.com/marstr/collection/v2"
)

func ExampleDictionary_Add() {
	subject := &collection.Dictionary{}

	const example = "hello"
	fmt.Println(subject.Contains(example))
	fmt.Println(subject.Size())
	subject.Add(example)
	fmt.Println(subject.Contains(example))
	fmt.Println(subject.Size())

	// Output:
	// false
	// 0
	// true
	// 1
}

func ExampleDictionary_Clear() {
	subject := &collection.Dictionary{}

	subject.Add("hello")
	subject.Add("world")

	fmt.Println(subject.Size())
	fmt.Println(collection.CountAll[string](subject))

	subject.Clear()

	fmt.Println(subject.Size())
	fmt.Println(collection.Any[string](subject))

	// Output:
	// 2
	// 2
	// 0
	// false
}

func ExampleDictionary_Enumerate() {
	subject := &collection.Dictionary{}
	subject.Add("world")
	subject.Add("hello")

	upperCase := collection.Select[string](subject, strings.ToUpper)

	for word := range subject.Enumerate(context.Background()) {
		fmt.Println(word)
	}

	for word := range upperCase.Enumerate(context.Background()) {
		fmt.Println(word)
	}

	// Output:
	// hello
	// world
	// HELLO
	// WORLD
}

func ExampleDictionary_Remove() {
	const world = "world"
	subject := &collection.Dictionary{}
	subject.Add("hello")
	subject.Add(world)

	fmt.Println(subject.Size())
	fmt.Println(collection.CountAll[string](subject))

	subject.Remove(world)

	fmt.Println(subject.Size())
	fmt.Println(collection.CountAll[string](subject))
	fmt.Println(collection.Any[string](subject))

	// Output:
	// 2
	// 2
	// 1
	// 1
	// true
}