list

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: MIT Imports: 4 Imported by: 0

README

AtomicGo | list

Downloads Latest Release Tests Coverage Unit test count Go report


Get The Module | Documentation | Contributing | Code of Conduct


AtomicGo


-----------------------------------------------------------------------------------------------------

go get atomicgo.dev/list


-----------------------------------------------------------------------------------------------------

list

import "atomicgo.dev/list"

Package list implements a generic list. It is a wrapper around a slice and has multiple useful methods. It can be used to develop in a functional style, but it is not required. The main purpose of this package is, to make working with slices easier. The package supports sorting, shuffling, filtering, mapping, appending, prepending, removing, inserting and more.

Index

type List

List is a generic list type.

type List[T any] struct {
    // contains filtered or unexported fields
}
Example (Functional)

package main

import (
	"fmt"
	"strings"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	l.Map(func(s string) string {
		return s + "!"
	}).Filter(func(s string) bool {
		return !strings.Contains(s, "b")
	})

	fmt.Println(l)
}
Output
[a! c!]

func SliceToList
func SliceToList[T any](items []T) List[T]

SliceToList converts a slice to a list.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	l := list.SliceToList([]string{"a", "b", "c"})

	fmt.Println(l)
}
Output
[a b c]

func (*List[T]) Append
func (l *List[T]) Append(items ...T) *List[T]

Append adds items to the end of list.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l)
}
Output
[a b c]

func (*List[T]) Clear
func (l *List[T]) Clear() *List[T]

Clear removes all items from the list.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Clear()

	fmt.Println(l)
}
Output
[]

func (List[T]) Contains
func (l List[T]) Contains(item T) bool

Contains returns true if the list contains the given item.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Contains("b"))
}
Output
true

func (*List[T]) Copy
func (l *List[T]) Copy() *List[T]

Copy returns a new copy of the list. Useful when you want to modify a list without modifying the original.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Append("appended"))         // Overwrites the original list
	fmt.Println(l.Copy().Append("appended1")) // Does not overwrite the original list
	fmt.Println(l.Copy().Append("appended2")) // Does not overwrite the original list
	fmt.Println(l)
}
Output
[a b c appended]
[a b c appended appended1]
[a b c appended appended2]
[a b c appended]

func (*List[T]) Filter
func (l *List[T]) Filter(f func(T) bool) *List[T]

Filter removes all items from the list that do not match the given predicate.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	l.Filter(func(s string) bool {
		return s != "b"
	})

	fmt.Println(l)
}
Output
[a c]

func (List[T]) ForEach
func (l List[T]) ForEach(f func(T))

ForEach iterates over the list and calls the given function for each item.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.ForEach(func(s string) {
		fmt.Println(s)
	})
}
Output
a
b
c

func (*List[T]) Get
func (l *List[T]) Get(i int) T

Get returns the item at the given index.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Get(1))
}
Output
b

func (List[T]) IndexOf
func (l List[T]) IndexOf(item T) int

IndexOf returns the index of the first occurrence of the given item.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.IndexOf("b"))
}
Output
1

func (*List[T]) Insert
func (l *List[T]) Insert(i int, items ...T) *List[T]

Insert adds items at the given index.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Insert(1, "inserted")
	fmt.Println(l)

	l.Insert(0, "a", "b", "c")
	fmt.Println(l)
}
Output
[a inserted b c]
[a b c a inserted b c]

func (*List[T]) Len
func (l *List[T]) Len() int

Len returns the length of the list.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Len())
}
Output
3

func (*List[T]) Map
func (l *List[T]) Map(f func(T) T) *List[T]

Map overwrites the list with the result of applying the given function to each item.

Example

package main

import (
	"fmt"
	"strings"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c").Map(strings.ToUpper)

	fmt.Println(l)
}
Output
[A B C]

func (*List[T]) Prepend
func (l *List[T]) Prepend(items ...T) *List[T]

Prepend adds items to the beginning of list.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Prepend("d", "e", "f")

	fmt.Println(l)
}
Output
[d e f a b c]

func (*List[T]) Reduce
func (l *List[T]) Reduce(f func(T, T) T) T

