rslices

package
v1.1.18 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 14, 2024 License: MIT Imports: 3 Imported by: 0

README

RSlices

Overview

Package with slices functions.

Content

Sum

Sums the values in a collection. If collection is empty 0 is returned.

package main

import (
    "fmt"
    
    "github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        s32 = []float32{1, 2, 3}
        s64 = []float64{1, 2, 3}
    )
    
    resFloats32 := rslices.Sum(s32)
    fmt.Println(resFloats32) // 6
    
    resFloats64 := rslices.Sum(s64)
    fmt.Println(resFloats64) // 6
}
Mul

Multiplying slice by slice.

package main

import (
    "fmt"
    
    "github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        s32_1 = []float32{1, 2, 3}
        s32_2 = []float32{2, 2, 2}
    
        s64_1 = []float64{1, 2, 3}
        s64_2 = []float64{2, 2, 2}
    )
    
    rslices.Mul(s32_1, s32_2)
    fmt.Println(s32_1) // [2 4 6]
    
    rslices.Mul(s64_1, s64_2)
    fmt.Println(s64_1) // [2 4 6]
}
MulNum

Multiplying slice by number.

package main

import (
    "fmt"
    
    "github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        s32_1          = []float32{1, 2, 3}
        num_32 float32 = 2
        
        s64_1          = []float64{1, 2, 3}
        num_64 float64 = 2
    )
    
    rslices.MulNum(s32_1, num_32)
    fmt.Println(s32_1) // [2 4 6] 
    
    rslices.MulNum(s64_1, num_64)
    fmt.Println(s64_1) // [2 4 6]
}
Add

Adding slice to slice.

package main

import (
    "fmt"
    
    "github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        s32_1 = []float32{1, 2, 3}
        s32_2 = []float32{2, 2, 2}
    
        s64_1 = []float64{1, 2, 3}
        s64_2 = []float64{2, 2, 2}
    )
    
    rslices.Add(s32_1, s32_2)
    fmt.Println(s32_1) // [3 4 5]
    
    rslices.Add(s64_1, s64_2)
    fmt.Println(s64_1) // [3 4 5]
}
AddNum

Adding slice by number.

package main

import (
    "fmt"
    
    "github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        s32_1          = []float32{1, 2, 3}
        num_32 float32 = 2
    
        s64_1          = []float64{1, 2, 3}
        num_64 float64 = 2
    )
    
    rslices.AddNum(s32_1, num_32)
    fmt.Println(s32_1) // [3 4 5] 
    
    rslices.AddNum(s64_1, num_64)
    fmt.Println(s64_1) // [3 4 5]
}
MaximumNum

Set 0 if a < 0.

package main

import (
    "fmt"
    
    "github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        s32_1          = []float32{1, 2, 3}
        num_32 float32 = 2
    
        s64_1          = []float64{1, 2, 3}
        num_64 float64 = 2
    )
    
    rslices.MaximumNum(s32_1, num_32)
    fmt.Println(s32_1) // [0 2 3]
    
    rslices.MaximumNum(s64_1, num_64)
    fmt.Println(s64_1) // [0 2 3]
}
IsIntersect

Return ok or not for intersection slice.

package main

import (
    "fmt"
    
    "github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        ints1 = []int{1, 2, 3}
        ints2 = []int{3, 4, 5}
    
        floats1 = []float64{1.1, 2.2, 3.3}
        floats2 = []float64{11.1, 22.2, 44.4}
    
        str1 = []string{"aaa", "bbb", "ccc"}
        str2 = []string{"aaa", "bbb", "ddd"}
    )
    
    resInts := rslices.IsIntersect(ints1, ints2)
    fmt.Println(resInts) // true
    
    resFloats := rslices.IsIntersect(floats1, floats2)
    fmt.Println(resFloats) // false
    
    resStrs := rslices.IsIntersect(str1, str2)
    fmt.Println(resStrs) // true
}
Intersection

Find intersection of two arrays. Returns new slice.

package main

import (
    "fmt"

	"github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        intsL = []int{1, 2, 3}
        intsR = []int{1, 2, 4}
        
        floatsL = []float64{1.1, 2.2, 3.3}
        floatsR = []float64{1.1, 2.2, 4.4}
        
        strL = []string{"aaa", "bbb", "ccc"}
        strR = []string{"aaa", "bbb", "ddd"}
    )
    
    resInts := rslices.Intersection(intsL, intsR)
    fmt.Println(resInts) // [1 2]
    
    resFloats := rslices.Intersection(floatsL, floatsR)
    fmt.Println(resFloats) // [1.1 2.2]
    
    resStrs := rslices.Intersection(strL, strR)
    fmt.Println(resStrs) // [aaa bbb]
}
Concat

Concatenation of multiple slices.

package main

import (
    "fmt"

	"github.com/ruauka/tools-go/rslices"
)

func main() {
    var (
        ints1 = []int{1, 2, 3}
        ints2 = []int{4, 5, 6}
        ints3 = []int{7, 8, 9}
    
        strs1 = []string{"1", "2", "3"}
        strs2 = []string{"4", "5", "6"}
        strs3 = []string{"7", "8", "9"}
    )
    
    ints := rslices.Concat(ints1, ints2, ints3)
    fmt.Println(ints) // [1 2 3 4 5 6 7 8 9]
    
    strs := rslices.Concat(strs1, strs2, strs3)
    fmt.Println(strs) // [1 2 3 4 5 6 7 8 9]
}

