Skip to content

Commit 7601d3c

Browse files
committed
Initial commit
0 parents  commit 7601d3c

File tree

8 files changed

+266
-0
lines changed

8 files changed

+266
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
.DS_Store

.travis.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
language: go
2+
sudo: false
3+
4+
matrix:
5+
include:
6+
- go: 1.7
7+
- go: 1.8
8+
- go: 1.9
9+
- go: 1.10.x
10+
- go: 1.11.x
11+
- go: 1.12.x
12+
- go: 1.13.x
13+
- go: tip
14+
allow_failures:
15+
- go: tip
16+
env:
17+
- GO111MODULE=on
18+
before_install:
19+
- go get github.com/mattn/goveralls
20+
script:
21+
- $GOPATH/bin/goveralls -service=travis-ci
22+
- go get -t -v ./...
23+
- diff -u <(echo -n) <(gofmt -d .)
24+
- go vet $(go list ./... | grep -v /vendor/)
25+
- go test -v -race ./...
26+
- go test --bench . --benchmem=true

CONTRIBUTING.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Contributing
2+
3+
## Must follow the guide for issues
4+
- Use the search tool before opening a new issue.
5+
- Please provide source code and stack trace if you found a bug.
6+
- Please review the existing issues and then provide feedback
7+
8+
## Pull Request Process
9+
- Before sending PR, create issue and discuss about the changes
10+
- You MUST send pull requests against `dev` branch
11+
- It should pass all tests in the available continuous integrations systems such as TravisCI.
12+
- You should add/modify tests to cover your proposed code changes.
13+
- If your pull request contains a new feature, please document it on the README.

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# The MIT License (MIT)
2+
3+
Copyright (c) 2020 Saddam H <thedevsaddam@gmail.com>
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
13+
> all 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
21+
> THE SOFTWARE.

