Skip to content

stable-online/go-support

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Support

Support 是通过Golang 1.18+版本以上,提供的开箱即用的功能工具助手.

Support is a Programming helpers powered by Golang 1.18+ generics.

Contents

Install

因为要用到泛型, 需要 go 版本为 1.18+

Requires go 1.18+

go get github.com/stable-online/support

OverviewCases

Slice

support.NewS([]string{"a", "b", "c"}).Map(support.SMapF(func(key int, item string) string {
// Concatenate the string '1' to the value of each item.
// 切片内每一个元素后拼接一个 1 字符串
return item + "1"
})).Filter(support.SFilterF(func(i int, t string) bool {
// Display to standard output the values of each item that is not equal to "a1".
// 如果不等于1, 返回true(将元素返回)	
return t != "a1"
})).Reduce(support.SReduceF(func(carry string, item string) string {
// Concatenate the value of each item.
// 拼接切片内的每一个元素. 	
return carry + item
}, ""))

// => b1c1

Map

support.NewM(map[string]int{"a": 1, "b": 2, "c": 3}).Map(support.MMapF(func(key string, v int) int {
// Add 1 to the value of each item.
// 字典内的每一个元素 + 1	
return v + 1
})).Filter(support.MFilterF(func(k string, t int) bool {
// Display to standard output the values of each item that is not equal to 2.
// 如果不等于2, 返回true(将元素返回)	
return t != 2
})).Reduce(support.MReduceF(func(carry int, key string, item int) int {
// Add the result to the value of each item.
// 将字典内的每一个元素值相加.
return carry + item
}, 0))

// => b1c1

Features

SliceStream

NewS

support.NewS([]string{"a", "b", "c"})

// => [a b c]

Get

support.NewS([]string{"a", "b", "c"}).Get()

// => [a b c]

Map

support.NewS([]string{"a", "b", "c"}).Map(support.SMapF(func(i int, t string) string { return t + "h" })).Get()

// => [ah bh ch]

Filter

support.NewS([]string{"a", "b", "c"}).Filter(support.SFilterF(func(k int, v string) bool {return v != "a" })).Get()

// => &{[b c]}

Reduce

support.NewS([]string{"a", "b", "c"}).Reduce(support.SReduceF(func(carry string, item string) string {return carry + item }, ""))

// => abc

SliceFunction

SMapF

support.SMapF(func(key int, item string) string { return item + "1" })([]string{"a", "b", "c"})

// => [a1 b1 c1]

SFilterF

support.SFilterF(func(i int, t string) bool { return t != "a" })([]string{"a", "b", "c"})

// => [b c]

SReduceF

support.SReduceF(func(i string, t string) string { return i + t }, "")([]string{"a", "b", "c"})

// => abc

MapStream

NewM

support.NewM(map[string]string{"a": "1", "b": "2", "c": "3"})

// => map[a:1 b:2 c:3]

Get

support.NewM(map[string]string{"a": "1", "b": "2", "c": "3"}).Get()

// => map[a:1 b:2 c:3]

Map

support.NewM(map[string]string{"a": "1", "b": "2", "c": "3"}).Map(support.MMapF(func(key string, val string) (res string) { return val + "1" })).Get()

// => map[a:11 b:21 c:31]

Filter

support.NewM(map[string]string{"a": "1", "b": "2", "c": "3"}).Filter(support.MFilterF(func(k string, t string) bool { return t != "1" })).Get()

// => map[b:2 c:3]

Reduce

support.NewM(map[string]string{"a": "1", "b": "2", "c": "3"}).Reduce(support.MReduceF(func(carry string, key string, item string) string { return carry + key + item }, ""))

// tips : Because the output of maps is unordered, the output may be different each time.
// => abc 

MapFunction

MMapF

support.MMapF(func(key string, val string) (res string) { return key + val })(map[string]string{"a": "1", "b": "2", "c": "3"})

// => map[a:a1 b:b2 c:c3]

MFilterF

support.MFilterF(func(key string, val string) bool { return val != "2" })(map[string]string{"a": "1", "b": "2", "c": "3"})

// => map[a:1 c:3]

MReduceF

support.MReduceF(func(carry string, key string, item string) string { return carry + key + item }, "")(map[string]string{"a": "1", "b": "2", "c": "3"})

// => a1b2c3

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages