Skip to content

Commit

Permalink
adding dictionary capability
Browse files Browse the repository at this point in the history
  • Loading branch information
tmontdev committed Nov 3, 2023
1 parent 119887d commit 6b5861c
Show file tree
Hide file tree
Showing 8 changed files with 408 additions and 153 deletions.
95 changes: 0 additions & 95 deletions collection.go

This file was deleted.

44 changes: 44 additions & 0 deletions dictionary.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package collection

type Dictionary[K comparable, V any] interface {
// Length method return how many elements are stored in Dictionary
Length() int

// IsEmpty method return true if there are no values stored in the Dictionary.
IsEmpty() bool

// IsNotEmpty method return true if there are one or more values stored in the Dictionary
IsNotEmpty() bool

// Where returns a new Dictionary containing only the elements witch satisfies de Predicate
Where(predicate KeyPredicate[K, V]) Dictionary[K, V]

// RemoveWhere deletes all elements witch satisfies the Predicate, and returns itself
RemoveWhere(predicate KeyPredicate[K, V]) Dictionary[K, V]

// Some returns true if one or more elements satisfies de Predicate
Some(predicate KeyPredicate[K, V]) bool

// None returns true when no element satisfies the Predicate
None(predicate KeyPredicate[K, V]) bool

// Every returns true when all the elements satisfies the Predicate
Every(predicate KeyPredicate[K, V]) bool

// Set sets the given value in the given key, and returns itself
Set(key K, value V) Dictionary[K, V]

// Get returns the value stored in the given key from the Dictionary
Get(key K) V

// Access returns the value stored in the given key (if stored)
Access(key K) (V, bool)

// Clone returns a new Dictionary with the same keys and values from the original
Clone() Dictionary[K, V]

// Has returns true if the given key is filled.
Has(key K) bool

// Keys returns a List with all
}
2 changes: 2 additions & 0 deletions functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package collection

type Predicate[T any] func(T) bool

type KeyPredicate[K comparable, V any] func(K, V) bool

type Mapper[T any] func(T) any

type Reducer[T any] func(any, T, int) any
Expand Down
95 changes: 95 additions & 0 deletions iterable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package collection

type Iterable[T any] interface {
// Length method returns how many element are in the Iterable
Length() int

// IsEmpty returns true if Length is zero
IsEmpty() bool

// IsNotEmpty returns true if Length is not zero
IsNotEmpty() bool

// At returns the pointer of the element in the given index from the Iterable
// If there is no element in the index, nil will be returned
At(int) *T

// ElementAt returns the element in the given index from the Iterable
// If there is no element in the index, panics
ElementAt(int) T

// Elements returns a built-in slice with all elements in the Iterable
Elements() []T

// Push add the given elements in the Iterable, and returns itself
Push(...T) Iterable[T]

// Clone returns an identical Iterable from the original
Clone() Iterable[T]

// FirstElement returns the first element in the Iterable
// if isEmpty, panics
FirstElement() T

// First returns the pointer of the first element in the Iterable
// if isEmpty, nil will be returned
First() *T

// LastElement returns the last element in the Iterable
// if isEmpty, panics
LastElement() T

// Last returns the pointer of the last element in the Iterable
// if isEmpty, nil will be returned
Last() *T

// FirstIndexWhere returns the index of the first element witch satisfies the predicate
// if no element satisfies the predicate, -1 will be returned
FirstIndexWhere(handler Predicate[T]) int

// LastIndexWhere returns the index of the last element witch satisfies the predicate
// if no element satisfies the predicate, -1 will be returned
LastIndexWhere(handler Predicate[T]) int

// IndexWhere returns a Iterable[int] for all element index witch satisfies the predicate
// if no element satisfies the predicate, an empty Iterable will be returned
IndexWhere(handler Predicate[T]) Iterable[int]

// Where returns a Iterable with all the elements witch satisfies the predicate.
// if no element satisfies the predicate, an empty Iterable will be returned
Where(handler Predicate[T]) Iterable[T]

// Map iterates over the element of the Iterable calling Mapper, and return a new Iterable with the results.
Map(handler Mapper[T]) Iterable[any]

// Reduce executes the Reducer for each element from the list with the given accumulator, and each result will be accumulator for the next
// The final result will be returned
Reduce(reducer Reducer[T], accumulator any) any

// Every returns true if every element in the Iterable satisfy the predicate
Every(handler Predicate[T]) bool

// Some returns true if at least one element in the Iterable satisfy the predicate
Some(handler Predicate[T]) bool

// None returns true no element in the Iterable satisfy the predicate
None(handler Predicate[T]) bool

// Pop removes the last element from the Iterable and returns itself
Pop() Iterable[T]

// Shift removes the first element from the Iterable and returns itself
Shift() Iterable[T]

// Set sets the given element in the given index, and returns itself
Set(index int, element T) Iterable[T]

// Interval returns a new Iterable with all elements between from and to given indexes
Interval(from, to int) Iterable[T]

// String returns a string representation of the Iterable
String() string

// Sort receive a Sorter function to sort its elements, and returns itself after sorted
Sort(sorter Sorter[T]) Iterable[T]
}
26 changes: 14 additions & 12 deletions list.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package collection

