Skip to content

Commit

Permalink
refactor and use generic types
Browse files Browse the repository at this point in the history
  • Loading branch information
yaa110 committed Jul 8, 2024
1 parent 21ccac3 commit b060e2c
Show file tree
Hide file tree
Showing 20 changed files with 390 additions and 263 deletions.
16 changes: 0 additions & 16 deletions .github/workflows/ci.yml

This file was deleted.

32 changes: 32 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: Test
on:
push:
branches:
- "*"
paths-ignore:
- "README.md"
tags-ignore:
- "*"
env:
GO_VERSION: "1.22.5"
GOFLAGS: -buildvcs=false
GOPROXY: https://proxy.golang.org,direct
jobs:
lint:
name: Run make lint
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- run: make lint
test:
name: Run make test
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ env.GO_VERSION }}
- run: make test
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
/vendor/
/Godeps/
go.sum
/bin

### Vim ###
# Swap
Expand Down
34 changes: 34 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
run:
timeout: 5m
allow-parallel-runners: true

issues:
exclude-use-default: false
linters:
disable-all: true
enable:
- bidichk
- bodyclose
- containedctx
- errcheck
- exportloopref
- ginkgolinter
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- gosimple
- govet
- inamedparam
- ineffassign
- misspell
- nakedret
- prealloc
- staticcheck
- tenv
- typecheck
- unconvert
- unparam
- unused
- whitespace
28 changes: 28 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
.PHONY: fmt
fmt:
go fmt ./...

.PHONY: vet
vet:
go vet ./...

.PHONY: test
test:
go test -race -cover ./...

.PHONY: lint
lint: golangci-lint
$(GOLANGCI_LINT) run

LOCALBIN ?= $(shell pwd)/bin
$(LOCALBIN):
mkdir -p $(LOCALBIN)

GOLANGCI_LINT = $(shell pwd)/bin/golangci-lint
GOLANGCI_LINT_VERSION ?= v1.59.1
.PHONY: golangci-lint
golangci-lint:
@[ -f $(GOLANGCI_LINT) ] || { \
set -e ;\
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell dirname $(GOLANGCI_LINT)) $(GOLANGCI_LINT_VERSION) ;\
}
30 changes: 20 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Goterator

[![Test and Build](https://github.com/yaa110/goterator/workflows/Test%20and%20Build/badge.svg)](https://github.com/yaa110/goterator/actions?query=workflow%3A"Test+and+Build") [![PkgGoDev](https://pkg.go.dev/badge/github.com/yaa110/goterator)](https://pkg.go.dev/github.com/yaa110/goterator) [![GoDoc](https://img.shields.io/badge/godoc-goterator-blue)](https://godoc.org/github.com/yaa110/goterator) [![Go Report](https://goreportcard.com/badge/github.com/yaa110/goterator)](https://goreportcard.com/report/github.com/yaa110/goterator) [![Coverage](https://gocover.io/_badge/github.com/yaa110/goterator)](https://gocover.io/github.com/yaa110/goterator)
[![Test](https://github.com/yaa110/goterator/actions/workflows/test.yaml/badge.svg)](https://github.com/yaa110/goterator/actions/workflows/test.yaml) [![PkgGoDev](https://pkg.go.dev/badge/github.com/yaa110/goterator)](https://pkg.go.dev/github.com/yaa110/goterator) [![Go Report](https://goreportcard.com/badge/github.com/yaa110/goterator)](https://goreportcard.com/report/github.com/yaa110/goterator)

Iterator implementation for Golang to provide map and reduce functionalities.
**Lazy iterator** implementation for Golang to provide map and reduce functionalities.

## Package

Expand All @@ -13,20 +13,32 @@ import (
)
```

## Example

```go
generator := generator.NewSlice([]string{"a", "b", "c"})

// values = []string["A", "B", "C"]
values := goterator.
New(generator).
Map(strings.ToUpper).
Collect()
```

## Getting Started

- Create a generator from slices

```go
words := []interface{}{"an", "example", "of", "goterator"}
words := []string{"an", "example", "of", "goterator"}

gen := generator.NewSlice(words)
```

- Create a generator from channels

```go
chn := make(chan interface{}, 4)
chn := make(chan string, 4)
chn <- "an"
chn <- "example"
chn <- "of"
Expand Down Expand Up @@ -54,7 +66,7 @@ func (g *TestGenerator) Next() bool {
return true
}

func (g *TestGenerator) Value() interface{} {
func (g *TestGenerator) Value() string {
return g.value
}

Expand All @@ -68,11 +80,9 @@ gen := &TestGenerator{
- Iterate over generators

```go
lengths := goterator.New(gen).Map(func(word interface{}) interface{} {
return len(word.(string))
lengths := goterator.New(gen).Map(func(word string) string {
return len(word)
}).Collect()

assert.Equal([]interface{}{2, 7, 2, 9}, lengths)
assert.Equal([]string{2, 7, 2, 9}, lengths)
```

Please for more information about mappers and reducers (consumers) check the [documentation](https://godoc.org/github.com/yaa110/goterator).
Loading

0 comments on commit b060e2c

Please sign in to comment.