nilsmagnus / functions

Handy collection functions for the go language

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

tests passing

functions to go

Handy collection-functions for the go language.

Functions to support common collection-operations:

  • map
  • reduce
  • filter
  • associate,associateby
  • contains, containsAll
  • first, last
  • forEach, forEachIndexed
  • distinct

Coming soon: See godoc for complete documentation.

getting started

go get github.com/nilsmagnus/functions
package main

import (
	"fmt"
	"github.com/nilsmagnus/functions"
	"log"
)

func main() {
	items := []int{1, 2, 99}

	mappedItems := functions.Map(items, func(a int) string { return fmt.Sprintf("mapped %d", a) })

	log.Printf("Mapped items: %v", mappedItems)
}

Run this example on the playground

Have a look at functions_test.go for usage.

Map-function

numbers := []int{1,2,3,199}

mapd := Map(numbers, func(a int) string { return fmt.Sprintf("%d", a)}) 

// mapd => []string{"1", "2", "3", "199"}

AssociateBy-function

numbers := []int{1,2,3,199}

mapd := AssociateBy(numbers, func(t int) string {
    return fmt.Sprintf("%d", t)
}) 

// => map[string]int {"1":1, "2":2, "3":3, "199": 199}

About

Handy collection functions for the go language

License:Other


Languages

Language:Go 99.6%Language:Makefile 0.4%