format docs

This commit is contained in:
Ruidy 2024-09-08 19:11:52 +02:00
parent bfac048fb4
commit eda818c6e0
No known key found for this signature in database
GPG key ID: E00F51288CB857CC
22 changed files with 143 additions and 137 deletions

View file

@ -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
}
```

View file

@ -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
}
```

View file

@ -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
}
```

View file

@ -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
}
```

View file

@ -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}
}
```

View file

@ -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}
}
```

View file

@ -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"}
}
```

View file

@ -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}
}
```

View file

@ -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
}
```

View file

@ -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() {

View file

@ -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}}
}
```

View file

@ -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}
}
```
```

View file

@ -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
}
```

View file

@ -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}
}
```

View file

@ -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
}
```

View file

@ -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
}
```

View file

@ -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}
}
```

View file

@ -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
}
```

View file

@ -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
}
```

View file

@ -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
}
```

View file

@ -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
}
```
```

View file

@ -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.