Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thedevsaddam committed Apr 24, 2020
0 parents commit 7601d3c
Show file tree
Hide file tree
Showing 8 changed files with 266 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.idea
.DS_Store
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: go
sudo: false

matrix:
include:
- go: 1.7
- go: 1.8
- go: 1.9
- go: 1.10.x
- go: 1.11.x
- go: 1.12.x
- go: 1.13.x
- go: tip
allow_failures:
- go: tip
env:
- GO111MODULE=on
before_install:
- go get github.com/mattn/goveralls
script:
- $GOPATH/bin/goveralls -service=travis-ci
- go get -t -v ./...
- diff -u <(echo -n) <(gofmt -d .)
- go vet $(go list ./... | grep -v /vendor/)
- go test -v -race ./...
- go test --bench . --benchmem=true
13 changes: 13 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Contributing

## Must follow the guide for issues
- Use the search tool before opening a new issue.
- Please provide source code and stack trace if you found a bug.
- Please review the existing issues and then provide feedback

## Pull Request Process
- Before sending PR, create issue and discuss about the changes
- You MUST send pull requests against `dev` branch
- It should pass all tests in the available continuous integrations systems such as TravisCI.
- You should add/modify tests to cover your proposed code changes.
- If your pull request contains a new feature, please document it on the README.
21 changes: 21 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# The MIT License (MIT)

Copyright (c) 2020 Saddam H <thedevsaddam@gmail.com>

> Permission is hereby granted, free of charge, to any person obtaining a copy
> of this software and associated documentation files (the "Software"), to deal
> in the Software without restriction, including without limitation the rights
> to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> copies of the Software, and to permit persons to whom the Software is
> furnished to do so, subject to the following conditions:
>
> The above copyright notice and this permission notice shall be included in
> all copies or substantial portions of the Software.
>
> THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
> AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> THE SOFTWARE.
84 changes: 84 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
iter
---
[![Build Status](https://travis-ci.org/thedevsaddam/iter.svg?branch=master)](https://travis-ci.org/thedevsaddam/iter)
[![Project status](https://img.shields.io/badge/version-v1-green.svg)](https://github.com/thedevsaddam/iter/releases)
[![Go Report Card](https://goreportcard.com/badge/github.com/thedevsaddam/iter)](https://goreportcard.com/report/github.com/thedevsaddam/iter)
[![Coverage Status](https://coveralls.io/repos/github/thedevsaddam/iter/badge.svg?branch=master)](https://coveralls.io/github/thedevsaddam/iter)
[![GoDoc](https://godoc.org/github.com/thedevsaddam/iter?status.svg)](https://pkg.go.dev/github.com/thedevsaddam/iter)
[![License](https://img.shields.io/dub/l/vibe-d.svg)](LICENSE.md)

Iter provides functionality like Python's range function to iterate over numbers and letters

### Installation

Install the package using
```go
$ go get github.com/thedevsaddam/iter
```

### Usage

To use the package import it in your `*.go` code
```go
import "github.com/thedevsaddam/iter"
```

Let's see a quick example:

```go
package main

import (
"fmt"

"github.com/thedevsaddam/iter"
)

func main() {
// sequence: 0-9
for v := range iter.N(10) {
fmt.Printf("%d ", v)
}
fmt.Println()
// output: 0 1 2 3 4 5 6 7 8 9

// sequence: 5-9
for v := range iter.N(5, 10) {
fmt.Printf("%d ", v)
}
fmt.Println()
// output: 5 6 7 8 9

// sequence: 1-9, increment by 2
for v := range iter.N(5, 10, 2) {
fmt.Printf("%d ", v)
}
fmt.Println()
// output: 5 7 9

// sequence: a-e
for v := range iter.L('a', 'e') {
fmt.Printf("%s ", string(v))
}
fmt.Println()
// output: a b c d e
}

```


## Bugs and Issues

If you encounter any bugs or issues, feel free to [open an issue at
github](https://github.com/thedevsaddam/iter/issues).

Also, you can shoot me an email to
<mailto:thedevsaddam@gmail.com> for hugs or bugs.

## Contribution
If you are interested to make the package better please send pull requests or create an issue so that others can fix.
[Read the contribution guide here](CONTRIBUTING.md)


## License
The **iter** is an open-source software licensed under the [MIT License](LICENSE.md).
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/thedevsaddam/iter

go 1.12
59 changes: 59 additions & 0 deletions range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Package iter provides functionality like Python's range function
// to iterate over numbers and letters
package iter

// N mimic the python range function.
// N(10) -> generate a sequence from 0 to 9.
// N(3, 10) -> generate a sequence from 3 to 9.
// N(3, 10, 2) -> generate a sequence from 3 to 9 by incrementing value by 2.
func N(in ...int) <-chan int {
ch := make(chan int)
var start, end int
step := 1
switch len(in) {
case 0:
close(ch)
return ch
case 1:
if in[0] > 0 {
end = in[0]
}
case 2:
if in[0] > 0 {
start = in[0]
}
if in[0] > 0 {
end = in[1]
}
case 3:
if in[0] > 0 {
start = in[0]
}
if in[1] > 0 {
end = in[1]
}
if in[2] > 0 {
step = in[2]
}

}
go func(yield chan<- int) {
for i := start; i < end; i += step {
yield <- i
}
close(yield)
}(ch)
return ch
}

// L generate a range of letters, such as: a-z or A-Z.
func L(start, end byte) <-chan byte {
ch := make(chan byte)
go func(yield chan<- byte) {
for i := start; i <= end; i++ {
yield <- i
}
close(yield)
}(ch)
return ch
}
58 changes: 58 additions & 0 deletions range_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package iter

import (
"testing"
)

func TestN(t *testing.T) {

t.Run("Should do nothing", func(t *testing.T) {
match := 0
for n := range N() {
if match != n {
t.Fail()
}
match++
}
})

t.Run("Should generate sequence 1-9", func(t *testing.T) {
match := 1
for n := range N(1, 10) {
if match != n {
t.Fail()
}
match++
}
})

t.Run("Should generate sequence 0-9", func(t *testing.T) {
match := 0
for n := range N(10) {
if match != n {
t.Fail()
}
match++
}
})

t.Run("Should generate sequence 1-9 by step+=2", func(t *testing.T) {
match := 1
for n := range N(1, 10, 2) {
if match != n {
t.Fail()
}
match += 2
}
})
}

func TestL(t *testing.T) {
output := ""
for c := range L('a', 'e') {
output += string(c)
}
if output != "abcde" {
t.Fail()
}
}

0 comments on commit 7601d3c

Please sign in to comment.