Skip to content

devalexandre/gofn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

e4aec7d · Feb 26, 2024

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

gofn

Coverage Go

library to use golang functional

Summary

Usage

array.Filter

Filter a array , with a function that return a boolean.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Filter(data, func(i int) bool {
        return i%2 == 0
    })

    fmt.Println(result) // [2 4 6 8 10]
	

array.Find

Find a element in the array , with a function that return a boolean.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Find(data, func(i int) bool {
        return i%2 == 0
    })

    fmt.Println(result) // 2

array.Map

Map a array , with a function that return a value.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Map(data, func(i int) int {
        return i * 2
    })

    fmt.Println(result) // [2 4 6 8 10 12 14 16 18 20]

array.FlatMap

FlatMap a array , with a function that return a slice.

a := []int{1, 2, 3, 4, 5}
b := array.FlatMap(a, func(x int) []int { return []int{x, x * 2} })

fmt.Println(b) // [1 2 2 4 3 6 4 8 5 10]

array.Reduce

Reduce a array , with a function that return a value.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Reduce(data, func(i, j int) int {
        return i + j
    })

    fmt.Println(result) // 55

array.Any

Check if any element in the array match the condition.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Any(data, func(i int) bool {
        return i%2 == 0
    })

    fmt.Println(result) // true

array.Some

Check if some element in the array match the condition.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Some(data, func(i int) bool {
        return i%2 == 0
    })

    fmt.Println(result) // true

array.Every

Check if every element in the array match the condition.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Every(data, func(i int) bool {
        return i%2 == 0
    })

    fmt.Println(result) // false

array.Sum

Sum all elements in the array.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Sum(data)

    fmt.Println(result) // 55

array.Max

Get the max element in the array.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Max(data)

    fmt.Println(result) // 10

array.Min

Get the min element in the array.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Min(data)

    fmt.Println(result) // 1

array.Product

Get the product of all elements in the array.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Product(data)

    fmt.Println(result) // 3628800

array.Reverse

Reverse te order of the array.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Reverse(data)

    fmt.Println(result) // [10 9 8 7 6 5 4 3 2 1]

array.Shuffle

Shuffle the array.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Shuffle(data)

    fmt.Println(result) // [10 9 8 7 6 5 4 3 2 1]

array.Unique

Get unique elements in the array.

data := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

    // filter
    result := array.Unique(data)

    fmt.Println(result) // [1 2 3 4 5 6 7 8 9 10]

array.Union

Get union of two arrays.

a := []int{1, 2, 3, 4, 5}
b := []int{6, 7, 8, 9, 10}

    // filter
    result := array.Union(a, b)

    fmt.Println(result) // [1 2 3 4 5 6 7 8 9 10]

array.Fill

Fill the array with a value.

a := []int{1, 2, 3, 4, 5}
b := array.Fill(a, 0)

fmt.Println(b) // [0 0 0 0 0]

array.Join

Join the array with a separator.

a := []int{1, 2, 3, 4, 5}
b := array.Join(a, "-")

fmt.Println(b) // 1-2-3-4-5

array.Pop

Pop the last element of the array, and return the removed element and the new array.

a := []int{1, 2, 3, 4, 5}
b, c := array.Pop(a)

fmt.Println(b) // 5
fmt.Println(c) // [1 2 3 4]

array.Push

Push an element to the array, and return the new array.

a := []int{1, 2, 3, 4, 5}
b := array.Push(a, 6)

fmt.Println(b) // [1 2 3 4 5 6]

array.Shift

Shift the first element of the array, and return the removed element and the new array.

a := []int{1, 2, 3, 4, 5}
b, c := array.Shift(a)

fmt.Println(b) // 1
fmt.Println(c) // [2 3 4 5]

array.Sort

Sort the array using the function.

a := []int{3, 2, 1, 5, 4}
b := array.Sort(a, func(i, j int) bool { return a[i] < a[j] })

fmt.Println(b) // [1 2 3 4 5]

array.GroupBy

type Itens struct {
		Name        string
		Price       float64
		Description string
		Qty         int
	}

	itens := []Itens{
		{"Item 1", 10.0, "Description 1", 1},
		{"Item 2", 20.0, "Description 2", 2},
		{"Item 3", 30.0, "Description 3", 3},
		{"Item 4", 40.0, "Description 4", 10},
		{"Item 4", 40.0, "Description 4", 15},
		{"Item 4", 40.0, "Description 4", 25},
	}

	grouped := array.GroupBy(itens, func(item Itens) string {
		return fmt.Sprintf("%s - %v", item.Name, item.Price)
	})
	fmt.Println(len(grouped)) // 4
	

chaining functions

You can chain the functions together.

data := array.Array[int]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

  a := data.
    Filter(func(i int) bool {
      return i%2 == 0
    }).
    Map(func(i int) int {
      return i * 2
    }).

  
    fmt.Println(a) // [4 8 12 16 20]