Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,4 @@ Icon
Network Trash Folder
Temporary Items
.apdisk
docs/public
7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,8 @@ build:
test: build
docker run --name underscore --rm -i -t underscore sh -c $(TEST)

doc:
cd docs && hugo server -D
docs:
cd docs && hugo server -D

build-docs:
cd docs && hugo --gc --minify
46 changes: 0 additions & 46 deletions docs/assets/sass/_variables.sass

This file was deleted.

35 changes: 24 additions & 11 deletions docs/config.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,25 @@
baseURL = 'http://example.org/'
languageCode = 'en-us'
title = 'Underscore'
theme = "compose" # edit this if you'ld rather use a fork of this repo
baseURL = "http://localhost"
title = "underscore"
theme = "hugo-geekdoc"

pluralizeListTitles = false

# Geekdoc required configuration
pygmentsUseClasses = true
pygmentsCodeFences = true
disablePathToLower = true

# Required if you want to render robots.txt template
enableRobotsTXT = true
[outputs]
home = ["HTML", "RSS","JSON"]
[params]
logo.lightMode = "logo.png"
logo.darkMode = "logo.png"
author.name = "Ruidy"
author.url = "https://github.com/rjNemo"

# Needed for mermaid shortcodes
[markup]
[markup.goldmark.renderer]
# Needed for mermaid shortcode
unsafe = true
[markup.tableOfContents]
startLevel = 1
endLevel = 9

[taxonomies]
tag = "tags"
44 changes: 44 additions & 0 deletions docs/content/_index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
title: underscore
---

![underscore](https://socialify.git.ci/rjNemo/underscore/image?description=1&font=Raleway&language=1&logo=https%3A%2F%2Fgithub.com%2FrjNemo%2Funderscore%2Fblob%2Fmain%2Fdocs%2Fstatic%2Flogo.png%3Fraw%3Dtrue&name=1&pattern=Floating%20Cogs&theme=Light)

![License](https://img.shields.io/github/license/rjNemo/underscore?style=for-the-badge)
![Go version](https://img.shields.io/github/go-mod/go-version/rjNemo/underscore?style=for-the-badge)
![test coverage](https://img.shields.io/codecov/c/github/rjNemo/underscore?style=for-the-badge)

`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 brought by `go1.18`.

## Quick Start

Install the library using

```shell
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.

```go
package main

import (
"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)

fmt.Println(res) // 120
}
```
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
---
title: "Chaining"
title: "Of"
date: 2021-12-31T13:11:41-04:00
---

## Of

Calling `Of` will cause all future method calls to return wrapped objects. When you've finished the computation,
call `Value` to retrieve the final value.
Expand Down
23 changes: 23 additions & 0 deletions docs/content/collections/all.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
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.

```go
package main

import (
"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
}
```
22 changes: 22 additions & 0 deletions docs/content/collections/any.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: "Any"
date: 2022-03-21T13:26:01-04:00
---

`Any` returns true if any of the values in the slice pass the predicate truth test. Short-circuits and stops traversing
the slice if a true element is found.

```go
package main

import (
"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
}
```
21 changes: 21 additions & 0 deletions docs/content/collections/contains.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: "Contains"
date: 2022-03-21T13:30:29-04:00
---

`Contains` returns true if the value is present in the slice.

```go
package main

import (
"fmt"
u "github.com/rjNemo/underscore"
)

func main() {
nums := []int{1, 3, 5, 7, 9}

fmt.Println(u.Contains(nums, 5)) // true
}
```
22 changes: 22 additions & 0 deletions docs/content/collections/drop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
title: "Drop"
date: 2022-03-21T13:48:21-04:00
---

`Drop` returns the rest of the elements in a slice. Pass an index to return the values of the slice from that index
onward.

```go
package main

import (
"fmt"
u "github.com/rjNemo/underscore"
)

func main() {
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}
}
```
25 changes: 25 additions & 0 deletions docs/content/collections/each.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
---
title: "Each"
date: 2022-03-21T13:30:59-04:00
---

`Each` iterates over a slice of elements, yielding each in turn to an action function.

```go
package main

import (
"fmt"
u "github.com/rjNemo/underscore"
)

func main() {
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"}
}
```
21 changes: 21 additions & 0 deletions docs/content/collections/filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: "Filter"
date: 2022-03-21T13:31:21-04:00
---

`Filter` looks through each value in the slice, returning a slice of all the values that pass a truth test (predicate).

```go
package main

import (
"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}
}
```
26 changes: 26 additions & 0 deletions docs/content/collections/find.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
title: "Find"
date: 2022-03-21T13:31:40-04:00
---

Find looks through each value in the slice, returning the first one that passes a truth test (predicate), or the default
value for the type and an error if no value passes the test. The function returns as soon as it finds an acceptable
element, and doesn't traverse the entire slice.

```go
package main

import (
"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 }

n, err := u.Find(nums, isOdd)
fmt.Println(n) // 5
fmt.Println(err) // nil
}
```
21 changes: 21 additions & 0 deletions docs/content/collections/last.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: "Last"
date: 2022-03-21T13:46:24-04:00
---

`Last` returns the last element of the slice.

```go
package main

import (
"fmt"
u "github.com/rjNemo/underscore"
)

func main() {
nums := []int{1, 9, 2, 8, 3, 7, 4, 6, 5}

fmt.Println(u.Last(nums)) // 5
}
```
23 changes: 23 additions & 0 deletions docs/content/collections/map.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: "Map"
date: 2022-03-21T13:32:10-04:00
---

`Map` produces a new slice of values by mapping each value in the slice through a transform function.

```go
package main

import (
"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}
}
```
21 changes: 21 additions & 0 deletions docs/content/collections/max.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
title: "Max"
date: 2022-03-21T13:32:43-04:00
---

`Max` returns the maximum value in the slice. This function can currently only compare numbers reliably. This function
uses operator `<`.

```go
package main

import (
"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
}
```
Loading