pythonic

package
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: MIT Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Dict

type Dict[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Dict is a Python-like generic dictionary implementation.

func NewDict

func NewDict[K comparable, V any](cap int) *Dict[K, V]

NewDict creates an empty Dict with optional capacity.

func NewDictFromMap

func NewDictFromMap[K comparable, V any](src map[K]V) *Dict[K, V]

NewDictFromMap constructs a Dict by copying entries from provided map.

func (*Dict[K, V]) Clear

func (d *Dict[K, V]) Clear()

Clear removes all entries.

func (*Dict[K, V]) Contains

func (d *Dict[K, V]) Contains(key K) bool

Contains returns true if the key exists.

func (*Dict[K, V]) Copy

func (d *Dict[K, V]) Copy() *Dict[K, V]

Copy returns a shallow copy of the dict.

func (*Dict[K, V]) Equal

func (d *Dict[K, V]) Equal(other *Dict[K, V]) bool

Equal returns true if two dicts have identical key/value mappings.

func (*Dict[K, V]) Get

func (d *Dict[K, V]) Get(key K) (V, error)

Get returns value for key or error if missing.

func (*Dict[K, V]) Items

func (d *Dict[K, V]) Items() []Pair[K, V]

Items returns a slice with all key/value pairs.

func (*Dict[K, V]) Keys

func (d *Dict[K, V]) Keys() []K

Keys returns a slice with all keys (iteration order unspecified).

func (*Dict[K, V]) Len

func (d *Dict[K, V]) Len() int

Len returns number of key/value pairs.

func (*Dict[K, V]) Pop

func (d *Dict[K, V]) Pop(key K) (V, error)

Pop removes key and returns its value. Error if key missing.

func (*Dict[K, V]) PopItem

func (d *Dict[K, V]) PopItem() (K, V, error)

PopItem removes and returns an arbitrary key/value pair. Error if empty.

func (*Dict[K, V]) Remove

func (d *Dict[K, V]) Remove(key K) error

Remove deletes key and returns error if key not present.

func (*Dict[K, V]) Set

func (d *Dict[K, V]) Set(key K, value V)

Set assigns value for key.

func (*Dict[K, V]) SetDefault

func (d *Dict[K, V]) SetDefault(key K, defaultValue V) V

SetDefault returns the value for key if present; otherwise sets key to defaultValue and returns it.

func (*Dict[K, V]) Update

func (d *Dict[K, V]) Update(other *Dict[K, V])

Update merges entries from other into this dict (overwrites existing keys).

func (*Dict[K, V]) Values

func (d *Dict[K, V]) Values() []V

Values returns a slice with all values (iteration order unspecified).

type List

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

List is a Python-like generic list implementation. It wraps a slice and provides common list methods (append, extend, insert, remove, pop, etc.).

func ListFromSlice

func ListFromSlice[T any](s []T) *List[T]

ListFromSlice wraps an existing slice (makes a copy) into a List.

func NewList

func NewList[T any](elems ...T) *List[T]

NewList creates a new List with optional initial elements.

func (*List[T]) Append

func (l *List[T]) Append(v T)

Append adds an element to the end.

func (*List[T]) AsSlice

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

AsSlice returns a copy of the underlying slice.

func (*List[T]) Clear

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

Clear removes all elements.

func (*List[T]) Concat

func (l *List[T]) Concat(other any) *List[T]

func (*List[T]) Contains

func (l *List[T]) Contains(v T) bool

Contains returns true if v is present.

func (*List[T]) Copy

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

Copy returns a shallow copy of the list.

func (*List[T]) Count

func (l *List[T]) Count(v T) int

Count returns number of occurrences of v.

func (*List[T]) Extend

func (l *List[T]) Extend(elems ...T)

Extend appends multiple elements.

func (*List[T]) Get

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

Get returns element at index i. Supports negative indices. Returns error if out of range.

func (*List[T]) Index

func (l *List[T]) Index(v T) (int, error)

Index returns the index of the first occurrence of v. Returns error if not found.

func (*List[T]) Insert

func (l *List[T]) Insert(i int, v T) error

Insert inserts element at index i (before current element at i). Supports negative indices.

func (*List[T]) Len

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

Len returns the number of elements.

func (*List[T]) Pop

func (l *List[T]) Pop(idxOpt ...int) (T, error)

Pop removes and returns element at index i (default last). Supports negative indices.

func (*List[T]) Remove

func (l *List[T]) Remove(v T) error

Remove deletes the first occurrence of v. Returns error if not found.

func (*List[T]) Reverse

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

Reverse reverses the list in-place.

func (*List[T]) Set

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

Set assigns value at index i. Supports negative indices.

func (*List[T]) Slice

func (l *List[T]) Slice(start, stop int) *List[T]

Slice returns a new List corresponding to l[start:stop]. Supports negative indices and nil-like behavior.

func (*List[T]) Sort

func (l *List[T]) Sort(less func(a, b T) bool) error

Sort sorts the list in-place using provided less function. If less is nil returns error.

type Pair

type Pair[K comparable, V any] struct {
	Key   K
	Value V
}

Pair represents a key/value pair returned by Items().

type Set

type Set[T comparable] struct {
	// contains filtered or unexported fields
}

Set is a Python-like generic set implementation. It requires element type T to be comparable. Internally backed by map[T]struct{} for O(1) membership operations.

func NewSet

func NewSet[T comparable](elems ...T) *Set[T]

NewSet creates a new Set with optional initial elements.

func SetFromSlice

func SetFromSlice[T comparable](s []T) *Set[T]

SetFromSlice creates a Set from a slice (duplicates removed).

func (*Set[T]) Add

func (s *Set[T]) Add(v T)

Add inserts element into the set.

func (*Set[T]) AsSlice

func (s *Set[T]) AsSlice() []T

AsSlice returns elements as a slice (iteration order is unspecified).

func (*Set[T]) Clear

func (s *Set[T]) Clear()

Clear removes all elements.

func (*Set[T]) Contains

func (s *Set[T]) Contains(v T) bool

Contains returns true if element is present.

func (*Set[T]) Copy

func (s *Set[T]) Copy() *Set[T]

Copy returns a shallow copy of the set.

func (*Set[T]) Difference

func (s *Set[T]) Difference(other *Set[T]) *Set[T]

Difference returns a new set with elements in s that are not in other.

func (*Set[T]) DifferenceWith

func (s *Set[T]) DifferenceWith(other *Set[T]) *Set[T]

DifferenceWith mutates s to remove any elements present in other.

func (*Set[T]) Discard

func (s *Set[T]) Discard(v T)

Discard deletes element if present; no error if missing.

func (*Set[T]) Equal

func (s *Set[T]) Equal(other *Set[T]) bool

Equal returns true if s and other have exactly the same elements.

func (*Set[T]) Intersection

func (s *Set[T]) Intersection(other *Set[T]) *Set[T]

Intersection returns a new set with elements common to s and other.

func (*Set[T]) IntersectionWith

func (s *Set[T]) IntersectionWith(other *Set[T]) *Set[T]

IntersectionWith mutates s to keep only elements present in other.

func (*Set[T]) IsDisjoint

func (s *Set[T]) IsDisjoint(other *Set[T]) bool

IsDisjoint returns true if s has no elements in common with other.

func (*Set[T]) IsSubset

func (s *Set[T]) IsSubset(other *Set[T]) bool

IsSubset returns true if s is a subset of other.

func (*Set[T]) IsSuperset

func (s *Set[T]) IsSuperset(other *Set[T]) bool

IsSuperset returns true if s is a superset of other.

func (*Set[T]) Len

func (s *Set[T]) Len() int

Len returns number of elements in the set.

func (*Set[T]) Pop

func (s *Set[T]) Pop() (T, error)

Pop removes and returns an arbitrary element. Returns error if set is empty.

func (*Set[T]) Remove

func (s *Set[T]) Remove(v T) error

Remove deletes element and returns error if not present.

func (*Set[T]) SymmetricDifference

func (s *Set[T]) SymmetricDifference(other *Set[T]) *Set[T]

SymmetricDifference returns elements in either s or other but not both.

func (*Set[T]) SymmetricDifferenceWith

func (s *Set[T]) SymmetricDifferenceWith(other *Set[T]) *Set[T]

SymmetricDifferenceWith mutates s to be the symmetric difference between s and other.

func (*Set[T]) Union

func (s *Set[T]) Union(other *Set[T]) *Set[T]

Union returns a new set with elements from s and other.

func (*Set[T]) UnionWith

func (s *Set[T]) UnionWith(other *Set[T]) *Set[T]

UnionWith mutates s to become the union of s and other.

func (*Set[T]) Update

func (s *Set[T]) Update(elems ...T)

Update adds multiple elements.

Jump to

Keyboard shortcuts

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