Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
go2funk/api/collection/example_test.go at main · glours/go2funk · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
glours
go2funk
Repository navigation
Code
Issues
5
(5)
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
go2funk
/
api
/
collection
/
example_test.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
185 lines (141 loc) · 4.39 KB
main
Breadcrumbs
go2funk
/
api
/
collection
/
example_test.go
Copy path
Top
File metadata and controls
Code
Blame
185 lines (141 loc) · 4.39 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package
collection_test
import
(
"fmt"
"maps"
"slices"
"strconv"
"github.com/glours/go2funk/api/collection"
)
func
ExampleList
() {
list
:=
collection
.
Of
(
1
,
2
,
3
,
4
,
5
)
fmt
.
Println
(
list
.
Length
(),
list
.
Head
().
OrElse
(
-
1
))
isEven
:=
func
(
value
int
)
bool
{
return
value
%
2
==
0
}
fmt
.
Println
(
list
.
Filter
(
isEven
).
Map
(
strconv
.
Itoa
))
// Prepend is O(1) and the original list is untouched.
fmt
.
Println
(
list
.
Prepend
(
0
),
list
)
// The zero value is the empty list.
var
zero
collection.
List
[
int
]
fmt
.
Println
(
zero
.
IsEmpty
(),
zero
.
Head
().
IsEmpty
())
// Output:
// 5 1
// List(2, 4)
// List(0, 1, 2, 3, 4, 5) List(1, 2, 3, 4, 5)
// true true
}
func
ExampleList_All
() {
list
:=
collection
.
Of
(
"a"
,
"b"
,
"c"
)
for
value
:=
range
list
.
All
() {
fmt
.
Println
(
value
)
}
fmt
.
Println
(
collection
.
Collect
(
list
.
All
()).
Reverse
())
// Output:
// a
// b
// c
// List(c, b, a)
}
func
ExampleList_Fold
() {
list
:=
collection
.
Of
(
1
,
2
,
3
,
4
)
sum
:=
list
.
Fold
(
0
,
func
(
acc
,
value
int
)
int
{
return
acc
+
value
})
joined
:=
list
.
Fold
(
""
,
func
(
acc
string
,
value
int
)
string
{
return
acc
+
strconv
.
Itoa
(
value
) })
fmt
.
Println
(
sum
,
joined
)
// Output:
// 10 1234
}
func
ExampleTree
() {
tree
:=
collection
.
TreeOf
(
5
,
3
,
8
,
1
,
9
)
fmt
.
Println
(
tree
)
fmt
.
Println
(
tree
.
Len
(),
tree
.
Contains
(
8
),
tree
.
Contains
(
4
))
fmt
.
Println
(
tree
.
Min
().
OrElse
(
-
1
),
tree
.
Max
().
OrElse
(
-
1
))
// Subtrees that cannot hold a value in the range are never visited.
for
value
:=
range
tree
.
Range
(
3
,
8
) {
fmt
.
Println
(
value
)
}
// Insert and Delete return new trees; the original is untouched.
fmt
.
Println
(
tree
.
Insert
(
4
).
Delete
(
9
),
tree
)
// The zero value is the empty tree.
var
zero
collection.
Tree
[
int
]
fmt
.
Println
(
zero
.
IsEmpty
(),
zero
.
Min
().
IsEmpty
())
// Output:
// Tree(1, 3, 5, 8, 9)
// 5 true false
// 1 9
// 3
// 5
// 8
// Tree(1, 3, 4, 5, 8) Tree(1, 3, 5, 8, 9)
// true true
}
func
ExampleTree_Range
() {
tree
:=
collection
.
TreeOf
(
10
,
20
,
30
,
40
,
50
)
for
value
:=
range
tree
.
Range
(
20
,
40
) {
fmt
.
Println
(
value
)
}
// Iteration is in order, and Backward walks the other way.
fmt
.
Println
(
tree
.
ToSlice
())
fmt
.
Println
(
collection
.
CollectTree
(
tree
.
Backward
()).
Max
().
OrElse
(
-
1
))
// Output:
// 20
// 30
// 40
// [10 20 30 40 50]
// 50
}
func
ExampleMap
() {
ages
:=
collection
.
EmptyMap
[
string
,
int
]().
Put
(
"ada"
,
36
).
Put
(
"alan"
,
41
)
fmt
.
Println
(
ages
)
fmt
.
Println
(
ages
.
Len
(),
ages
.
Get
(
"ada"
).
OrElse
(
-
1
),
ages
.
Get
(
"grace"
).
IsEmpty
())
// Put and Delete return new maps; the original is untouched.
fmt
.
Println
(
ages
.
Put
(
"grace"
,
85
).
Delete
(
"alan"
),
ages
)
// Map changes the value type and keeps every key.
fmt
.
Println
(
ages
.
Map
(
func
(
age
int
)
string
{
return
strconv
.
Itoa
(
age
)
+
" years"
}))
// The zero value is the empty map.
var
zero
collection.
Map
[
string
,
int
]
fmt
.
Println
(
zero
.
IsEmpty
(),
zero
.
Get
(
"ada"
).
IsEmpty
())
// Output:
// Map[ada:36 alan:41]
// 2 36 true
// Map[ada:36 grace:85] Map[ada:36 alan:41]
// Map[ada:36 years alan:41 years]
// true true
}
func
ExampleCollectMap
() {
// In from a Go map, and back out, through the standard iterators.
ages
:=
collection
.
CollectMap
(
maps
.
All
(
map
[
string
]
int
{
"ada"
:
36
,
"alan"
:
41
}))
older
:=
ages
.
Filter
(
func
(
_
string
,
age
int
)
bool
{
return
age
>
40
})
fmt
.
Println
(
maps
.
Collect
(
older
.
All
()))
// Iteration order is unspecified, so sort when order matters.
fmt
.
Println
(
slices
.
Sorted
(
ages
.
Keys
()))
total
:=
ages
.
Fold
(
0
,
func
(
sum
int
,
_
string
,
age
int
)
int
{
return
sum
+
age
})
fmt
.
Println
(
total
)
// Output:
// map[alan:41]
// [ada alan]
// 77
}
func
ExampleSet
() {
team
:=
collection
.
SetOf
(
"ada"
,
"alan"
,
"grace"
)
reviewers
:=
collection
.
SetOf
(
"grace"
,
"linus"
)
fmt
.
Println
(
team
)
fmt
.
Println
(
team
.
Len
(),
team
.
Contains
(
"ada"
),
team
.
Contains
(
"linus"
))
fmt
.
Println
(
team
.
Union
(
reviewers
))
fmt
.
Println
(
team
.
Intersection
(
reviewers
))
fmt
.
Println
(
team
.
Difference
(
reviewers
))
// Insert and Delete return new sets; the original is untouched.
fmt
.
Println
(
team
.
Insert
(
"linus"
).
Delete
(
"alan"
),
team
)
// Numbers print sorted as text; sort them yourself when the order matters.
numbers
:=
collection
.
SetOf
(
2
,
10
,
1
)
fmt
.
Println
(
numbers
,
slices
.
Sorted
(
numbers
.
All
()))
// The zero value is the empty set.
var
zero
collection.
Set
[
string
]
fmt
.
Println
(
zero
.
IsEmpty
(),
zero
.
Contains
(
"ada"
))
// Output:
// Set(ada, alan, grace)
// 3 true false
// Set(ada, alan, grace, linus)
// Set(grace)
// Set(ada, alan)
// Set(ada, grace, linus) Set(ada, alan, grace)
// Set(1, 10, 2) [1 2 10]
// true false
}
You can’t perform that action at this time.