js_array_methods

package module
v0.0.0-...-67d85f5 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2023 License: MIT Imports: 0 Imported by: 0

README

JavaScript array and LINQ methods in golang 🤯

This package is just for fun and should not seriously be used in production.

This includes

Method Name What it does
.Map() will perform the callback function action on each member of the enumerable object
.Filter() will remove items in the enumerable object that do not conform to the filters passed callback
.Reduce() this will allow for an enumerable object to collapse into a single value using a callback method and a starting value
.ReduceInt() Due to go not allowing generics on struct methods this allows for custom structs to be reduced into an integer value
.ReduceString() Again due to go not allowing generics on struct methods this allows for customer structs to be reduced into a string
.Select() Straight out of C#'s LINQ this method allows for mapping an Enumerable list into a different type
.ToList() As is sounds, converts the enumerable type into a standard slice
.Count() unneccessary but stops you having to do len(my_enum.ToList()), grabs the number of items in the enumerable

Example usage

Import the package

import (
	js_array_methods "github.com/callummclu/js-array-methods-in-go"
)

Create the Enumerable

nums := js_array_methods.ToEnumerable([]int{1,2,3,4,5})
.Map()

Call method on enumerable

nums.Map(func(x *int){
	*(x) * 2
})

Example output

[ 2 4 6 8 10 ]
.Filter()

Call method on enumerable

nums.Filter(func(x *int) bool {
	return *(x) % 2 == 0
})

Example output

[2 4]
.Reduce()

Call method on enumerable

total := nums.Reduce(func(prev, curr *int){
	return *prev + *curr
}, 0)

Example output

15
.ReduceInt()

Call method on enumerable

type Person struct{
	age int
}

people := ToEnumerable([]Person{{age:18},{age:19},{age:20},{age:21},{age:22}})

average_age := people.ReduceInt(func(prev *int, curr *Person) int{
	return *prev + curr.age
},0) / people.Count()

Example Output

20
.ReduceString()

Call method on enumerable

type Person struct{
	name string
}

people := ToEnumerable([]Person{{name:"gene"},{name:"oliver"},{name:"lewis"},{name:"andrew"},{name:"norman"},{name:"geoff"}})

team_name := people.ReduceString(func(prev *string, curr *Person) string{
	return *prev + curr.name[0:1]
},"")

Example Output

"golang"
.Select()

Call method on enumerable

type Person struct{
	age int
}

type Adult struct{
	is_above_18 bool
	age int
}

people := ToEnumerable([]Person{{age:10},{age:20},{age:18},{age:12}})

adults := people.Filter(func(p *Person) bool{
	return p.age >= 18
}).Select(func(p Person) interface{}{
	return Adult{is_above_18:true, age:p.age}
})

Example Output

{true 20} {true 18}
.ToList()

Call method on enumerable

nums := ToEnumerable([]int{1,2,3,4}})

nums.ToList()

Example output

[ 1 2 3 4 ] // as a standard slice
.Count()

Call method on enumerable

nums := ToEnumerable([]int{1,2,3,4}})

nums.Count()

Example output

4

All of these method should also function with any other data type not just int

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Enumerable

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

func ToEnumerable

func ToEnumerable[T any](items []T) Enumerable[T]

func (*Enumerable[T]) Count

func (e *Enumerable[T]) Count() int

returns the number of items in the enumerable

func (*Enumerable[T]) Filter

func (e *Enumerable[T]) Filter(fn func(*T) bool) *Enumerable[T]

Call an anonymous function across each element in your list of items to filter out unwanted items.

func (*Enumerable[T]) Map

func (e *Enumerable[T]) Map(fn func(*T)) *Enumerable[T]

Call an anonymous function across each element in your list of items to change values.

func (*Enumerable[T]) Reduce

func (e *Enumerable[T]) Reduce(fn func(*T, *T) T, starting_point T) T

Squash your list of items into the same type using a criteria specified in a callback method using a starting value.

- Outputs a value unlike the rest of the functional methods in this package

func (*Enumerable[T]) ReduceInt

func (e *Enumerable[T]) ReduceInt(fn func(*int, *T) int, starting_point int) int

Squash your list of items into a single integer using a criteria specified in a callback method using a starting value.

- Outputs a value unlike the rest of the functional methods in this package

func (*Enumerable[T]) ReduceString

func (e *Enumerable[T]) ReduceString(fn func(*string, *T) string, starting_point string) string

Squash your list of items into a single string using a criteria specified in a callback method using a starting value.

- Outputs a value unlike the rest of the functional methods in this package

func (*Enumerable[T]) Select

func (e *Enumerable[T]) Select(fn func(T) interface{}) (new_list []any)

From C# works similarly to Map but instead returns a new value Call an anonymous function across each element in your list of items to change values.

func (*Enumerable[T]) ToList

func (e *Enumerable[T]) ToList() []T

Jump to

Keyboard shortcuts

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