golist

package module
v0.6.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2023 License: MIT Imports: 7 Imported by: 0

README

Generic List for Go.

Software License

Contents

Installation

go get -u github.com/kafkiansky/golist

Usage

From

Create the List[V] from given slice:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.From([]int{1, 2, 3}))
}

FromString

Create the List[string] from given string, split it by separator and apply mapper:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.FromString[int]("1, 2, 3", ",", func(s string) (int, bool) {
        // do conversation logic here.
	}))
}

Range

Create the List[V] from the given range:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Range(0, 10).Values()) // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}

L

Alias to From.

Var

Create the List[V] from variadic of type V:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).Values()) // [1, 2, 3]
}

Values

Get the values as builtin slice []V from the List[V].

First

Get the first element of List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).First()) // 1
}

Last

Get the last element of List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).Last()) // 3
}

Len

Get the actual len of List[V].

Add

Add the element to List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).Add(4).Values()) // [1, 2, 3, 4]
}

Delete

Delete the element from List[V] by index:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).Delete(0).Values()) // [2, 3]
}

Filter

Filter List[V] by the given filter function:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).Filter(func(v int) bool {
		// do your filter logic here
		return false
    }).Values())
}

Each

Iterate List[V] and apply a function:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).Each(func(v int) int {
		// do your logic here
		return v * 2
    }).Values()) // [2, 4, 6]
}

Or the Each function that take the List[V] and output new List[E]:

package main

import (
  "fmt"
  "github.com/kafkiansky/golist"
  "strconv"
)

func main() {
  fmt.Println(golist.Each(golist.Var(1, 2, 3), func(v int) string {
    return strconv.Itoa(v)
  }).Values()) // ["1", "2", "3"]
}

Chunk

Chunk List[V] to the slice of List[V] by the given size:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).Chunk(2)[0].Values()) // [1, 2]
}

Join

Chunk List[V] to the slice of List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(1, 2, 3).Join(golist.Var(4, 5)).Values()) // [1, 2, 3, 4, 5]
}

Nth

Get each nth element from the List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(0, 1, 2, 3, 4, 5, 6).Nth(2).Values()) // [0, 2, 4, 6]
}

Random

Get random value from the List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(0, 1, 2, 3, 4, 5, 6).Random()) // 2, in example
}

Contains

Check if element exists in the List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(0, 1, 2, 3, 4, 5, 6).Contains(2)) // true
	fmt.Println(golist.Var(0, 1, 2, 3, 4, 5, 6).Contains(11)) // false
}

Reverse

Generate the reverse list of List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(0, 1, 2, 3, 4, 5, 6).Reverse().Values()) // [6, 5, 4, 3, 2, 1, 0]
}

Shuffle

Shuffle the given List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(0, 1, 2, 3, 4, 5, 6).Shuffle().Values()) // [4, 1, 0, 6, 2, 5, 3], in example
}

Unique

Create the List[V] only from unique values of the target List[V]:

package main

import (
	"fmt"
	"github.com/kafkiansky/golist"
)

func main() {
	fmt.Println(golist.Var(0, 0, 1, 1, 2).Unique().Values()) // [0, 1, 2]
}

Zip

Zip target List[V] with the other:

package main

import (
  "fmt"
  "github.com/kafkiansky/golist"
  "log"
)

func main() {
  l, err := golist.Var(0, 1, 2).Zip(golist.Var(3, 5, 6))
  if err != nil {
    log.Fatalln(err) // occurred when lengths of the given list are different
  }

  fmt.Println(l[0].Values()) // [0, 3]
  fmt.Println(l[1].Values()) // [1, 5]
  fmt.Println(l[2].Values()) // [2, 6]
}

JoinToString

Joins the List[V] to string if V is type of string.

package main

import (
  "fmt"
  "github.com/kafkiansky/golist"
)

func main() {
  fmt.Println(golist.Var("first", "second").JoinToString(", ")) // "first, second"
}

Fill

Generate List[V] of the given V and provided count:

package main

import (
  "fmt"
  "github.com/kafkiansky/golist"
)

func main() {
  fmt.Println(golist.Fill("?", 3).Values()) // [?, ?, ?]
}

Sequence

Generate sequence. Useful for SQL query building:

package main

import (
  "fmt"
  "github.com/kafkiansky/golist"
)

func main() {
  fmt.Println(golist.Sequence("$", 3, 1).JoinToString(", ")) // "$1, $2, $3"
}

Interface

Converts the List[V] to []interface{}:

package main

import (
  "fmt"
  "github.com/kafkiansky/golist"
)

func main() {
  fmt.Println(golist.Var(0, 1, 2).Interface()) // []interface{}{0, 1, 2}
}

Empty

Return true if list len is zero:

package main

import (
  "fmt"
  "github.com/kafkiansky/golist"
)

func main() {
  fmt.Println(golist.L([]int{}).Empty()) // true
  fmt.Println(golist.L([]int{200}).Empty()) // false
}

Testing

$ make check

License

The MIT License (MIT). See License File for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Every added in v0.5.0

func Every[V comparable, E any](l List[V], fn func(V) E) []E

Every apply the given func to each element of List[V] and return the new slice of E.

Types

type List

type List[V comparable] struct {
	// contains filtered or unexported fields
}

List represents go slice of generic values as concurrency safe List[V].

func Between added in v0.6.0

func Between[V comparable](min, max, chunk int, f func(start, count int) V) List[V]

func Each

func Each[V, E comparable](l List[V], fn func(V) E) List[E]

Each apply the given func to each element of List[V] and return the new List[E].

func Fill added in v0.2.1

func Fill[V comparable](symbol V, count int) List[V]

Fill create the List[V] from given V by the specified count.

func From

func From[V comparable](values []V) List[V]

From creates the List[V] from the given slice.

func FromString

func FromString[V comparable](s string, separator string, mapper func(string) (V, bool)) List[V]

FromString split the string by separator, apply the mapper for each value and output the List[V].

func L

func L[V comparable](values []V) List[V]

L alias to From.

func Range

func Range[V constraints.Integer](min, max V) List[V]

Range creates the list of given range constrained by Integer.

func Sequence added in v0.2.1

func Sequence(symbol string, count int, start int) List[string]

Sequence generate the List[string] of the given symbol concatenated with index. Useful for SQL query generation. In example: golist.Sequence("$", 3, 1).Values() [$1, $2, $3]

func Var

func Var[V comparable](values ...V) List[V]

Var creates the List[V] from variadic.

func (List[V]) Add

func (l List[V]) Add(v V) List[V]

Add allow to add element to the List[V].

func (List[V]) Chunk

func (l List[V]) Chunk(size int) []List[V]

Chunk creates the slice of List[V] for the given slice size.

func (List[V]) Contains

func (l List[V]) Contains(v V) bool

Contains check that V exists in List[V].

func (List[V]) Delete

func (l List[V]) Delete(index uint) List[V]

Delete deletes the element from slice by index.

func (List[V]) Each

func (l List[V]) Each(mapper func(V) V) List[V]

Each apply the mapper function for each value and return the List[V].

func (List[V]) Empty added in v0.4.0

func (l List[V]) Empty() bool

Empty return true if len is zero.

func (List[V]) Filter

func (l List[V]) Filter(filter func(V) bool) List[V]

Filter filters element of the List[V].

func (List[V]) First

func (l List[V]) First() V

First return the first element of List[V]

func (List[V]) Interface added in v0.3.1

func (l List[V]) Interface() []interface{}

Interface return List[V] as slice of interfaces.

func (List[V]) Join

func (l List[V]) Join(lists ...List[V]) List[V]

Join other lists with the target list.

func (List[V]) JoinToString added in v0.2.1

func (l List[V]) JoinToString(separator string) string

JoinToString joins the list elements if V is type of string. Otherwise, skip the value.

func (List[V]) Last

func (l List[V]) Last() V

Last return the last element of List[V].

func (List[V]) Len

func (l List[V]) Len() int

Len return actual slice len.

func (List[V]) Nth

func (l List[V]) Nth(nth int) List[V]

Nth return each nth element of the List[V].

func (List[V]) Random

func (l List[V]) Random() V

Random return random element from List[V].

func (List[V]) Reverse

func (l List[V]) Reverse() List[V]

Reverse return reversed List[V].

func (List[V]) Shuffle

func (l List[V]) Shuffle() List[V]

Shuffle the target List[V].

func (List[V]) Unique

func (l List[V]) Unique() List[V]

Unique return only unique values of the List[V]

func (List[V]) Values

func (l List[V]) Values() []V

Values return the builtin slice of V.

func (List[V]) Zip

func (l List[V]) Zip(other List[V]) ([]List[V], error)

Zip creates List[V] together with other List[V].

Jump to

Keyboard shortcuts

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