weaklinq

package module
v2.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2025 License: MIT Imports: 4 Imported by: 0

README ΒΆ

Weak LINQ: Lazy LINQ-like Functions for Go

An implementation of lazy linq-like functions for Go. This library provides a familiar, expressive, and declarative way to query and manipulate collections, inspired by C#'s Language Integrated Query (LINQ). By utilizing lazy evaluation, it only processes elements when the result is materialized, improving performance for long chains of operations.

πŸš€ Features

The weak-linq library aims to provide functionality similar to that of .NET's LINQ, but for Go, including:

  • Lazy Evaluation: Operations are deferred until a materialization function is called (e.g., ToSlice()), allowing for efficient chaining of operations.
  • Filtering: Selectively includes elements from a collection (e.g., FilterOn).
  • Transformation: Projects elements into a new form (e.g., Get).
  • Grouping: Organizes elements into groups based on a key (e.g., GroupBy).
  • Joining: Combines elements from different collections (e.g., Join).
  • Materialization: Functions to convert the lazy query back into a concrete slice or map (e.g., AndAssignToSlice(), AndAssignToMap()).

weak-linq also has a few features not found in LINQ like:

  • Group Lists: GroupListsBy functions allowing for quick grouping without overwriting values
  • Field Name Arg Option: All functions have a version that accepts a field name for readability (expect a slight performance hit)
  • API Aiming For Human Readablity: Functions are chanined together in a way that will hopefully make code more readable to others

πŸ“¦ Installation

To start using weak-linq, simply use go get to add the library to your project:

go get github.com/madicen/weak-linq/v2@latest

πŸ’‘ Usage and Examples

The entry point for most queries is the From() function, which initializes a new lazy query.

Human Readable Example Query

The following example demonstrates grouping lists of people's names by their age.

type Person struct { Name string; Age int }
people := []Person{...}

peopleByAge := map[int][]string

weaklinq.From(people).
  GroupListsOf("Name").
  By("Age").
  AndAssignToMap(&peopleByAge)
Basic LINQ-style Example

The following example demonstrates filtering out numbers less than or equal to 3 and then doubling the remaining numbers.

data := []int{1, 5, 2, 8, 3, 7}

// 1. Start the query from a slice.
// 2. Filter: Keep only elements greater than 3 (5, 8, 7).
// 3. Transform: Multiply each element by 2 (10, 16, 14).
// 4. Materialize: Convert the lazy query result back into a slice.

result := make([]int, 0)
weaklinq.From(data).
  FilterOnThis(func(x int) any {
    return x > 3
  }).
  GetThese(func(x int) int {
    return x * 2
  }).
  AndAssignToSlice(&result)

fmt.Println(result) 
// Output: [10 16 14]

🀝 Contributing

Contributions are welcome! If you would like to contribute, please:

  1. Fork the repository.
  2. Create a new branch (git checkout -b feature/awesome-thing).
  3. Commit your changes (git commit -m 'Add awesome thing').
  4. Push to the branch (git push origin feature/awesome-thing).
  5. Open a Pull Request.

πŸ“„ License

This project is licensed under the MIT License. See the LICENSE file for details.

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

This section is empty.

Types ΒΆ

type DeferredJoinIterable ΒΆ

type DeferredJoinIterable[T any] JoinIterable[T]

DeferredJoinIterable is a JoinIterable where the key selectors have not yet been set. Designed to be used in tandem with the On and Equals functions. Has very little use outside of that.

func (DeferredJoinIterable[T]) AsPairs ΒΆ

func (iterable DeferredJoinIterable[T]) AsPairs() Iterable[any]

AsPairs projects the joined items as Pair structs.

func (DeferredJoinIterable[T]) AsThis ΒΆ

func (iterable DeferredJoinIterable[T]) AsThis(joinSelector func(T, any) any) Iterable[any]

AsThis projects the joined items using the given joinSelector function.

func (DeferredJoinIterable[T]) Equals ΒΆ

func (iterable DeferredJoinIterable[T]) Equals(fieldName string) DeferredJoinIterable[T]

Equals sets the right key selector function using the given field name.

func (DeferredJoinIterable[T]) EqualsThis ΒΆ

