Skip to content

peculiar‐list

Muhammad Usama edited this page Sep 7, 2023 · 2 revisions

GoDoc

Peculiar-List is an implementation of slice/list which exposes general functions for re-usability

Here is a quick summary of what peculiar-list offers

Function Description
Length Returns the size of underlying collection
Clear Clears all content from underlying collection
Add Adds value in underlying collection
AddIfAbsent Adds value in collection (only if absent)
Remove Removes value from collection on specified index
RemoveByValue Removes specified value from collection
GetValue Gets value by index from collection
GetValues Gets all values in collection
SetValue Updates value in collection at specified index
Foreach Iterates current collection with provided function
Map Iterates current collection and modify values for-each entry
SetValue Updates value in collection at specified index
Contains Checks if collection contains a value
IsEmpty Checks if collection is empty
Contains Checks if collections contains a value
Filter Filter current collection into a new collection which fulfils provided filter function

peculiar-list usage

To create a new slice instance for string

list := peculiar.NewList[string]()

OR

Or you can create a new instance with existing values by using following functions

list := peculiar.NewListWith[string]()

Adding Values

To add values to underlying collection

	...
	list.Add("s0")
	list.Add("s1")
	list.Add("s3")
	...

Removing Values

To remove values from underlying collection:

	err := list.Remove(1)
	if err == nil {
		// handle error
	}

Note: This function throws 'index out of range' error

Getting Values

To get value from underlying collection

	v, err := list.GetValue(1)
	if err != nil {
		// handle error
	}

Note: This function throws 'index out of range' error

Filtering Peculiar-List

To filter records from underlying collection into a new collection, you need to provide a filter function which returns a boolean

	value := "s5"
	newList := list.Filter(func(v string) bool {
		return v == value
	})

This will populate newList variable with filtered items if provided condition is true

...

If you are looking for key value pair collection, here is Peculiar-Map wiki

Clone this wiki locally