GList is an additional layer of abstraction over slice that provides some new functionality for data manipulation. This implementation is implemented according to OOP principles, and was written for internal purposes.
There are several interfaces for managing data based on its type and purpose. Each interface extends previous one.
IListBase
- base interface definition for basic data read nad add functionality.IList
- interface definition for not comparable elements.IListComparable
- interface definition for comparable elements.
IListBase.Add(elems ...T)
IListBase.Count() int
IListBase.ForEach(func(elem T))
IListBase.First() (T, error)
IListBase.FindFirst(func(elem T) bool) (T, error)
IListBase.Last() (T, error)
IListBase.FindLast(func(elem T) bool) (T, error)
IListBase.ToSlice() []T
IList.Where(func(elem T) bool) IList
IListComparable.Where(func(elem T) bool) IListComparable
IListComparable.Contains(elem T) bool
IListComparable.Delete(elem T)
IListComparable.Distinct() IListComparable[T]
Each implementation has constructor:
NewListBase()
- creates newIListBase
implementation.NewList()
- creates newIList
implementation.NewListComparable()
- creates newIListComparable
implementation.
You can found a lot of examples in tests.
We cannot declare an additional type in the declaration of a separate interface method, so implementing the Select method is impossible. To do something like this, I propose a trivial solution:
l := NewListComparable[SomeSourceType]()
selcetedElems := NewListComparable[SomeDestType]()
l.ForEach(func(elem SomeSourceType) {
selcetedElems.Add(SomeDestType{
// fill data
})
})