Skip to content
/ slice Public

Implements some popular helpers for working with Go slices in functional manner.

License

Notifications You must be signed in to change notification settings

nDmitry/slice

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 

Repository files navigation

slice

Package slice implements some popular helpers for working with Go slices in functional manner.

It uses Go generic syntax according to the proposal that might be implemented in the language in Go 1.18.

Usage

package main

import "github.com/nDmitry/slice"

func main() {
    ints := []int{1, 2, 3, 4, 5}
    strings := []string{"eat", "a", "cake"}

    fmt.Println(
        slice.Filter(ints, func(item int, _ int) bool { return item%2 == 0 }),
        slice.Filter(ints, func(item int, i int) bool { return i%2 == 0 }),
        slice.Filter(strings, func(item string, _ int) bool { return len(item) == 4 }),

        slice.Some(ints, func(item int, _ int) bool { return item%2 == 0 }),
        slice.Every(ints, func(item int, _ int) bool { return item%2 == 0 }),

        slice.Find(ints, func(item int, _ int) bool { return item%2 == 0 }),
        slice.FindLast(ints, func(item int, _ int) bool { return item%2 == 0 }),
        slice.FindIndex(ints, func(item int, _ int) bool { return item%2 == 0 }),
        slice.FindLastIndex(ints, func(item int, _ int) bool { return item%2 == 0 }),

        slice.Map(ints, func(item int, _ int) int { return item * item }),
        slice.Map(ints, func(item int, _ int) float64 { return float64(item) * 0.5 }),

        slice.Reduce(ints, func(sum int, item int, _ int) int { return sum + item }, 0),
    )
}

About

Implements some popular helpers for working with Go slices in functional manner.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published