Reduce reduces the list to a single value by applying the given function to each item.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[int]

	l.Append(1, 2, 3)
	sum := l.Reduce(func(a, b int) int {
		return a + b
	})

	fmt.Println(sum)
}
Output
6

func (*List[T]) Remove
func (l *List[T]) Remove(i int) *List[T]

Remove removes the item at the given index.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Remove(1)

	fmt.Println(l)
}
Output
[a c]

func (*List[T]) Reverse
func (l *List[T]) Reverse() *List[T]

Reverse reverses the list.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Reverse()

	fmt.Println(l)
}
Output
[c b a]

func (*List[T]) Set
func (l *List[T]) Set(i int, item T) *List[T]

Set sets the item at the given index.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Set(1, "set")

	fmt.Println(l)
}
Output
[a set c]

func (*List[T]) Shuffle
func (l *List[T]) Shuffle() *List[T]

Shuffle shuffles the list. You need to seed the random number generator yourself.

Example

package main

import (
	"fmt"
	"math/rand"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	rand.Seed(1337) // You should probably use time.Now().UnixNano() in your code.

	l.Append("a", "b", "c")
	l.Shuffle()

	fmt.Println(l)
}
Output
[b a c]

func (*List[T]) Slice
func (l *List[T]) Slice() []T

Slice returns the list as a slice.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Slice())
}
Output
[a b c]

func (*List[T]) Sort
func (l *List[T]) Sort(f func(T, T) bool) *List[T]

Sort sorts the list using the given function.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[int]

	l.Append(3, 2, 1)
	l.Sort(func(a, b int) bool {
		return a < b
	})

	fmt.Println(l)
}
Output
[1 2 3]

func (List[T]) String
func (l List[T]) String() string
Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l)
}
Output
[a b c]

func (*List[T]) Swap
func (l *List[T]) Swap(i, j int) *List[T]

Swap swaps the items at the given indices.

Example

package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Swap(0, 2)

	fmt.Println(l)
}
Output
[c b a]

Generated by gomarkdoc


AtomicGo.dev  ·  with ❤️ by @MarvinJWendt | MarvinJWendt.com

Documentation

Overview

Package list implements a generic list. It is a wrapper around a slice and has multiple useful methods. It can be used to develop in a functional style, but it is not required. The main purpose of this package is, to make working with slices easier. The package supports sorting, shuffling, filtering, mapping, appending, prepending, removing, inserting and more.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List[T any] struct {
	// contains filtered or unexported fields
}

List is a generic list type.

Example (Functional)
package main

import (
	"fmt"
	"strings"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	l.Map(func(s string) string {
		return s + "!"
	}).Filter(func(s string) bool {
		return !strings.Contains(s, "b")
	})

	fmt.Println(l)
}
Output:

[a! c!]

func SliceToList

func SliceToList[T any](items []T) List[T]

SliceToList converts a slice to a list.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	l := list.SliceToList([]string{"a", "b", "c"})

	fmt.Println(l)
}
Output:

[a b c]

func (*List[T]) Append

func (l *List[T]) Append(items ...T) *List[T]

Append adds items to the end of list.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l)
}
Output:

[a b c]

func (*List[T]) Clear

func (l *List[T]) Clear() *List[T]

Clear removes all items from the list.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Clear()

	fmt.Println(l)
}
Output:

[]

func (List[T]) Contains

func (l List[T]) Contains(item T) bool

Contains returns true if the list contains the given item.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Contains("b"))
}
Output:

true

func (*List[T]) Copy

func (l *List[T]) Copy() *List[T]

Copy returns a new copy of the list. Useful when you want to modify a list without modifying the original.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Append("appended"))         // Overwrites the original list
	fmt.Println(l.Copy().Append("appended1")) // Does not overwrite the original list
	fmt.Println(l.Copy().Append("appended2")) // Does not overwrite the original list
	fmt.Println(l)
}
Output:

[a b c appended]
[a b c appended appended1]
[a b c appended appended2]
[a b c appended]

func (*List[T]) Filter