func (iterable DeferredJoinIterable[T]) EqualsThis(rightKeySelector func(any) any) DeferredJoinIterable[T]

EqualsThis sets the right key selector function.

func (DeferredJoinIterable[T]) On ΒΆ

func (iterable DeferredJoinIterable[T]) On(fieldName string) DeferredJoinIterable[T]

On sets the key selector functions for both left and right iterables using the given field name.

func (DeferredJoinIterable[T]) OnThis ΒΆ

func (iterable DeferredJoinIterable[T]) OnThis(keySelector func(T) any) DeferredJoinIterable[T]

OnThis sets the key selector functions for both left and right iterables.

type DeferredKeyMapIterable ΒΆ

type DeferredKeyMapIterable[T any] MapIterable[T]

DeferredKeyMapIterable is a MapIterable where the key selector has not yet been set. Designed to be used in tandem with the By functions. Has very little use outside of that.

func (DeferredKeyMapIterable[T]) By ΒΆ

func (iterable DeferredKeyMapIterable[T]) By(fieldName string) MapIterable[T]

By returns a new MapIterable where the items are grouped by the given field name. Callable from other MapIterables. Designed to be used in tandem with the GroupThis or GroupListsOfThis functions. Items with the same key will NOT be overwritten as this iterable is iterated.

func (DeferredKeyMapIterable[T]) ByThis ΒΆ

func (iterable DeferredKeyMapIterable[T]) ByThis(selector func(T) any) MapIterable[T]

ByThis returns a new MapIterable where the items are grouped by the given key selector. Callable from other MapIterables. Designed to be used in tandem with the GroupThis or GroupListsOfThis functions. Items with the same key will NOT be overwritten as this iterable is iterated.

type Iterable ΒΆ

type Iterable[T any] struct {
	iter.Seq[T]
}

Iterable is the base structure for an iterable. It allows for the lazy iteration of a collection of items and exists to allow functions to be called on the collection.

func From ΒΆ

func From[T any](items []T) Iterable[T]

From creates a new Iterable from a slice of items.

func (Iterable[T]) AndAssignToSlice ΒΆ

func (iterable Iterable[T]) AndAssignToSlice(result any)

AndAssignToSlice assigns the result of iterating over the Iterable to a slice. The type of the result slice must be the same as the type of the item in the Iterable. If there is a type mismatch, or the result is not a pointer to a slice, this function will panic.

func (Iterable[T]) AsAny ΒΆ

func (iterable Iterable[T]) AsAny() Iterable[any]

AsAny converts an Iterable of any type T to an Iterable of type any.

func (Iterable[T]) Distinct ΒΆ

func (iterable Iterable[T]) Distinct() Iterable[T]

Distinct returns a new Iterable where the items are distinct.

func (Iterable[T]) FilterOn ΒΆ

func (iterable Iterable[T]) FilterOn(fieldName string) Iterable[T]

FilterOn returns a new Iterable where the items are filtered by the given fieldName. If T is not a struct, or fieldName is not found, this function will panic.

func (Iterable[T]) FilterOnThis ΒΆ

func (iterable Iterable[T]) FilterOnThis(predicate func(T) bool) Iterable[T]

FilterOnThis returns a new Iterable where the items are filtered by the given predicate.

func (Iterable[T]) Get ΒΆ

func (iterable Iterable[T]) Get(fieldName string) Iterable[any]

Get returns a new Iterable where the items are transformed by fieldName. If T is not a struct, or fieldName is not found, this function will panic.

func (Iterable[T]) GetThese ΒΆ

func (iterable Iterable[T]) GetThese(selector func(T) any) Iterable[any]

GetThese returns a new Iterable where the items are transformed by selector.

func (Iterable[T]) Group ΒΆ

func (iterable Iterable[T]) Group(fieldName string) DeferredKeyMapIterable[T]

Group returns a new MapIterable where the items are grouped by the given field name. Items with the same key WILL BE OVERWRITTEN as this iterable is iterated. Should be used in tandem with the By functions. Has very little use outside of that. If T is not a struct, or fieldName is not found, this function will panic.

func (Iterable[T]) GroupBy ΒΆ