README.md

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
iter
2+
---
3+
[![Build Status](https://travis-ci.org/thedevsaddam/iter.svg?branch=master)](https://travis-ci.org/thedevsaddam/iter)
4+
[![Project status](https://img.shields.io/badge/version-v1-green.svg)](https://github.com/thedevsaddam/iter/releases)
5+
[![Go Report Card](https://goreportcard.com/badge/github.com/thedevsaddam/iter)](https://goreportcard.com/report/github.com/thedevsaddam/iter)
6+
[![Coverage Status](https://coveralls.io/repos/github/thedevsaddam/iter/badge.svg?branch=master)](https://coveralls.io/github/thedevsaddam/iter)
7+
[![GoDoc](https://godoc.org/github.com/thedevsaddam/iter?status.svg)](https://pkg.go.dev/github.com/thedevsaddam/iter)
8+
[![License](https://img.shields.io/dub/l/vibe-d.svg)](LICENSE.md)
9+
10+
Iter provides functionality like Python's range function to iterate over numbers and letters
11+
12+
### Installation
13+
14+
Install the package using
15+
```go
16+
$ go get github.com/thedevsaddam/iter
17+
```
18+
19+
### Usage
20+
21+
To use the package import it in your `*.go` code
22+
```go
23+
import "github.com/thedevsaddam/iter"
24+
```
25+
26+
Let's see a quick example:
27+
28+
```go
29+
package main
30+
31+
import (
32+
"fmt"
33+
34+
"github.com/thedevsaddam/iter"
35+
)
36+
37+
func main() {
38+
// sequence: 0-9
39+
for v := range iter.N(10) {
40+
fmt.Printf("%d ", v)
41+
}
42+
fmt.Println()
43+
// output: 0 1 2 3 4 5 6 7 8 9
44+
45+
// sequence: 5-9
46+
for v := range iter.N(5, 10) {
47+
fmt.Printf("%d ", v)
48+
}
49+
fmt.Println()
50+
// output: 5 6 7 8 9
51+
52+
// sequence: 1-9, increment by 2
53+
for v := range iter.N(5, 10, 2) {
54+
fmt.Printf("%d ", v)
55+
}
56+
fmt.Println()
57+
// output: 5 7 9
58+
59+
// sequence: a-e
60+
for v := range iter.L('a', 'e') {
61+
fmt.Printf("%s ", string(v))
62+
}
63+
fmt.Println()
64+
// output: a b c d e
65+
}
66+
67+
```
68+
69+
70+
## Bugs and Issues
71+
72+
If you encounter any bugs or issues, feel free to [open an issue at
73+
github](https://github.com/thedevsaddam/iter/issues).
74+
75+
Also, you can shoot me an email to
76+
<mailto:thedevsaddam@gmail.com> for hugs or bugs.
77+
78+
## Contribution
79+
If you are interested to make the package better please send pull requests or create an issue so that others can fix.
80+
[Read the contribution guide here](CONTRIBUTING.md)
81+
82+
83+
## License
84+
The **iter** is an open-source software licensed under the [MIT License](LICENSE.md).

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module github.com/thedevsaddam/iter
2+
3+
go 1.12

range.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Package iter provides functionality like Python's range function
2+
// to iterate over numbers and letters
3+
package iter
4+
5+
// N mimic the python range function.
6+
// N(10) -> generate a sequence from 0 to 9.
7+
// N(3, 10) -> generate a sequence from 3 to 9.
8+
// N(3, 10, 2) -> generate a sequence from 3 to 9 by incrementing value by 2.
9+
func N(in ...int) <-chan int {
10+
ch := make(chan int)
11+
var start, end int
12+
step := 1
13+
switch len(in) {
14+
case 0:
15+
close(ch)
16+
return ch
17+
case 1:
18+
if in[0] > 0 {
19+
end = in[0]
20+
}
21+
case 2:
22+
if in[0] > 0 {
23+
start = in[0]
24+
}
25+
if in[0] > 0 {
26+
end = in[1]
27+
}
28+
case 3:
29+
if in[0] > 0 {
30+
start = in[0]
31+
}
32+
if in[1] > 0 {
33+
end = in[1]
34+
}
35+
if in[2] > 0 {
36+
step = in[2]
37+
}
38+
39+
}
40+
go func(yield chan<- int) {
41+
for i := start; i < end; i += step {
42+
yield <- i
43+
}
44+
close(yield)
45+
}(ch)
46+
return ch
47+
}
48+
49+
// L generate a range of letters, such as: a-z or A-Z.
50+
func L(start, end byte) <-chan byte {
51+
ch := make(chan byte)
52+
go func(yield chan<- byte) {
53+
for i := start; i <= end; i++ {
54+
yield <- i
55+
}
56+
close(yield)
57+
}(ch)
58+
return ch
59+
}

range_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package iter
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestN(t *testing.T) {
8+
9+
t.Run("Should do nothing", func(t *testing.T) {
10+
match := 0
11+
for n := range N() {
12+
if match != n {
13+
t.Fail()
14+
}
15+
match++
16+
}
17+
})
18+
19+
t.Run("Should generate sequence 1-9", func(t *testing.T) {
20+
match := 1
21+
for n := range N(1, 10) {
22+
if match != n {
23+
t.Fail()
24+
}
25+
match++
26+
}
27+
})
28+
29+
t.Run("Should generate sequence 0-9", func(t *testing.T) {
30+
match := 0
31+
for n := range N(10) {
32+
if match != n {
33+
t.Fail()
34+
}
35+
match++
36+
}
37+
})
38+
39+
t.Run("Should generate sequence 1-9 by step+=2", func(t *testing.T) {
40+
match := 1
41+
for n := range N(1, 10, 2) {
42+
if match != n {
43+
t.Fail()
44+
}
45+
match += 2
46+
}
47+
})
48+
}
49+
50+
func TestL(t *testing.T) {
51+
output := ""
52+
for c := range L('a', 'e') {
53+
output += string(c)
54+
}
55+
if output != "abcde" {
56+
t.Fail()
57+
}
58+
}

0 commit comments

Comments
 (0)