Documentation

Overview

Package rslices - slices functions.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Add

func Add[T constraints.Float](x, y []T)

Add - adding slice x to slice y.

Example
var (
	s32_1 = []float32{1, 2, 3}
	s32_2 = []float32{2, 2, 2}

	s64_1 = []float64{1, 2, 3}
	s64_2 = []float64{2, 2, 2}
)

Add(s32_1, s32_2)
fmt.Println(s32_1)

Add(s64_1, s64_2)
fmt.Println(s64_1)
Output:

[3 4 5]
[3 4 5]

func AddNum

func AddNum[S ~[]E, E constraints.Float](x S, a E)

AddNum - adding slice x by a.

Example
var (
	s32_1          = []float32{1, 2, 3}
	num_32 float32 = 2

	s64_1          = []float64{1, 2, 3}
	num_64 float64 = 2
)

AddNum(s32_1, num_32)
fmt.Println(s32_1)

AddNum(s64_1, num_64)
fmt.Println(s64_1)
Output:

[3 4 5]
[3 4 5]

func Concat added in v1.1.14

func Concat[S ~[]E, E any](collections ...S) S

Concat - concatenation two slices.

Example
var (
	ints1 = []int{1, 2, 3}
	ints2 = []int{4, 5, 6}
	ints3 = []int{7, 8, 9}

	strs1 = []string{"1", "2", "3"}
	strs2 = []string{"4", "5", "6"}
	strs3 = []string{"7", "8", "9"}
)

ints := Concat(ints1, ints2, ints3)
fmt.Println(ints)

strs := Concat(strs1, strs2, strs3)
fmt.Println(strs)
Output:

[1 2 3 4 5 6 7 8 9]
[1 2 3 4 5 6 7 8 9]

func Intersection added in v1.1.14

func Intersection[T cmp.Ordered](s1, s2 []T) []T

Intersection - intersection of two arrays. Returns new slice.

Example
var (
	intsL = []int{1, 2, 3}
	intsR = []int{1, 2, 4}

	floatsL = []float64{1.1, 2.2, 3.3}
	floatsR = []float64{1.1, 2.2, 4.4}

	strL = []string{"aaa", "bbb", "ccc"}
	strR = []string{"aaa", "bbb", "ddd"}
)

resInts := Intersection(intsL, intsR)
fmt.Println(resInts)

resFloats := Intersection(floatsL, floatsR)
fmt.Println(resFloats)

resStrs := Intersection(strL, strR)
fmt.Println(resStrs)
Output:

[1 2]
[1.1 2.2]
[aaa bbb]

func IsIntersect added in v1.1.14

func IsIntersect[E comparable](s1, s2 []E) bool

IsIntersect - return ok or not for intersection slice.

Example
var (
	ints1 = []int{1, 2, 3}
	ints2 = []int{3, 4, 5}

	floats1 = []float64{1.1, 2.2, 3.3}
	floats2 = []float64{11.1, 22.2, 44.4}

	str1 = []string{"aaa", "bbb", "ccc"}
	str2 = []string{"aaa", "bbb", "ddd"}
)

resInts := IsIntersect(ints1, ints2)
fmt.Println(resInts)

resFloats := IsIntersect(floats1, floats2)
fmt.Println(resFloats)

resStrs := IsIntersect(str1, str2)
fmt.Println(resStrs)
Output:

true
false
true

func MaximumNum

func MaximumNum[S ~[]E, E constraints.Float](x S, a E)

MaximumNum - set 0 if a < 0.

Example
var (
	s32_1          = []float32{1, 2, 3}
	num_32 float32 = 2

	s64_1          = []float64{1, 2, 3}
	num_64 float64 = 2
)

MaximumNum(s32_1, num_32)
fmt.Println(s32_1)

MaximumNum(s64_1, num_64)
fmt.Println(s64_1)
Output:

[0 2 3]
[0 2 3]

func Mul

func Mul[T constraints.Float](x, y []T)

Mul - multiplying slice x by slice y.

Example
var (
	s32_1 = []float32{1, 2, 3}
	s32_2 = []float32{2, 2, 2}

	s64_1 = []float64{1, 2, 3}
	s64_2 = []float64{2, 2, 2}
)

Mul(s32_1, s32_2)
fmt.Println(s32_1)

Mul(s64_1, s64_2)
fmt.Println(s64_1)
Output:

[2 4 6]
[2 4 6]

func MulNum

func MulNum[S ~[]E, E constraints.Float](x S, a E)

MulNum - multiplying slice x by a.

Example
var (
	s32_1          = []float32{1, 2, 3}
	num_32 float32 = 2

	s64_1          = []float64{1, 2, 3}
	num_64 float64 = 2
)

MulNum(s32_1, num_32)
fmt.Println(s32_1)

MulNum(s64_1, num_64)
fmt.Println(s64_1)
Output:

[2 4 6]
[2 4 6]

func Sum

func Sum[T constraints.Float | constraints.Integer | constraints.Complex](collection []T) T

Sum - sums the values in a collection. If collection is empty 0 is returned.

Example
var (
	s32 = []float32{1, 2, 3}
	s64 = []float64{1, 2, 3}
)

resFloats32 := Sum(s32)
fmt.Println(resFloats32)

resFloats64 := Sum(s64)
fmt.Println(resFloats64)
Output:

6
6

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL