Skip to content

Commit

Permalink
add Contains operation (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
psampaz authored Nov 27, 2019
1 parent c93bbe8 commit c12fe89
Show file tree
Hide file tree
Showing 4 changed files with 1,280 additions and 22 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
### Added
- Min operation
- Max operation
- Contains operation
52 changes: 30 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ Type-safe functions for common Go slice operations.

| | Min | Max | Contains | Reverse | Shuffle | Clone | Deduplicate | Insert | Delete |
| ---------- | --- | --- | -------- | ------- | ------- | ----- | ----------- | ------ | ------ |
| bool ||| - | - | - | - | - | - | - |
| byte ||| - | - | - | - | - | - | - |
| complex64 ||| - | - | - | - | - | - | - |
| complex128 ||| - | - | - | - | - | - | - |
| float32 ||| - | - | - | - | - | - | - |
| float64 ||| - | - | - | - | - | - | - |
| int ||| - | - | - | - | - | - | - |
| int8 ||| - | - | - | - | - | - | - |
| int16 ||| - | - | - | - | - | - | - |
| int32 ||| - | - | - | - | - | - | - |
| int64 ||| - | - | - | - | - | - | - |
| rune ||| - | - | - | - | - | - | - |
| string ||| - | - | - | - | - | - | - |
| uint ||| - | - | - | - | - | - | - |
| uint8 ||| - | - | - | - | - | - | - |
| uint16 ||| - | - | - | - | - | - | - |
| uint32 ||| - | - | - | - | - | - | - |
| uint64 ||| - | - | - | - | - | - | - |
| uintptr ||| - | - | - | - | - | - | - |
| bool ||| | - | - | - | - | - | - |
| byte ||| | - | - | - | - | - | - |
| complex64 ||| | - | - | - | - | - | - |
| complex128 ||| | - | - | - | - | - | - |
| float32 ||| | - | - | - | - | - | - |
| float64 ||| | - | - | - | - | - | - |
| int ||| | - | - | - | - | - | - |
| int8 ||| | - | - | - | - | - | - |
| int16 ||| | - | - | - | - | - | - |
| int32 ||| | - | - | - | - | - | - |
| int64 ||| | - | - | - | - | - | - |
| rune ||| | - | - | - | - | - | - |
| string ||| | - | - | - | - | - | - |
| uint ||| | - | - | - | - | - | - |
| uint8 ||| | - | - | - | - | - | - |
| uint16 ||| | - | - | - | - | - | - |
| uint32 ||| | - | - | - | - | - | - |
| uint64 ||| | - | - | - | - | - | - |
| uintptr ||| | - | - | - | - | - | - |

# Examples

Expand All @@ -44,15 +44,23 @@ Type-safe functions for common Go slice operations.
Min returns the minimum value of a slice or an error in case of a nil or empty slice.
```go
a := []int{1, 2, 3, 0, 4, 5}
min, err := slice.MinInt(a)
min, err := slice.MinInt(a) // 0, nil
```

## Max

Max returns the maximum value of a slice or an error in case of a nil or empty slice.
```go
a := []int{1, 2, 3, 0, 4, 5}
max, err := slice.MaxInt(a)
max, err := slice.MaxInt(a) // 5, nil
```

## Contains

Contains checks if a specific value exists in a slice.
```go
a := []int{"a","b","c","d"}
b := "c"
exists := slice.Contains(a, "c") // true
```

# Contributing
Expand Down
248 changes: 248 additions & 0 deletions contains.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
package slice

// ContainsBool checks if a value exists in a bool slice
func ContainsBool(a []bool, x bool) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsByte checks if a value exists in a byte slice
func ContainsByte(a []byte, x byte) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsComplex64 checks if a value exists in a complex64 slice
func ContainsComplex64(a []complex64, x complex64) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsComplex128 checks if a value exists in a complex128 slice
func ContainsComplex128(a []complex128, x complex128) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsFloat32 checks if a value exists in a float32 slice
func ContainsFloat32(a []float32, x float32) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsFloat64 checks if a value exists in a float64 slice
func ContainsFloat64(a []float64, x float64) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsInt checks if a value exists in an int slice
func ContainsInt(a []int, x int) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsInt16 checks if a value exists in an int16 slice
func ContainsInt16(a []int16, x int16) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsInt32 checks if a value exists in an int32 slice
func ContainsInt32(a []int32, x int32) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsInt64 checks if a value exists in an int64 slice
func ContainsInt64(a []int64, x int64) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsInt8 checks if a value exists in an int8 slice
func ContainsInt8(a []int8, x int8) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsRune checks if a value exists in a rune slice
func ContainsRune(a []rune, x rune) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsString checks if a value exists in a string slice
func ContainsString(a []string, x string) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsUint checks if a value exists in a uint slice
func ContainsUint(a []uint, x uint) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsUint8 checks if a value exists in a uint8 slice
func ContainsUint8(a []uint8, x uint8) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsUint16 checks if a value exists in a uint16 slice
func ContainsUint16(a []uint16, x uint16) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsUint32 checks if a value exists in a uint32 slice
func ContainsUint32(a []uint32, x uint32) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsUint64 checks if a value exists in a uint64 slice
func ContainsUint64(a []uint64, x uint64) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}

// ContainsUintptr checks if a value exists in a uintptr slice
func ContainsUintptr(a []uintptr, x uintptr) bool {
if len(a) == 0 {
return false
}
for k := range a {
if a[k] == x {
return true
}
}
return false
}
Loading

0 comments on commit c12fe89

Please sign in to comment.