https://github.com/beeleelee/list
Collection manipulation utilities.
https://github.com/beeleelee/list
functional-programming go golang underscore
Last synced: 3 months ago
JSON representation
Collection manipulation utilities.
- Host: GitHub
- URL: https://github.com/beeleelee/list
- Owner: beeleelee
- License: mit
- Created: 2019-07-16T07:19:25.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2019-09-09T07:24:51.000Z (about 6 years ago)
- Last Synced: 2024-10-31T17:28:24.427Z (11 months ago)
- Topics: functional-programming, go, golang, underscore
- Language: Go
- Size: 91.8 KB
- Stars: 8
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Package list
list provide some useful utilities
in order to manipulate collection conveniently
in the form of functional programming
hope it will be helpful
Example
``` package main
import (
"fmt"
lee "github.com/beeleelee/list")
func main() {
intList, _ := lee.From([]int{0,1,2})
// list.Each
intList.Each(func(v lee.Item, i int){
fmt.Println(v, i)
})
// lee.Each(intList, func(v lee.Item, i int){
// fmt.Println(v, i)
// })// 0 0
// 1 1
// 2 2// list.Map
intListMapped := intList.Map(func(v lee.Item, i int) lee.Item {
return v.(int) * 2
})
// intListMapped := lee.Map(intList, func(v lee.Item, i int) lee.Item {
// return v.(int) * 2
// })fmt.Println(intListMapped)
// [0 2 4]// list.Filter
intListFiltered := intList.Filter(func(v lee.Item, i int) bool {
return v.(int) % 2 == 1
})// intListFiltered := lee.Filter(intList, func(v lee.Item, i int) bool {
// return v.(int) % 2 == 1
// })fmt.Println(intListFiltered)
// [1]lee.FromInts([]int{1,2,3,4,5})
.Reduce(func(a, b lee.Item) lee.Item {
return a.(int) + b.(int)
}, nil)
// 15lee.FromInts([]int{3,6,9,12})
.Intersection(leeFromInts([]{2,4,6,8,10,12}), func(a, b lee.Item) bool {
return a == b
})
// [6,12]}
```FUNCTIONS
func Contains(list List, f ItemTestFn) (r bool)
Contains - like Findreturn true if find the item return false if can not find the item
func Each(list List, f EachFn)
Each - each loopuse for loop to get item from list and feed item to EachFn
func Equal(s, t List, f EqualFn) (r bool)
Equal - a way to compare whether two list is equalit accept a EqualFn which handle the equal logic
func Every(list List, f ItemTestFn) (r bool)
Every - return true if every item pass testfunc FindIndex(list List, f ItemTestFn) (index int)
FindIndex - a way to find the index of a specific itemit return -1 if could not find the item
it accept a ItemTestFn which will specific the itemfunc IsSorted(list List, f LessFn) bool
IsSorted - convenience wrapper for std sort.SliceIsSortedfunc Some(list List, f ItemTestFn) (r bool)
Some - return true if any item pass testfunc Sort(list List, f LessFn)
Sort - convenience wrapper for std sort.SliceTYPES
type EachFn func(Item, int)
EachFn each loop handle signaturefunc(v Item, i int){
// switch value to the expected type
sv, _ := v.(int) // just for example, actually can use any type you specified
fmt.Println(sv)}
type EqualFn func(a, b Item) bool
EqualFn compare handle signaturefunc(a, b Item) bool {
return a == b
}
type Item interface{}
Item - generic type for list itemin order to accept any type of item in collection
func Find(list List, f ItemTestFn) (r Item, ok bool)
Find - like FindIndex, but not return index of itemit returns the specific item and ok flag
func Get(list List, i int) Item
Get - get item from list it can accept negative int as index, like -1
attention: it will never failed if then index out of range, or no item
in list, it will return nilfunc Reduce(list List, f ReduceFn, a Item) (r Item)
Reduce - fold the listtype ItemTestFn func(Item, int) bool
ItemTestFn filter loop handle signaturefunc(v Item, i int) bool {
sv := v.(string)
return sv == "foo"}
type LessFn func(i, j int) bool
LessFn same signature as sort.Lesstype List []Item
List a struct wrap collection in Data fieldfunc Difference(s List, t List, f EqualFn) (r List)
Difference - return a list with items not in the other listfunc Filter(list List, f ItemTestFn) List
Filter - filter loopfirst create a new list then use each loop to get item from list and
feed item to ItemTestFn which decide weather keep it or notfunc From(source interface{}) (nl List, e error)
From - convert regular slice to Listas do not know the item type in the slic
so use reflect package to get the item type
and rebuild a new slice with Item typecall it like this:
list.From([]int{1,2,3})func FromFloat64s(source []float64) (nl List)
FromFloat64s convert float64 slice to Listfunc FromInts(source []int) (nl List)
FromInts convert int slice to Listfunc FromStrings(source []string) (nl List)
FromStrings convert string slice to Listfunc Intersection(s List, t List, f EqualFn) (r List)
Intersection - return a list with items in both listfunc Map(list List, f MapFn) List
Map - map loopuse for loop to get item from list and feed item to MapFn
func New(length int) List
New generate a new List instancefunc Shuffle(list List) (r List)
Shuffle - return a shuffled listfunc Tail(list List, n int) List
Tail - get items from lastfunc Union(s List, t List) List
Union - union two listsfunc (l List) Contains(f ItemTestFn) bool
Contains convenience wrapper for Contains functionfunc (l List) Difference(t List, f EqualFn) List
Difference convenience wrapper for Difference Functionfunc (l List) Each(f EachFn) List
Each convenience wrapper for Each functionfunc (l List) Equal(t List, f EqualFn) bool
Equal convenience wrapper for Equal functionfunc (l List) Every(f ItemTestFn) bool
Every convenience wrapper for Every Functionfunc (l List) Filter(f ItemTestFn) List
Filter convenience wrapper for Filter functionfunc (l List) Find(f ItemTestFn) (Item, bool)
Find convenience wrapper for Find functionfunc (l List) FindIndex(f ItemTestFn) int
FindIndex convenience wrapper for FindIndex functionfunc (l List) Get(i int) Item
Get convenience wrapper for Get Functionfunc (l List) Intersection(t List, f EqualFn) List
Intersection convenience wrapper for Intersection Functionfunc (l List) IsSorted(f LessFn) bool
IsSorted convenience wrapper for std sort.SliceIsSortedfunc (l List) Map(f MapFn) List
Map convenience wrapper for Map functionfunc (l List) Reduce(f ReduceFn, a Item) Item
Reduce convenience wrapper for Reduce functionfunc (l List) Shuffle() List
Shuffle convenience wrapper for Shuffle Functionfunc (l List) Some(f ItemTestFn) bool
Some convenience wrapper for Some Functionfunc (l List) Sort(f LessFn) List
Sort convenience wrapper for std sort.Slicefunc (l List) Tail(n int) List
Tail convenience wrapper for Tail Functionfunc (l List) Union(t List) List
Union convenience wrapper for Union Functiontype MapFn func(Item, int) Item
MapFn map loop handle signaturefunc(v Item, i int) (item Item) {
sv, _ := v.(float64)
return sv * sv}
type ReduceFn func(a, b Item) Item
ReduceFn reduce handle signaturetype SwapFn func(i, j int)
SwapFn swap items by index