diff --git a/docs/content/_index.md b/docs/content/_index.md index cbaaacb..57fe824 100644 --- a/docs/content/_index.md +++ b/docs/content/_index.md @@ -9,9 +9,11 @@ title: _Underscore ![underscore](https://socialify.git.ci/rjNemo/underscore/image?description=1&font=KoHo&language=1&logo=https%3A%2F%2Fraw.githubusercontent.com%2FrjNemo%2Funderscore%2Fmain%2Fdocs%2Fstatic%2Flogo.png&owner=1&pattern=Floating%20Cogs&stargazers=1&theme=Dark) -`underscore` is a `Go` library providing useful functional programming helpers without extending any built-in objects. +`underscore` is a `Go` library providing useful functional programming helpers without +extending any built-in objects. -It is mostly a port from the `underscore.js` library based on generics available from `go1.18`. +It is mostly a port from the `underscore.js` library based on generics available +from `go1.18`. ## Quick Start @@ -21,25 +23,26 @@ Install the library using go get github.com/rjNemo/underscore ``` -Please check out the [examples](https://github.com/rjNemo/underscore/tree/main/examples) to see how to use the library. +Please check out the [examples](https://github.com/rjNemo/underscore/tree/main/examples) +to see how to use the library. ```go package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} - // filter even numbers from the slice - evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 }) - // square every number in the slice - squares := u.Map(evens, func(n int) int { return n * n }) - // reduce to the sum - res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0) + numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} + // filter even numbers from the slice + evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 }) + // square every number in the slice + squares := u.Map(evens, func(n int) int { return n * n }) + // reduce to the sum + res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0) - fmt.Println(res) // 120 + fmt.Println(res) // 120 } ``` diff --git a/docs/content/collections/all.md b/docs/content/collections/all.md index 4927021..760fc87 100644 --- a/docs/content/collections/all.md +++ b/docs/content/collections/all.md @@ -3,7 +3,6 @@ title: "All" date: 2021-12-30T13:24:39-04:00 --- - `All` returns true if all the values in the slice pass the predicate truth test.\ Short-circuits and stops traversing the slice if a false element is found. @@ -11,13 +10,13 @@ Short-circuits and stops traversing the slice if a false element is found. package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 3, 5, 7, 9} - isOdd := func(n int) bool { return n%2 != 0 } - fmt.Println(u.All(nums, isOdd)) // true + nums := []int{1, 3, 5, 7, 9} + isOdd := func(n int) bool { return n%2 != 0 } + fmt.Println(u.All(nums, isOdd)) // true } ``` diff --git a/docs/content/collections/any.md b/docs/content/collections/any.md index 2c56c89..7ecb574 100644 --- a/docs/content/collections/any.md +++ b/docs/content/collections/any.md @@ -10,13 +10,13 @@ the slice if a true element is found. package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 2, 4, 6, 8} - isEven := func(n int) bool { return n%2 == 0 } - fmt.Println(u.Any(nums, isEven)) // true + nums := []int{1, 2, 4, 6, 8} + isEven := func(n int) bool { return n%2 == 0 } + fmt.Println(u.Any(nums, isEven)) // true } ``` diff --git a/docs/content/collections/contains.md b/docs/content/collections/contains.md index 3126012..ea8d429 100644 --- a/docs/content/collections/contains.md +++ b/docs/content/collections/contains.md @@ -9,13 +9,13 @@ date: 2022-03-21T13:30:29-04:00 package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 3, 5, 7, 9} + nums := []int{1, 3, 5, 7, 9} - fmt.Println(u.Contains(nums, 5)) // true + fmt.Println(u.Contains(nums, 5)) // true } ``` diff --git a/docs/content/collections/difference.md b/docs/content/collections/difference.md index 680d0a1..a2c9449 100644 --- a/docs/content/collections/difference.md +++ b/docs/content/collections/difference.md @@ -9,14 +9,14 @@ Returns a copy of the array with all instances of the values that are not presen package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 3, 5, 6, 7, 9} - reject := []int{9, 7, 5, 4} - - fmt.Println(u.Difference(nums, reject)) // {1, 3, 6} + nums := []int{1, 3, 5, 6, 7, 9} + reject := []int{9, 7, 5, 4} + + fmt.Println(u.Difference(nums, reject)) // {1, 3, 6} } ``` diff --git a/docs/content/collections/drop.md b/docs/content/collections/drop.md index c0c4ffd..d8d9f05 100644 --- a/docs/content/collections/drop.md +++ b/docs/content/collections/drop.md @@ -10,13 +10,13 @@ onward. package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5} + nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5} - fmt.Println(u.Drop(nums, 3)) // {1, 9, 2, 3, 7, 4, 6, 5} + fmt.Println(u.Drop(nums, 3)) // {1, 9, 2, 3, 7, 4, 6, 5} } ``` diff --git a/docs/content/collections/each.md b/docs/content/collections/each.md index 2e26cb9..b1ce0ab 100644 --- a/docs/content/collections/each.md +++ b/docs/content/collections/each.md @@ -9,17 +9,17 @@ date: 2022-03-21T13:30:59-04:00 package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - names := []string{"Alice", "Bob", "Charles"} - res := make([]string, 0) + names := []string{"Alice", "Bob", "Charles"} + res := make([]string, 0) - u.Each(names, func(n string) { - res = append(res, fmt.Sprintf("Hi %s", n)) - }) - fmt.Println(res) // {"Hi Alice", "Hi Bob", "Hi Charles"} + u.Each(names, func(n string) { + res = append(res, fmt.Sprintf("Hi %s", n)) + }) + fmt.Println(res) // {"Hi Alice", "Hi Bob", "Hi Charles"} } ``` diff --git a/docs/content/collections/filter.md b/docs/content/collections/filter.md index b3b2797..91c1e14 100644 --- a/docs/content/collections/filter.md +++ b/docs/content/collections/filter.md @@ -9,13 +9,13 @@ date: 2022-03-21T13:31:21-04:00 package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - isEven := func(n int) bool { return n%2 == 0 } - fmt.Println(u.Filter(nums, isEven)) // {0, 2, 4, 6, 8} + nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + isEven := func(n int) bool { return n%2 == 0 } + fmt.Println(u.Filter(nums, isEven)) // {0, 2, 4, 6, 8} } ``` diff --git a/docs/content/collections/find.md b/docs/content/collections/find.md index 1c5479a..3389e1b 100644 --- a/docs/content/collections/find.md +++ b/docs/content/collections/find.md @@ -11,16 +11,16 @@ element, and doesn't traverse the entire slice. package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{2, 4, 5, 6, 8, 0} - isOdd := func(n int) bool { return n%2 != 0 } + nums := []int{2, 4, 5, 6, 8, 0} + isOdd := func(n int) bool { return n%2 != 0 } - n, err := u.Find(nums, isOdd) - fmt.Println(n) // 5 - fmt.Println(err) // nil + n, err := u.Find(nums, isOdd) + fmt.Println(n) // 5 + fmt.Println(err) // nil } ``` diff --git a/docs/content/collections/flatmap.md b/docs/content/collections/flatmap.md index 80033fa..a0ead15 100644 --- a/docs/content/collections/flatmap.md +++ b/docs/content/collections/flatmap.md @@ -10,8 +10,8 @@ Flatmap flattens the input slice element into the new slice. FlatMap maps every package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { diff --git a/docs/content/collections/groupby.md b/docs/content/collections/groupby.md index 04cb548..bcb51a9 100644 --- a/docs/content/collections/groupby.md +++ b/docs/content/collections/groupby.md @@ -9,13 +9,13 @@ GroupBy splits a slice into a map[K][]V grouped by the result of the iterator fu package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []float64{1.3, 2.1, 2.4} - groupingFunc := func(n float64) int { return int(math.Floor(n)) } - res := u.GroupBy(nums, groupingFunc) // { 1: {1.3}, 2: {2.1, 2.4}} + nums := []float64{1.3, 2.1, 2.4} + groupingFunc := func(n float64) int { return int(math.Floor(n)) } + res := u.GroupBy(nums, groupingFunc) // { 1: {1.3}, 2: {2.1, 2.4}} } ``` diff --git a/docs/content/collections/intersection.md b/docs/content/collections/intersection.md index b7b4fdd..a82e051 100644 --- a/docs/content/collections/intersection.md +++ b/docs/content/collections/intersection.md @@ -11,8 +11,8 @@ package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main(){ @@ -21,4 +21,5 @@ func main(){ fmt.Println(u.Intersection(a, b)) // {3, 5} } -``` \ No newline at end of file +``` + diff --git a/docs/content/collections/last.md b/docs/content/collections/last.md index 5f1bb75..5289ec1 100644 --- a/docs/content/collections/last.md +++ b/docs/content/collections/last.md @@ -9,13 +9,13 @@ date: 2022-03-21T13:46:24-04:00 package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5} + nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5} - fmt.Println(u.Last(nums)) // 5 + fmt.Println(u.Last(nums)) // 5 } ``` diff --git a/docs/content/collections/map.md b/docs/content/collections/map.md index b9be2e4..ad63564 100644 --- a/docs/content/collections/map.md +++ b/docs/content/collections/map.md @@ -9,15 +9,15 @@ date: 2022-03-21T13:32:10-04:00 package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 2, 3} - toSquare := func(n int) int { - return n * n - } - fmt.Println(u.Map(nums, toSquare)) // {1, 4, 9} + nums := []int{1, 2, 3} + toSquare := func(n int) int { + return n * n + } + fmt.Println(u.Map(nums, toSquare)) // {1, 4, 9} } ``` diff --git a/docs/content/collections/max.md b/docs/content/collections/max.md index 3bf2ec5..cebb891 100644 --- a/docs/content/collections/max.md +++ b/docs/content/collections/max.md @@ -10,12 +10,12 @@ uses operator `<`. package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5} - fmt.Println(u.Max(nums)) // 9 + nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5} + fmt.Println(u.Max(nums)) // 9 } ``` diff --git a/docs/content/collections/min.md b/docs/content/collections/min.md index a8489c5..85523b9 100644 --- a/docs/content/collections/min.md +++ b/docs/content/collections/min.md @@ -10,12 +10,12 @@ uses operator `<`. package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5} - fmt.Println(u.Min(nums)) // 1 + nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5} + fmt.Println(u.Min(nums)) // 1 } ``` diff --git a/docs/content/collections/partition.md b/docs/content/collections/partition.md index af265f0..fe3d5a5 100644 --- a/docs/content/collections/partition.md +++ b/docs/content/collections/partition.md @@ -10,16 +10,16 @@ satisfy predicate. package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - isEven := func(n int) bool { return n%2 == 0 } + nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + isEven := func(n int) bool { return n%2 == 0 } - evens, odds := u.Partition(nums, isEven) - fmt.Println(evens) // {0, 2, 4, 6, 8} - fmt.Println(odds) // {1, 3, 5, 7, 9} + evens, odds := u.Partition(nums, isEven) + fmt.Println(evens) // {0, 2, 4, 6, 8} + fmt.Println(odds) // {1, 3, 5, 7, 9} } ``` diff --git a/docs/content/collections/reduce.md b/docs/content/collections/reduce.md index 311b9a9..c0ccbda 100644 --- a/docs/content/collections/reduce.md +++ b/docs/content/collections/reduce.md @@ -10,13 +10,13 @@ be returned by the reduction function. package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - sum := func(n, acc int) int { return n + acc } - fmt.Println(u.Reduce(nums, sum, 0)) // 45 + nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + sum := func(n, acc int) int { return n + acc } + fmt.Println(u.Reduce(nums, sum, 0)) // 45 } ``` diff --git a/docs/content/collections/sum.md b/docs/content/collections/sum.md index e8332f5..56cde6c 100644 --- a/docs/content/collections/sum.md +++ b/docs/content/collections/sum.md @@ -9,13 +9,13 @@ date: 2022-03-21T13:50:29-04:00 package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} + nums := []int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} - fmt.Println(u.Sum(nums)) // 45 + fmt.Println(u.Sum(nums)) // 45 } ``` diff --git a/docs/content/collections/unique.md b/docs/content/collections/unique.md index 4b1ba12..3f7b58f 100644 --- a/docs/content/collections/unique.md +++ b/docs/content/collections/unique.md @@ -9,13 +9,13 @@ date: 2022-04-12T17:18:04-04:00 package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - nums := []int{1, 4, 2, 5, 3, 1, 5, 2, 8, 9} - - fmt.Println(u.Unique(nums)) // 1, 4, 2, 5, 3, 8, 9 + nums := []int{1, 4, 2, 5, 3, 1, 5, 2, 8, 9} + + fmt.Println(u.Unique(nums)) // 1, 4, 2, 5, 3, 8, 9 } ``` diff --git a/docs/content/pipe/new_pipe.md b/docs/content/pipe/new_pipe.md index fb92af5..1083096 100644 --- a/docs/content/pipe/new_pipe.md +++ b/docs/content/pipe/new_pipe.md @@ -3,29 +3,30 @@ title: "NewPipe" date: 2021-12-31T13:11:41-04:00 --- +Calling `NewPipe` will cause all future method calls to return wrapped objects. +When you've finished the computation, call `Value` to retrieve the final value. -Calling `NewPipe` will cause all future method calls to return wrapped objects. When you've finished the computation, -call `Value` to retrieve the final value. - -Methods not returning a collection such as `Reduce`, `All`, `Any`, will break the chain and return `Value` instantly. +Methods not returning a collection such as `Reduce`, `All`, `Any`, will break the +chain and return `Value` instantly. ```go package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - sum := u.NewPipe([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}). - // filter even numbers from the slice - Filter(func(n int) bool { return n%2 == 0 }). - // square every number in the slice - Map(func(n int) int { return n * n }). - // reduce to the sum - Reduce(func(n, acc int) int { return n + acc }, 0) + sum := u.NewPipe([]int{1, 2, 3, 4, 5, 6, 7, 8, 9}). + // filter even numbers from the slice + Filter(func(n int) bool { return n%2 == 0 }). + // square every number in the slice + Map(func(n int) int { return n * n }). + // reduce to the sum + Reduce(func(n, acc int) int { return n + acc }, 0) - fmt.Println(sum) // 120 + fmt.Println(sum) // 120 } -``` \ No newline at end of file +``` + diff --git a/docs/content/usage/start.md b/docs/content/usage/start.md index aede265..1d7aff1 100644 --- a/docs/content/usage/start.md +++ b/docs/content/usage/start.md @@ -11,38 +11,40 @@ Install the library using go get github.com/rjNemo/underscore ``` -Please check out the [examples](https://github.com/rjNemo/underscore/tree/main/examples) to see how to use the library. +Please check out the [examples](https://github.com/rjNemo/underscore/tree/main/examples) +to see how to use the library. ```go package main import ( - "fmt" - u "github.com/rjNemo/underscore" + "fmt" + u "github.com/rjNemo/underscore" ) func main() { - numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} - // filter even numbers from the slice - evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 }) - // square every number in the slice - squares := u.Map(evens, func(n int) int { return n * n }) - // reduce to the sum - res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0) + numbers := []int{1, 2, 3, 4, 5, 6, 7, 8, 9} + // filter even numbers from the slice + evens := u.Filter(numbers, func(n int) bool { return n%2 == 0 }) + // square every number in the slice + squares := u.Map(evens, func(n int) int { return n * n }) + // reduce to the sum + res := u.Reduce(squares, func(n, acc int) int { return n + acc }, 0) - fmt.Println(res) // 120 + fmt.Println(res) // 120 } ``` ## Installation -These instructions will get you a copy of the project up and running on your local machine for development and testing -purposes. +These instructions will get you a copy of the project up and running on your local +machine for development and testing purposes. ### Prerequisites You need at least `go1.18` for development. The project is shipped with -a [Dockerfile](https://github.com/rjNemo/underscore/tree/main/Dockerfile) based on `go1.18`. +a [Dockerfile](https://github.com/rjNemo/underscore/tree/main/Dockerfile) based +on `go1.18`. If you prefer local development, navigate to the [official download page](https://go.dev/dl/) and install version `1.18` or beyond.