# fun
[![GoDoc](https://godoc.org/github.com/luraim/fun?status.svg)](https://godoc.org/github.com/luraim/fun)
### Simple generic utility functions to reduce golang boilerplate
- Inspired by Kotlin and Rust collection functions
- Supplement to the generic functions in golang.org/x/exp/slices and golang.org/x/exp/maps
- Note: The Go compiler does not currently inline generic callback functions. So please use your judgement while using functions from this library that involve callbacks. Use them when the expressiveness is worth any performance degration compared to handcoded *for loop* boilerplate.
## List of functions
- [fun](#fun)
- [Simple generic utility functions to reduce golang boilerplate](#simple-generic-utility-functions-to-reduce-golang-boilerplate)
- [List of functions](#list-of-functions)
- [All](#all)
- [Any](#any)
- [AppendToGroup](#appendtogroup)
- [Associate](#associate)
- [Chunked](#chunked)
- [ChunkedBy](#chunkedby)
- [Distinct](#distinct)
- [DistinctBy](#distinctby)
- [Drop](#drop)
- [DropLast](#droplast)
- [DropWhile](#dropwhile)
- [DropLastWhile](#droplastwhile)
- [Filter](#filter)
- [FilterIndexed](#filterindexed)
- [FilterMap](#filtermap)
- [Fold](#fold)
- [FoldIndexed](#foldindexed)
- [FoldItems](#folditems)
- [GetOrInsert](#getorinsert)
- [GroupBy](#groupby)
- [Items](#items)
- [Map](#map)
- [MapIndexed](#mapindexed)
- [Partition](#partition)
- [Reduce](#reduce)
- [ReduceIndexed](#reduceindexed)
- [Reverse](#reverse)
- [Reversed](#reversed)
- [Take](#take)
- [TakeLast](#takelast)
- [TakeWhile](#takewhile)
- [TakeLastWhile](#takelastwhile)
- [TransformMap](#transformmap)
- [Unzip](#unzip)
- [Windowed](#windowed)
- [Zip](#zip)
### All
- Returns true if all elements return true for given predicate
```go
All([]int{1, 2, 3, 4, 5}, func(i int)bool {return i < 7})
// true
All([]int{1, 2, 3, 4, 5}, func(i int)bool {return i % 2 == 0})
// false
```
### Any
- Returns true if at least one element returns true for given predicate
```go
Any([]int{1, 2, 3}, func(i int)bool {return i%2==0})
// true
Any([]int{1, 2, 3}, func(i int)bool {return i > 7})
// false
```
### AppendToGroup
- Adds the key, value to the given map where each key maps to a slice of values
```go
group := make(map[string][]int)
AppendToGroup(grp, "a", 1)
AppendToGroup(grp, "b", 2)
AppendToGroup(grp, "a", 10)
AppendToGroup(grp, "b", 20)
AppendToGroup(grp, "a", 100)
AppendToGroup(grp, "b", 200)
// {"a":[1, 10, 100], "b":[2, 20, 200]}
```
### Associate
- Returns a map containing key-value pairs returned by the given function applied to the elements of the given slice
```go
Associate([]int{1, 2, 3, 4}, func(i int) (string, int) {
return fmt.Sprintf("M%d", i), i * 10
})
// {"M1": 10, "M2": 20, "M3": 30, "M4": 40}
```
### Chunked
- Splits the slice into a slice of slices, each not exceeding given chunk size
- The last slice might have fewer elements than the given chunk size
```go
Chunked([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}, 2)
// [[1, 2], [3, 4], [5, 6], [7, 8], [9]]
```
### ChunkedBy
- Splits the slice into a slice of slices, starting a new sub slice whenever the callback function returns false.
- The callback function is passed the previous and current element.
```go
input := []int{10, 20, 30, 40, 31, 31, 33, 34, 21, 22, 23, 24, 11, 12, 13, 14}
ChunkedBy(input, func(prev, next int) bool { return prev < next})
// [[10, 20, 30, 40], [31], [31, 33, 34], [21, 22, 23, 24], [11, 12, 13, 14]]
```
### Distinct
- Returns a slice containing only distinct elements from the given slice
```go
Distinct([]int{1, 1, 2, 3, 3, 4, 4, 4, 4, 5, 5, 5})
// [1, 2, 3, 4, 5]
```
### DistinctBy
- Returns a slice containing only distinct elements from the given slice as distinguished by the given selector function
```go
DistinctBy([]string{"a", "A", "b", "B", "c", "C"},func(s string) string {
return strings.ToLower(s)
})
// ["a", "b", "c"]
```
### Drop
- Returns a slice containing all elements except the first n.
```go
// letters = ['a'..'z']
Drop(letters, 23)
// ['x', 'y', 'z']
```
### DropLast
- Returns a slice containing all elements except the last n.
```go
// letters = ['a'..'z']
DropLast(letters, 23)
// ['a', 'b', 'c']
```
### DropWhile
- Returns a slice containing all elements except the first elements that satisfy the given predicate.
```go
// letters = ['a'..'z']
DropWhile(letters, func(r rune) bool { return r < 'x' })
// ['x', 'y', 'z']
```
### DropLastWhile
- Returns a slice containing all elements except the last elements that satisfy the given predicate.
```go
// letters = ['a'..'z']
DropLastWhile(letters, func(r rune) bool { return r > 'c' })
// ['a', 'b', 'c']
```
### Filter
- Returns the slice obtained after retaining only those elements in the given slice for which the given function returns true
```go
Filter([]int{1, 2, 3, 4, 5, 6, 7, 8}, func(i int)bool {return i%2==0})
// [2, 4, 6, 8]
```
### FilterIndexed
- Returns the slice obtained after retaining only those elements in the given slice for which the given function returns true
- Predicate function receives the value as well as its index in the slice.
```go
FilterIndexed([]int{0, 1, 2, 3, 4, 8, 6}, func(index int, v int) bool {
return index == v
})
// [0, 1, 2, 3, 4, 6]
```
### FilterMap
- FilterMap returns the slice obtained after both filtering and mapping using the given function.
- The function should return two values - the result of the mapping operation and whether the element should be included or dropped.
- This is faster than doing separate filter and map operations, since it avoids extra allocations and slice traversals.
- Inspired by std::iter::filter_map in Rust
```go
FilterMap([]int{1, 2, 3, 4, 5},
func(i int) (int, bool) {
if i%2 != 0 {
return i, false // drop odd numbers
}
return i * i, true // square even numbers
})
// [4, 16]
```
### Fold
- Accumulates values starting with given initial value and applying given function to current accumulator and each element of the given slice.
```go
Fold([]int{1, 2, 3, 4, 5}, func(acc, v int) int { return acc + v })
// 15
```
### FoldIndexed
- Accumulates values starting with given initial value and applying given function to current accumulator and each element of the given slice.
- Function also receives index of current element.
```go
FoldIndexed([]int{1, 2, 3, 4, 5}, func(index, acc, v int) int {
return acc + index*v
})
// 40
```
### FoldItems
- Accumulates values starting with given intial value and applying given function to current accumulator and each key, value of the given map.
- Accumulator can be of any reference type.
```go
m := map[int]int{1: 10, 2: 20, 3: 30}
FoldItems(m, func(acc map[string]string, k, v int) map[string]string {
acc[fmt.Sprintf("entry_%d", k)] = fmt.Sprintf("%d->%d", k, v)
return acc
})
// {"entry_1": "1->10", "entry_2": "2->20", "entry_3": "3->30"}
```
### GetOrInsert
- checks if a value corresponding to the given key is present in the map.
- If present it returns the existing value.
- If not present, it invokes the given callback function to get a new value for the given key, inserts it in the map and returns the new value
```go
m := map[int]int{1:10, 2:20}
GetOrInsert(m, 3, func(i int) int {return i * 10})
// returns 30; m is updated to {1:10, 2:20, 3:30},
```
### GroupBy
- Returns a map where each key maps to slices of elements all having the same key as returned by the given function
```go
GroupBy([]string{"a", "abc", "ab", "def", "abcd"}, func(s string) (int,string) {
return len(s), s
})
// {1: ["a"], 2: ["ab"], 3: ["abc", "def"], 4: ["abcd"]},
```
### Items
- Returns the (key, value) pairs of the given map as a slice
```go
// m := map[string][]int{"a": {1, 2, 3, 4}, "b": {1, 2}, "c": {1, 2, 3}}
Items(m)
// []*Pair[string, []int]{
// {"a", []int{1, 2, 3, 4}},
// {"b", []int{1, 2}},
// {"c",
golang的通用功能助手___下载.zip
版权申诉
36 浏览量
2023-04-18
00:22:06
上传
评论
收藏 16KB ZIP 举报
快撑死的鱼
- 粉丝: 1w+
- 资源: 9154
最新资源
- (54节)视频号0粉起号课,逻辑梳理,技能提升,完整版-课程网盘链接提取码下载 .txt
- Mendix5-添加自定义逻辑
- 基于java的图片浏览小程序源码.zip
- 陈呵keyshot产品渲染第2期-课程网盘链接提取码下载 .txt
- 分层MPC控制器的实时车辆最优控制和避障Hierarchical MPC controller for real time ve
- 基于QT+C++实现微动背景,界面看似静态实则动态+源码
- 非线性弹簧质量系统的简单模型预测控制(MPC)Simple Model Predictive Control (MPC) for
- GEE案例-广东省梅州市梅县区松口镇的梅江横山水灾监测(利用sentinel-1和大津法计算洪水时序面积).pdf
- camunda简介&实现简单审批流程文档
- 科来网络分析系统15.2.0.15277-技术交流版
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