import "fmt"
import (
"fmt"
)

// NewList returns a new List with the given elements
func NewList[T any](elements ...T) *List[T] {
Expand Down Expand Up @@ -47,13 +49,13 @@ func (l *List[T]) Elements() []T {
}

// Push add the given elements in the List, and returns itself
func (l *List[T]) Push(elements ...T) Collection[T] {
func (l *List[T]) Push(elements ...T) Iterable[T] {
*l = append(l.Elements(), elements...)
return l
}

// Clone returns an identical List from the original
func (l *List[T]) Clone() Collection[T] {
func (l *List[T]) Clone() Iterable[T] {
return NewList[T](l.Elements()...)
}

Expand Down Expand Up @@ -106,7 +108,7 @@ func (l *List[T]) LastIndexWhere(handler Predicate[T]) int {

// IndexWhere returns a new List[int] for all element index witch satisfies the predicate
// if no element satisfies the predicate, an empty List will be returned
func (l *List[T]) IndexWhere(handler Predicate[T]) Collection[int] {
func (l *List[T]) IndexWhere(handler Predicate[T]) Iterable[int] {
list := NewList[int]()
for i, v := range l.Elements() {
if handler(v) {
Expand All @@ -118,7 +120,7 @@ func (l *List[T]) IndexWhere(handler Predicate[T]) Collection[int] {

// Where returns a new List with all the elements witch satisfies the predicate.
// if no element satisfies the predicate, an empty List will be returned
func (l *List[T]) Where(handler Predicate[T]) Collection[T] {
func (l *List[T]) Where(handler Predicate[T]) Iterable[T] {
selected := NewList[T]()
for _, v := range l.Elements() {
if handler(v) {
Expand All @@ -129,7 +131,7 @@ func (l *List[T]) Where(handler Predicate[T]) Collection[T] {
}

// Map iterates over the elements of the List calling Mapper, and return a new List with the results.
func (l *List[T]) Map(handler Mapper[T]) Collection[any] {
func (l *List[T]) Map(handler Mapper[T]) Iterable[any] {
mapped := NewList[any]()
for _, v := range l.Elements() {
mapped.Push(handler(v))
Expand Down Expand Up @@ -177,28 +179,28 @@ func (l *List[T]) None(handler Predicate[T]) bool {
}

// Pop removes the last element from the List and returns itself
func (l *List[T]) Pop() Collection[T] {
func (l *List[T]) Pop() Iterable[T] {
*l = l.Elements()[0 : l.Length()-1]
return l
}

// Shift removes the first element from the List and returns itself
func (l *List[T]) Shift() Collection[T] {
func (l *List[T]) Shift() Iterable[T] {
*l = l.Elements()[1:l.Length()]
return l
}

// Set sets the given element in the given index, and returns itself
// if the given index is not yet filled, panics
func (l *List[T]) Set(index int, element T) Collection[T] {
func (l *List[T]) Set(index int, element T) Iterable[T] {
l.ElementAt(index)
at := l.At(index)
*at = element
return l
}

// Interval returns a new Collection with all elements between from and to given indexes
func (l *List[T]) Interval(from, to int) Collection[T] {
// Interval returns a new Iterable with all elements between from and to given indexes
func (l *List[T]) Interval(from, to int) Iterable[T] {
return NewList[T](l.Elements()[from : to+1]...)
}

Expand All @@ -208,7 +210,7 @@ func (l *List[T]) String() string {
}

// Sort receive a Sorter function to sort its elements, and returns itself after sorted
func (l *List[T]) Sort(sorter Sorter[T]) Collection[T] {
func (l *List[T]) Sort(sorter Sorter[T]) Iterable[T] {
changed := true
for changed {
changed = l.sort(sorter)
Expand Down
Loading

0 comments on commit 6b5861c

Please sign in to comment.