func (l *List[T]) Filter(f func(T) bool) *List[T]

Filter removes all items from the list that do not match the given predicate.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	l.Filter(func(s string) bool {
		return s != "b"
	})

	fmt.Println(l)
}
Output:

[a c]

func (List[T]) ForEach

func (l List[T]) ForEach(f func(T))

ForEach iterates over the list and calls the given function for each item.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.ForEach(func(s string) {
		fmt.Println(s)
	})
}
Output:

a
b
c

func (*List[T]) Get

func (l *List[T]) Get(i int) T

Get returns the item at the given index.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Get(1))
}
Output:

b

func (List[T]) IndexOf

func (l List[T]) IndexOf(item T) int

IndexOf returns the index of the first occurrence of the given item.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.IndexOf("b"))
}
Output:

1

func (*List[T]) Insert

func (l *List[T]) Insert(i int, items ...T) *List[T]

Insert adds items at the given index.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Insert(1, "inserted")
	fmt.Println(l)

	l.Insert(0, "a", "b", "c")
	fmt.Println(l)
}
Output:

[a inserted b c]
[a b c a inserted b c]

func (*List[T]) Len

func (l *List[T]) Len() int

Len returns the length of the list.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Len())
}
Output:

3

func (*List[T]) Map

func (l *List[T]) Map(f func(T) T) *List[T]

Map overwrites the list with the result of applying the given function to each item.

Example
package main

import (
	"fmt"
	"strings"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c").Map(strings.ToUpper)

	fmt.Println(l)
}
Output:

[A B C]

func (*List[T]) Prepend

func (l *List[T]) Prepend(items ...T) *List[T]

Prepend adds items to the beginning of list.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Prepend("d", "e", "f")

	fmt.Println(l)
}
Output:

[d e f a b c]

func (*List[T]) Reduce

func (l *List[T]) Reduce(f func(T, T) T) T

Reduce reduces the list to a single value by applying the given function to each item.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[int]

	l.Append(1, 2, 3)
	sum := l.Reduce(func(a, b int) int {
		return a + b
	})

	fmt.Println(sum)
}
Output:

6

func (*List[T]) Remove

func (l *List[T]) Remove(i int) *List[T]

Remove removes the item at the given index.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Remove(1)

	fmt.Println(l)
}
Output:

[a c]

func (*List[T]) Reverse

func (l *List[T]) Reverse() *List[T]

Reverse reverses the list.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Reverse()

	fmt.Println(l)
}
Output:

[c b a]

func (*List[T]) Set

func (l *List[T]) Set(i int, item T) *List[T]

Set sets the item at the given index.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Set(1, "set")

	fmt.Println(l)
}
Output:

[a set c]

func (*List[T]) Shuffle

func (l *List[T]) Shuffle() *List[T]

Shuffle shuffles the list. You need to seed the random number generator yourself.

Example
package main

import (
	"fmt"
	"math/rand"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	rand.Seed(1337) // You should probably use time.Now().UnixNano() in your code.

	l.Append("a", "b", "c")
	l.Shuffle()

	fmt.Println(l)
}
Output:

[b a c]

func (*List[T]) Slice

func (l *List[T]) Slice() []T

Slice returns the list as a slice.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l.Slice())
}
Output:

[a b c]

func (*List[T]) Sort

func (l *List[T]) Sort(f func(T, T) bool) *List[T]

Sort sorts the list using the given function.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[int]

	l.Append(3, 2, 1)
	l.Sort(func(a, b int) bool {
		return a < b
	})

	fmt.Println(l)
}
Output:

[1 2 3]

func (List[T]) String

func (l List[T]) String() string
Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")

	fmt.Println(l)
}
Output:

[a b c]

func (*List[T]) Swap

func (l *List[T]) Swap(i, j int) *List[T]

Swap swaps the items at the given indices.

Example
package main

import (
	"fmt"

	"atomicgo.dev/list"
)

func main() {
	var l list.List[string]

	l.Append("a", "b", "c")
	l.Swap(0, 2)

	fmt.Println(l)
}
Output:

[c b a]

Jump to

Keyboard shortcuts

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