func (iterable Iterable[T]) GroupBy(fieldName string) MapIterable[T]

GroupBy returns a new MapIterable where the items are grouped by the given field name. Items with the same key WILL BE OVERWRITTEN as this iterable is iterated. If T is not a struct, or fieldName is not found, this function will panic.

func (Iterable[T]) GroupByThis ΒΆ

func (iterable Iterable[T]) GroupByThis(selector func(T) any) MapIterable[T]

GroupByThis returns a new MapIterable where the items are grouped by the given key selector. Items with the same key WILL BE OVERWRITTEN as this iterable is iterated.

func (Iterable[T]) GroupListsBy ΒΆ

func (iterable Iterable[T]) GroupListsBy(fieldName string) MapIterable[T]

GroupListsBy returns a new MapIterable where the items are grouped by the given field name. Items with the same key will NOT be overwritten as this iterable is iterated. If T is not a struct, or fieldName is not found, this function will panic.

func (Iterable[T]) GroupListsByThis ΒΆ

func (iterable Iterable[T]) GroupListsByThis(selector func(T) any) MapIterable[T]

GroupListsByThis returns a new MapIterable where the items are grouped by the given key selector. Items with the same key will NOT be overwritten as this iterable is iterated.

func (Iterable[T]) GroupListsOf ΒΆ

func (iterable Iterable[T]) GroupListsOf(fieldName string) DeferredKeyMapIterable[T]

GroupListsOf returns a new MapIterable where the items are grouped by the given field name. Items with the same key will NOT be overwritten as this iterable is iterated. Should be used in tandem with the By functions. Has very little use outside of that. If T is not a struct, or fieldName is not found, this function will panic.

func (Iterable[T]) GroupListsOfThis ΒΆ

func (iterable Iterable[T]) GroupListsOfThis(selector func(T) any) DeferredKeyMapIterable[T]

GroupListsOfThis returns a new MapIterable where the items are grouped by the given selector. Items with the same key will NOT be overwritten as this iterable is iterated. Should be used in tandem with the By functions. Has very little use outside of that.

func (Iterable[T]) GroupThis ΒΆ

func (iterable Iterable[T]) GroupThis(selector func(T) any) DeferredKeyMapIterable[T]

GroupThis returns a new MapIterable where the items are grouped by the given selector. Items with the same key WILL BE OVERWRITTEN as this iterable is iterated. Should be used in tandem with the By functions. Has very little use outside of that.

func (Iterable[T]) Join ΒΆ

func (iterable Iterable[T]) Join(joinIterable Iterable[any]) DeferredJoinIterable[T]

Join returns a new DeferredJoinIterable that will join the items of the given iterable

func (Iterable[T]) JoinSlice ΒΆ

func (iterable Iterable[T]) JoinSlice(joinSlice any) DeferredJoinIterable[T]

JoinSlice returns a new DeferredJoinIterable that will join the items of the given slice

type JoinIterable ΒΆ

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

JoinIterable is a specialized iterable that includes a right iterable, a key selector for the left iterable, and a key selector for the right iterable. These selectors are stored until the collection is iterated, and then applied to the items.

type MapIterable ΒΆ

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

MapIterable is a specialized iterable that includes a key selector and an item selector. These selectors are stored until the collection is iterated, and then applied to the items. The Overwrite flag determines whether the map value for a given key should be overwritten or appended to. If set to true, the result map will be expected to be a map[K]V, and if set to false, the result map will be expected to be a map[K][]V.

func (MapIterable[T]) AndAssignToMap ΒΆ

func (iterable MapIterable[T]) AndAssignToMap(result any)

AndAssignToMap assigns the result of iterating over the MapIterable to a map. The type of the result map must be the same as the type of the key and value selector results in the MapIterable. If there is a key or value type mismatch, or the result is not a pointer to a map, this function will panic. If the MapIterable is set to overwrite, this function will attempt to build a slice of the value type, or append to the value if it already exists.

type Pair ΒΆ

type Pair[TLeft any, TRight any] struct {
	Left  TLeft
	Right TRight
}

Pair is a struct that holds a left and right item for use in join operations.

Jump to

Keyboard shortcuts

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