pythonic

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Accumulate added in v1.0.2

func Accumulate[T any](s []T, fn func(T, T) T) []T

Accumulate returns running accumulation of elements using fn. For empty input returns nil. The first element of result is s[0].

func All added in v1.0.2

func All[T any](s []T, pred func(T) bool) bool

All returns true if all elements satisfy pred (true for empty slice).

func Any added in v1.0.2

func Any[T any](s []T, pred func(T) bool) bool

Any returns true if any element satisfies pred.

func Chain added in v1.0.2

func Chain[T any](slices ...[]T) []T

Chain concatenates multiple slices into one.

func Cycle added in v1.0.2

func Cycle[T any](s []T, count int) []T

Cycle repeats the slice count times. If count <= 0 returns nil.

func DropWhile added in v1.0.2

func DropWhile[T any](s []T, pred func(T) bool) []T

DropWhile drops leading elements satisfying pred and returns the remainder.

func Filter added in v1.0.2

func Filter[T any](s []T, pred func(T) bool) []T

Filter returns elements where pred(elem) is true.

func Islice added in v1.0.2

func Islice[T any](s []T, start, stop, step int) []T

Islice returns s[start:stop:step] behavior (stop exclusive). Step must not be 0.

func Map added in v1.0.2

func Map[T any, U any](s []T, fn func(T) U) []U

Map applies fn to each element and returns a new slice.

func Max added in v1.0.2

func Max[T Ordered](s []T) (T, error)

Max returns the maximum element for Ordered types.

func Min added in v1.0.2

func Min[T Ordered](s []T) (T, error)

Min returns the minimum element for Ordered types.

func RangeInts added in v1.0.2

func RangeInts(start, stop, step int) []int

RangeInts returns a slice of ints from start (inclusive) to stop (exclusive) with step.

func Reduce added in v1.0.2

func Reduce[T any, R any](s []T, init R, fn func(R, T) R) R

Reduce reduces slice to a single value using accumulator function.

func Repeat added in v1.0.2

func Repeat[T any](value T, count int) []T

Repeat returns a slice containing value repeated count times. If count <=0 returns nil.

func Sorted added in v1.0.2

func Sorted[T Ordered](s []T) []T

Sorted returns a sorted copy of the slice for Ordered types.

func Sum added in v1.0.2

func Sum[T Numeric](s []T) T

Sum sums a slice of any numeric type and returns the same numeric type.

func SumFloat64 added in v1.0.2

func SumFloat64(s []float64) float64

SumFloat64 sums a slice of float64s.

func SumInts added in v1.0.2

func SumInts(s []int) int

SumInts sums a slice of ints.

func TakeWhile added in v1.0.2

func TakeWhile[T any](s []T, pred func(T) bool) []T

TakeWhile returns the leading slice of elements satisfying pred.

func Zip added in v1.0.2

func Zip[T any](slices ...[]T) [][]T

Zip zips slices of the same element type into a slice of rows. The result length is the length of the shortest input slice.

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 EnumeratePair added in v1.0.2

type EnumeratePair[T any] struct {
	Idx int
	Val T
}

EnumeratePair is used by Enumerate to return index and value.

func Enumerate added in v1.0.2

func Enumerate[T any](s []T, start int) []EnumeratePair[T]

Enumerate returns pairs of (index+start, value).

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 Numeric added in v1.0.2

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64
}

Numeric matches integer and floating-point numeric types.

type Ordered added in v1.0.2

type Ordered interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr |
		~float32 | ~float64 | ~string
}

Ordered is a constraint that matches ordered types for comparisons and sorting.

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