├── .github ├── release.yml ├── dependabot.yml └── workflows │ ├── release.yml │ └── ci.yml ├── go.mod ├── go.sum ├── godash_compat.go ├── godash_go123.go ├── LICENSE ├── .tagpr ├── README.md ├── CHANGELOG.md ├── godash.go └── godash_test.go /.github/release.yml: -------------------------------------------------------------------------------- 1 | changelog: 2 | exclude: 3 | labels: 4 | - tagpr 5 | -------------------------------------------------------------------------------- /go.mod: -------------------------------------------------------------------------------- 1 | module github.com/hatena/godash 2 | 3 | go 1.21.1 4 | 5 | require github.com/samber/lo v1.46.0 6 | 7 | require golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 8 | 9 | require golang.org/x/text v0.16.0 // indirect 10 | -------------------------------------------------------------------------------- /.github/dependabot.yml: -------------------------------------------------------------------------------- 1 | version: 2 2 | updates: 3 | - package-ecosystem: gomod 4 | directory: / 5 | schedule: 6 | interval: weekly 7 | day: monday 8 | time: "09:00" 9 | timezone: Asia/Tokyo 10 | open-pull-requests-limit: 10 11 | - package-ecosystem: github-actions 12 | directory: / 13 | schedule: 14 | interval: weekly 15 | day: monday 16 | time: "09:00" 17 | timezone: Asia/Tokyo 18 | open-pull-requests-limit: 10 19 | -------------------------------------------------------------------------------- /go.sum: -------------------------------------------------------------------------------- 1 | github.com/samber/lo v1.46.0 h1:w8G+oaCPgz1PoCJztqymCFaKwXt+5cCXn51uPxExFfQ= 2 | github.com/samber/lo v1.46.0/go.mod h1:RmDH9Ct32Qy3gduHQuKJ3gW1fMHAnE/fAzQuf6He5cU= 3 | golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM= 4 | golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= 5 | golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4= 6 | golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI= 7 | -------------------------------------------------------------------------------- /godash_compat.go: -------------------------------------------------------------------------------- 1 | //go:build !go1.23 2 | 3 | package godash 4 | 5 | import ( 6 | "github.com/samber/lo" 7 | ) 8 | 9 | // Chunk receives the collection and chunks it into a slice of slices each of the given size. 10 | func Chunk[T any](collection []T, size int) [][]T { 11 | return lo.Chunk(collection, size) 12 | } 13 | 14 | // Keys returns the keys of the map as a slice. 15 | func Keys[K comparable, V any](in map[K]V) []K { 16 | return lo.Keys(in) 17 | } 18 | 19 | // Values returns the values of the map as a slice. 20 | func Values[K comparable, V any](in map[K]V) []V { 21 | return lo.Values(in) 22 | } 23 | -------------------------------------------------------------------------------- /.github/workflows/release.yml: -------------------------------------------------------------------------------- 1 | name: Release with tagpr 2 | 3 | on: 4 | push: 5 | branches: ["main"] 6 | 7 | jobs: 8 | tagpr: 9 | runs-on: ubuntu-latest 10 | steps: 11 | - uses: actions/create-github-app-token@67018539274d69449ef7c02e8e71183d1719ab42 # v2.1.4 12 | id: app-token 13 | with: 14 | app-id: ${{ vars.APP_ID }} 15 | private-key: ${{ secrets.PRIVATE_KEY }} 16 | - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 17 | with: 18 | token: ${{ steps.app-token.outputs.token }} 19 | - uses: Songmu/tagpr@7191605433b03e11b313dbbc0efb80185170de4b # v1.9.0 20 | env: 21 | GITHUB_TOKEN: ${{ steps.app-token.outputs.token }} 22 | -------------------------------------------------------------------------------- /godash_go123.go: -------------------------------------------------------------------------------- 1 | //go:build go1.23 2 | 3 | package godash 4 | 5 | import ( 6 | "github.com/samber/lo" 7 | ) 8 | 9 | // Chunk receives the collection and chunks it into a slice of slices each of the given size. 10 | // 11 | // Deprecated: Use https://pkg.go.dev/slices#Chunk instead. 12 | func Chunk[T any](collection []T, size int) [][]T { 13 | return lo.Chunk(collection, size) 14 | } 15 | 16 | // Keys returns the keys of the map as a slice. 17 | // 18 | // Deprecated: Use https://pkg.go.dev/maps#Keys instead. 19 | func Keys[K comparable, V any](in map[K]V) []K { 20 | return lo.Keys(in) 21 | } 22 | 23 | // Values returns the values of the map as a slice. 24 | // 25 | // Deprecated: Use https://pkg.go.dev/maps#Values instead. 26 | func Values[K comparable, V any](in map[K]V) []V { 27 | return lo.Values(in) 28 | } 29 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | main 7 | pull_request: 8 | 9 | permissions: 10 | contents: read 11 | pull-requests: read 12 | 13 | jobs: 14 | test: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 18 | - uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 19 | with: 20 | go-version-file: "go.mod" 21 | - run: go test -v 22 | golangci-lint: 23 | runs-on: ubuntu-latest 24 | steps: 25 | - uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0 26 | - uses: actions/setup-go@44694675825211faa026b3c33043df3e48a5fa00 # v6.0.0 27 | with: 28 | go-version-file: "go.mod" 29 | - name: golangci-lint 30 | uses: golangci/golangci-lint-action@4afd733a84b1f43292c63897423277bb7f4313a9 # v8.0.0 31 | with: 32 | version: v2.5.0 33 | # https://github.com/golangci/golangci-lint-action/issues/1246 34 | #only-new-issues: true 35 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2023 Hatena Co., Ltd. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.tagpr: -------------------------------------------------------------------------------- 1 | # config file for the tagpr in git config format 2 | # The tagpr generates the initial configuration, which you can rewrite to suit your environment. 3 | # CONFIGURATIONS: 4 | # tagpr.releaseBranch 5 | # Generally, it is "main." It is the branch for releases. The tagpr tracks this branch, 6 | # creates or updates a pull request as a release candidate, or tags when they are merged. 7 | # 8 | # tagpr.versionFile 9 | # Versioning file containing the semantic version needed to be updated at release. 10 | # It will be synchronized with the "git tag". 11 | # Often this is a meta-information file such as gemspec, setup.cfg, package.json, etc. 12 | # Sometimes the source code file, such as version.go or Bar.pm, is used. 13 | # If you do not want to use versioning files but only git tags, specify the "-" string here. 14 | # You can specify multiple version files by comma separated strings. 15 | # 16 | # tagpr.vPrefix 17 | # Flag whether or not v-prefix is added to semver when git tagging. (e.g. v1.2.3 if true) 18 | # This is only a tagging convention, not how it is described in the version file. 19 | # 20 | # tagpr.changelog (Optional) 21 | # Flag whether or not changelog is added or changed during the release. 22 | # 23 | # tagpr.command (Optional) 24 | # Command to change files just before release. 25 | # 26 | # tagpr.template (Optional) 27 | # Pull request template in go template format 28 | # 29 | # tagpr.release (Optional) 30 | # GitHub Release creation behavior after tagging [true, draft, false] 31 | # If this value is not set, the release is to be created. 32 | # 33 | # tagpr.majorLabels (Optional) 34 | # Label of major update targets. Default is [major] 35 | # 36 | # tagpr.minorLabels (Optional) 37 | # Label of minor update targets. Default is [minor] 38 | # 39 | [tagpr] 40 | vPrefix = true 41 | releaseBranch = main 42 | versionFile = - 43 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # godash 2 | 3 | [![Go Reference](https://pkg.go.dev/badge/github.com/hatena/godash.svg)](https://pkg.go.dev/github.com/hatena/godash) 4 | 5 | hatena/godash is a minimal wrapper of https://github.com/samber/lo. 6 | 7 | **Though this repository is still maintained, we strongly recommend to use iter instead.** 8 | 9 | ## Motivation 10 | 11 | https://github.com/samber/lo is a nice library, but in some cases teams do not want to have all of the helper functions available. Some helper functions defined in samber/lo are actually already available as part of the official [slices package](https://pkg.go.dev/slices). 12 | 13 | hatena/godash is meant to only provide a small portion of samber/lo's functions: 14 | - functions that are not available via Go's standard library 15 | - functions that are not "too much", such as "Conditional helpers" or "Error handling" 16 | 17 | ## Usage 18 | Install the package to your repo with `go get`. 19 | 20 | ```sh 21 | go get -u github.com/hatena/godash 22 | ``` 23 | 24 | Sample usage: 25 | ```go 26 | package main 27 | 28 | import ( 29 | "fmt" 30 | 31 | "github.com/hatena/godash" 32 | ) 33 | 34 | func main() { 35 | integers := []int{0, 1, 1, 3, 2, 1, 1, 0} 36 | fmt.Println(godash.Chunk(integers, 3)) 37 | } 38 | ``` 39 | 40 | Check out more examples at [godash package - github.com/hatena/godash - Go Packages](https://pkg.go.dev/github.com/hatena/godash). 41 | 42 | ### Recommended golangci-lint configuration 43 | Since this package aims to only present a subset of functions from `samber/lo`, it is recommended to add linter settings to deny importing directly from `samber/lo`. (If you want to use `samber/lo` directly, then there is no meaning to use `hatena/godash`). 44 | 45 | Below is a minimal `.golangci.yml` configuration to deny `samber/lo` imports. 46 | 47 | ```yml 48 | linters: 49 | enable: 50 | - depguard 51 | 52 | linters-settings: 53 | depguard: 54 | rules: 55 | deny-samber-lo: 56 | deny: 57 | - pkg: "github.com/samber/lo" 58 | desc: Use github.com/hatena/godash instead. 59 | ``` 60 | 61 | Once you have `.golangci.yml`, make sure to run `golangci-lint` via CI. Below is an example for GitHub Actions. 62 | 63 | ```yml 64 | name: CI 65 | 66 | on: 67 | push: 68 | branches: main 69 | pull_request: 70 | 71 | permissions: 72 | contents: read 73 | pull-requests: read 74 | 75 | jobs: 76 | lint: 77 | runs-on: ubuntu-latest 78 | steps: 79 | - uses: actions/checkout@v4 80 | - uses: actions/setup-go@v4 81 | with: 82 | go-version-file: "go.mod" 83 | - name: golangci-lint 84 | uses: golangci/golangci-lint-action@v3 85 | with: 86 | version: v1.54.2 87 | only-new-issues: true 88 | ``` 89 | 90 | See [Introduction | golangci-lint](https://golangci-lint.run/) for more details. 91 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## [v1.0.6](https://github.com/hatena/godash/compare/v1.0.5...v1.0.6) - 2025-03-17 4 | - Bump github.com/samber/lo from 1.44.0 to 1.46.0 by @dependabot in https://github.com/hatena/godash/pull/32 5 | - Deprecate methods added in Go 1.23 by @stefafafan in https://github.com/hatena/godash/pull/35 6 | - add a notice to README.md by @lufia in https://github.com/hatena/godash/pull/36 7 | 8 | ## [v1.0.5](https://github.com/hatena/godash/compare/v1.0.4...v1.0.5) - 2024-07-03 9 | - Bump github.com/samber/lo from 1.38.1 to 1.39.0 by @dependabot in https://github.com/hatena/godash/pull/23 10 | - Bump actions/setup-go from 4 to 5 by @dependabot in https://github.com/hatena/godash/pull/25 11 | - Bump golangci/golangci-lint-action from 3 to 5 by @dependabot in https://github.com/hatena/godash/pull/27 12 | - Bump github.com/samber/lo from 1.39.0 to 1.44.0 by @dependabot in https://github.com/hatena/godash/pull/29 13 | - PickByKeys を追加 by @ne-sachirou in https://github.com/hatena/godash/pull/30 14 | - Bump golangci/golangci-lint-action from 5 to 6 by @dependabot in https://github.com/hatena/godash/pull/28 15 | 16 | ## [v1.0.4](https://github.com/hatena/godash/compare/v1.0.3...v1.0.4) - 2023-11-28 17 | - Apply `go mod tidy` by @utgwkk in https://github.com/hatena/godash/pull/21 18 | - Add FlatMap and SomeBy function by @utgwkk in https://github.com/hatena/godash/pull/20 19 | 20 | ## [v1.0.3](https://github.com/hatena/godash/compare/v1.0.2...v1.0.3) - 2023-11-21 21 | - Use generated token for actions/checkout by @stefafafan in https://github.com/hatena/godash/pull/17 22 | - Add Associate wrapper function by @lufia in https://github.com/hatena/godash/pull/19 23 | 24 | ## [v1.0.2](https://github.com/hatena/godash/compare/v1.0.1...v1.0.2) - 2023-10-15 25 | - Add GitHub Workflow for tagpr by @stefafafan in https://github.com/hatena/godash/pull/15 26 | 27 | ## [v1.0.1](https://github.com/hatena/godash/compare/v1.0.0...v1.0.1) - 2023-09-24 28 | - Update README.md with details on setup; fix flaky tests by @stefafafan in https://github.com/hatena/godash/pull/14 29 | 30 | ## [v1.0.0](https://github.com/hatena/godash/commits/v1.0.0) - 2023-09-24 31 | - add dependabot.yml by @stefafafan in https://github.com/hatena/godash/pull/1 32 | - add simple readme by @stefafafan in https://github.com/hatena/godash/pull/2 33 | - Add Uniq / UniqBy wrapper functions by @stefafafan in https://github.com/hatena/godash/pull/3 34 | - Rename repository/package name to godash by @stefafafan in https://github.com/hatena/godash/pull/4 35 | - Add a subset of slice related functions by @stefafafan in https://github.com/hatena/godash/pull/5 36 | - rename package name by @stefafafan in https://github.com/hatena/godash/pull/6 37 | - add more functions that would probably be used a bunch by @stefafafan in https://github.com/hatena/godash/pull/7 38 | - add simple documentation code, as well as a link to the package site by @stefafafan in https://github.com/hatena/godash/pull/8 39 | - nit adjustments to the package doc comments by @stefafafan in https://github.com/hatena/godash/pull/9 40 | - add some example tests along with simple CI by @stefafafan in https://github.com/hatena/godash/pull/10 41 | - add golangci-lint-action; other adjustments to the CI by @stefafafan in https://github.com/hatena/godash/pull/11 42 | - Add dependabot configuration for GitHub Actions by @stefafafan in https://github.com/hatena/godash/pull/12 43 | - Add example tests for the rest of the functions by @stefafafan in https://github.com/hatena/godash/pull/13 44 | -------------------------------------------------------------------------------- /godash.go: -------------------------------------------------------------------------------- 1 | // Package godash implements slice/map related methods and is a wrapper of the [github.com/samber/lo] package. 2 | // 3 | // This package focuses on only presenting a subset of functions from [github.com/samber/lo], 4 | // in order to make sure that the user takes advantage of Go's standard methods from the slice package etc. 5 | // If any of the functions presented here gets included in the standard library, that function will be 6 | // marked deprecated (the function will not get removed, but will point the user to the standard library). 7 | package godash 8 | 9 | import ( 10 | "github.com/samber/lo" 11 | "golang.org/x/exp/constraints" 12 | ) 13 | 14 | // Filter iterates through the collection and returns a slice with only the values that match the predicate. 15 | func Filter[V any](collection []V, predicate func(item V, index int) bool) []V { 16 | return lo.Filter(collection, predicate) 17 | } 18 | 19 | // Map iterates through the collection and returns a slice with the values converted though the iteratee. 20 | func Map[T any, R any](collection []T, iteratee func(item T, index int) R) []R { 21 | return lo.Map(collection, iteratee) 22 | } 23 | 24 | // Reduce iterates through the collection and reduces it to one value, using the accumulator. 25 | func Reduce[T any, R any](collection []T, accumulator func(agg R, item T, index int) R, initial R) R { 26 | return lo.Reduce(collection, accumulator, initial) 27 | } 28 | 29 | // Uniq returns a slice with only unique values. 30 | func Uniq[T comparable](collection []T) []T { 31 | return lo.Uniq(collection) 32 | } 33 | 34 | // UniqBy returns a slice with only unique values. The uniqueness is determined by the iteratee. 35 | func UniqBy[T any, U comparable](collection []T, iteratee func(item T) U) []T { 36 | return lo.UniqBy(collection, iteratee) 37 | } 38 | 39 | // GroupBy groups the values of the collection using the iteratee, and returns it as a map. 40 | func GroupBy[T any, U comparable](collection []T, iteratee func(item T) U) map[U][]T { 41 | return lo.GroupBy(collection, iteratee) 42 | } 43 | 44 | // PartitionBy partitions the collection by the iteratee. This is similar to Chunk, but instead of a given size, a function can be applied. 45 | // Use GroupBy if you want a map of slices. 46 | func PartitionBy[T any, K comparable](collection []T, iteratee func(item T) K) [][]T { 47 | return lo.PartitionBy(collection, iteratee) 48 | } 49 | 50 | // Flatten flattens a slice of slices to a single slice. 51 | func Flatten[T any](collection [][]T) []T { 52 | return lo.Flatten(collection) 53 | } 54 | 55 | // KeyBy iterates through the collection and returns a map with the key generated via the iteratee. 56 | func KeyBy[K comparable, V any](collection []V, iteratee func(item V) K) map[K]V { 57 | return lo.KeyBy(collection, iteratee) 58 | } 59 | 60 | // Sum returns a sum of the values within the slice. The values of the slice need to be numbers. 61 | func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T { 62 | return lo.Sum(collection) 63 | } 64 | 65 | // SumBy returns a sum of the values within the slice, added using the specified iteratee function. The values of the slice need to be numbers. 66 | func SumBy[T any, R constraints.Float | constraints.Integer | constraints.Complex](collection []T, iteratee func(item T) R) R { 67 | return lo.SumBy(collection, iteratee) 68 | } 69 | 70 | // EveryBy returns whether or not all the values within the collection meet the predicate. 71 | func EveryBy[T any](collection []T, predicate func(item T) bool) bool { 72 | return lo.EveryBy(collection, predicate) 73 | } 74 | 75 | // SomeBy returns whether or not any of the values within the collection meet the predicate. 76 | func SomeBy[T any](collection []T, predicate func(item T) bool) bool { 77 | return lo.SomeBy(collection, predicate) 78 | } 79 | 80 | // NoneBy returns whether or not all the values within the collection do not meet the predicate. 81 | func NoneBy[T any](collection []T, predicate func(item T) bool) bool { 82 | return lo.NoneBy(collection, predicate) 83 | } 84 | 85 | // Find returns the first value that meets the predicate, as well as whether or not a match was found. 86 | func Find[T any](collection []T, predicate func(item T) bool) (T, bool) { 87 | return lo.Find(collection, predicate) 88 | } 89 | 90 | func Associate[T any, K comparable, V any](collection []T, transform func(item T) (K, V)) map[K]V { 91 | return lo.Associate(collection, transform) 92 | } 93 | 94 | // FlatMap iterates through the collection and returns a slice with the values converted through the iteratee and flattened. 95 | func FlatMap[T any, R any](collection []T, iteratee func(item T, index int) []R) []R { 96 | return lo.FlatMap(collection, iteratee) 97 | } 98 | 99 | // PickByKeys returns same map type filtered by given keys. 100 | func PickByKeys[K comparable, V any, Map ~map[K]V](in Map, keys []K) Map { 101 | return lo.PickByKeys(in, keys) 102 | } 103 | -------------------------------------------------------------------------------- /godash_test.go: -------------------------------------------------------------------------------- 1 | package godash_test 2 | 3 | import ( 4 | "fmt" 5 | "slices" 6 | 7 | "github.com/hatena/godash" 8 | ) 9 | 10 | func ExampleFilter() { 11 | integers := []int{-2, 0, 19, 5, 42} 12 | fmt.Println(godash.Filter(integers, func(x int, _ int) bool { 13 | return x%2 == 0 14 | })) 15 | // Output: [-2 0 42] 16 | } 17 | 18 | func ExampleMap() { 19 | integers := []int{-2, 0, 19, 5, 42} 20 | fmt.Println(godash.Map(integers, func(x int, _ int) int { 21 | return x * 2 22 | })) 23 | // Output: [-4 0 38 10 84] 24 | } 25 | 26 | func ExampleReduce() { 27 | integers := []int{-2, 0, 19, 5, 42} 28 | fmt.Println(godash.Reduce(integers, func(agg int, x int, _ int) int { 29 | return agg + x 30 | }, 0)) 31 | // Output: 64 32 | } 33 | 34 | func ExampleUniq() { 35 | integers := []int{0, 1, 1, 3, 2, 1, 1, 0} 36 | fmt.Println(godash.Uniq(integers)) 37 | // Output: [0 1 3 2] 38 | } 39 | 40 | func ExampleUniqBy() { 41 | type User struct { 42 | id int 43 | name string 44 | } 45 | users := []User{ 46 | {1, "foo"}, 47 | {2, "foobar"}, 48 | {2, "foobar"}, 49 | {3, "foo"}, 50 | } 51 | fmt.Println(godash.UniqBy(users, func(u User) int { 52 | return u.id 53 | })) 54 | // Output: [{1 foo} {2 foobar} {3 foo}] 55 | } 56 | 57 | func ExampleGroupBy() { 58 | type User struct { 59 | id int 60 | name string 61 | } 62 | users := []User{ 63 | {1, "foo"}, 64 | {2, "foobar"}, 65 | {3, "foobar"}, 66 | {4, "foo"}, 67 | {5, "bar"}, 68 | } 69 | fmt.Println(godash.GroupBy(users, func(u User) string { 70 | return u.name 71 | })) 72 | // Output: map[bar:[{5 bar}] foo:[{1 foo} {4 foo}] foobar:[{2 foobar} {3 foobar}]] 73 | } 74 | 75 | func ExampleChunk() { 76 | integers := []int{0, 1, 1, 3, 2, 1, 1, 0} 77 | fmt.Println(godash.Chunk(integers, 3)) 78 | // Output: [[0 1 1] [3 2 1] [1 0]] 79 | } 80 | 81 | func ExamplePartitionBy() { 82 | type User struct { 83 | id int 84 | name string 85 | } 86 | users := []User{ 87 | {1, "foo"}, 88 | {2, "foobar"}, 89 | {3, "foobar"}, 90 | {4, "foo"}, 91 | {5, "bar"}, 92 | } 93 | fmt.Println(godash.PartitionBy(users, func(u User) string { 94 | return u.name 95 | })) 96 | // Output: [[{1 foo} {4 foo}] [{2 foobar} {3 foobar}] [{5 bar}]] 97 | } 98 | 99 | func ExampleFlatten() { 100 | integers := [][]int{ 101 | {1, 2, 5, 10}, 102 | {9, 8, 1}, 103 | {0}, 104 | {42, 3}, 105 | } 106 | fmt.Println(godash.Flatten(integers)) 107 | // Output: [1 2 5 10 9 8 1 0 42 3] 108 | } 109 | 110 | func ExampleKeyBy() { 111 | type User struct { 112 | id int 113 | name string 114 | } 115 | users := []User{ 116 | {1, "foo"}, 117 | {2, "foobar"}, 118 | {3, "foobar"}, 119 | {4, "foo"}, 120 | {5, "bar"}, 121 | } 122 | fmt.Println(godash.KeyBy(users, func(u User) int { 123 | return u.id 124 | })) 125 | // Output: map[1:{1 foo} 2:{2 foobar} 3:{3 foobar} 4:{4 foo} 5:{5 bar}] 126 | } 127 | 128 | func ExampleKeys() { 129 | m := map[int]string{ 130 | 1: "foo", 131 | 2: "bar", 132 | 3: "baz", 133 | } 134 | keys := godash.Keys(m) 135 | slices.Sort(keys) 136 | fmt.Println(keys) 137 | // Output: [1 2 3] 138 | } 139 | 140 | func ExampleValues() { 141 | m := map[int]string{ 142 | 1: "foo", 143 | 2: "bar", 144 | 3: "baz", 145 | } 146 | values := godash.Values(m) 147 | slices.Sort(values) 148 | fmt.Println(values) 149 | // Output: [bar baz foo] 150 | } 151 | 152 | func ExampleSum() { 153 | numbers := []float32{1, 5, 2, 3.2, 42} 154 | fmt.Println(godash.Sum(numbers)) 155 | // Output: 53.2 156 | } 157 | 158 | func ExampleSumBy() { 159 | type User struct { 160 | name string 161 | age int 162 | } 163 | users := []User{ 164 | {"foo", 20}, 165 | {"bar", 25}, 166 | {"baz", 40}, 167 | } 168 | fmt.Println(godash.SumBy(users, func(u User) int { 169 | return u.age 170 | })) 171 | // Output: 85 172 | } 173 | 174 | func ExampleEveryBy() { 175 | type User struct { 176 | name string 177 | age int 178 | } 179 | users := []User{ 180 | {"foo", 20}, 181 | {"bar", 25}, 182 | {"baz", 40}, 183 | } 184 | fmt.Println(godash.EveryBy(users, func(u User) bool { 185 | return u.age >= 20 186 | })) 187 | // Output: true 188 | } 189 | 190 | func ExampleSomeBy() { 191 | type User struct { 192 | name string 193 | age int 194 | } 195 | users := []User{ 196 | {"foo", 20}, 197 | {"bar", 25}, 198 | {"baz", 40}, 199 | } 200 | fmt.Println(godash.SomeBy(users, func(u User) bool { 201 | return u.age >= 30 202 | })) 203 | // Output: true 204 | } 205 | 206 | func ExampleNoneBy() { 207 | type User struct { 208 | name string 209 | age int 210 | } 211 | users := []User{ 212 | {"foo", 20}, 213 | {"bar", 25}, 214 | {"baz", 40}, 215 | } 216 | fmt.Println(godash.NoneBy(users, func(u User) bool { 217 | return u.age > 40 218 | })) 219 | // Output: true 220 | } 221 | 222 | func ExampleFind() { 223 | type User struct { 224 | name string 225 | age int 226 | } 227 | users := []User{ 228 | {"foo", 20}, 229 | {"bar", 25}, 230 | {"baz", 40}, 231 | } 232 | fmt.Println(godash.Find(users, func(u User) bool { 233 | return u.age > 24 234 | })) 235 | // Output: {bar 25} true 236 | } 237 | 238 | func ExampleAssociate() { 239 | type User struct { 240 | name string 241 | age int 242 | } 243 | users := []User{ 244 | {"foo", 20}, 245 | {"bar", 25}, 246 | {"baz", 40}, 247 | } 248 | fmt.Println(godash.Associate(users, func(u User) (string, int) { 249 | return u.name, u.age 250 | })) 251 | // Output: map[bar:25 baz:40 foo:20] 252 | } 253 | 254 | func ExampleFlatMap() { 255 | integers := []int{0, 1, 2, 3} 256 | fmt.Println(godash.FlatMap(integers, func(x int, _ int) []int { 257 | return []int{2 * x, 2*x + 1} 258 | })) 259 | // Output: [0 1 2 3 4 5 6 7] 260 | } 261 | 262 | func ExamplePickByKeys() { 263 | kv := map[string]int{"foo": 1, "bar": 2, "baz": 3} 264 | result := godash.PickByKeys(kv, []string{"foo", "baz"}) 265 | fmt.Printf("%v", result) 266 | // Output: map[baz:3 foo:1] 267 | } 268 | --------------------------------------------------------------------------------