gollection

package module
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 29, 2016 License: MIT Imports: 5 Imported by: 20

README

gollection

GoDoc Coverage Status CircleCI

gollection is golang collection util library (inspired by kotlin collection)

supported functions

  • distinct
  • distinctBy
  • filter
  • flatMap
  • flatten
  • fold
  • map
  • reduce
  • skip (java8)
  • sortBy
  • take

Feature

  • read-only original slice
  • stream(goroutine) support

Installation

go get github.com/azihsoyn/gollection or go get gopkg.in/azihsoyn/gollection.v1

Usage

see examples code

or godoc examples

LICENSE

MIT

Documentation

Overview

Package gollection provides collection util to any type slices.

Example (CustomType)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	type User struct {
		ID   int
		Name string
	}

	in := []User{
		{ID: 1, Name: "aaa"},
		{ID: 2, Name: "bbb"},
		{ID: 3, Name: "ccc"},
		{ID: 4, Name: "ddd"},
		{ID: 5, Name: "eee"},
		{ID: 6, Name: "fff"},
		{ID: 7, Name: "ggg"},
	}
	res, err := gollection.New(in).Filter(func(v User) bool {
		return v.ID > 5
	}).Result()
	fmt.Printf("%#v %#v\n", res.([]User), err)
}
Output:

[]gollection_test.User{gollection_test.User{ID:6, Name:"fff"}, gollection_test.User{ID:7, Name:"ggg"}} <nil>
Example (CustomTypeWithResultAs)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	type User struct {
		ID   int
		Name string
	}

	in := []User{
		{ID: 1, Name: "aaa"},
		{ID: 2, Name: "bbb"},
		{ID: 3, Name: "ccc"},
		{ID: 4, Name: "ddd"},
		{ID: 5, Name: "eee"},
		{ID: 6, Name: "fff"},
		{ID: 7, Name: "ggg"},
	}
	var res []User
	err := gollection.New(in).Filter(func(v User) bool {
		return v.ID > 5
	}).ResultAs(&res)
	fmt.Printf("%#v %#v\n", res, err)
}
Output:

[]gollection_test.User{gollection_test.User{ID:6, Name:"fff"}, gollection_test.User{ID:7, Name:"ggg"}} <nil>
Example (Distinct)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := []int{1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10}

	res, err := gollection.New(arr).Distinct().Result()
	fmt.Println(res, err)
}
Output:

[1 2 3 4 5 6 7 8 9 10] <nil>
Example (Filter)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	res, err := gollection.New(arr).Filter(func(v int) bool {
		return v > 5
	}).Result()
	fmt.Println(res, err)
}
Output:

[6 7 8 9 10] <nil>
Example (FlatMap)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := [][]int{
		{1, 2, 3},
		{4, 5},
		{6, 7, 8, 9, 10},
	}

	res, err := gollection.New(arr).FlatMap(func(v int) int {
		return v * 2
	}).Result()
	fmt.Println(res, err)
}
Output:

[2 4 6 8 10 12 14 16 18 20] <nil>
Example (Flatten)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := [][]int{
		{1, 2, 3},
		{4, 5},
		{6, 7, 8, 9, 10},
	}

	res, err := gollection.New(arr).Flatten().Result()
	fmt.Println(res, err)
}
Output:

[1 2 3 4 5 6 7 8 9 10] <nil>
Example (Fold)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	res, err := gollection.New(arr).Fold(100, func(v1, v2 int) int {
		return v1 + v2
	}).Result()
	fmt.Println(res, err)
}
Output:

155 <nil>
Example (Map)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	res, err := gollection.New(arr).Map(func(v int) int {
		return v * 2
	}).Result()
	fmt.Println(res, err)
}
Output:

[2 4 6 8 10 12 14 16 18 20] <nil>
Example (Reduce)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	res, err := gollection.New(arr).Reduce(func(v1, v2 int) int {
		return v1 + v2
	}).Result()
	fmt.Println(res, err)
}
Output:

55 <nil>
Example (Skip)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	res, err := gollection.New(arr).Skip(3).Result()
	fmt.Println(res, err)
	res, err = gollection.New(arr).Skip(30).Result()
	fmt.Println(res, err)
}
Output:

[4 5 6 7 8 9 10] <nil>
[] <nil>
Example (Sort)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1}

	res, err := gollection.New(arr).SortBy(func(v1, v2 int) bool {
		return v1 < v2
	}).Result()
	fmt.Println(arr)
	fmt.Println(res, err)
}
Output:

[1 2 3 4 5 6 7 8 9 10 9 8 7 6 5 4 3 2 1]
[1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 9 10] <nil>
Example (Take)
package main

import (
	"fmt"

	"github.com/azihsoyn/gollection"
)

func main() {
	arr := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

	res, err := gollection.New(arr).Take(3).Result()
	fmt.Println(res, err)
	res, err = gollection.New(arr).Take(30).Result()
	fmt.Println(res, err)
}
Output:

[1 2 3] <nil>
[1 2 3 4 5 6 7 8 9 10] <nil>

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(slice interface{}) *gollection

New returns a gollection instance which can method chain *sequentially* specified by some type of slice.

func NewStream added in v1.1.0

func NewStream(slice interface{}) *gollection

NewStream returns a gollection instance which can method chain *parallel* specified by some type of slice.

Types

This section is empty.

Jump to

Keyboard shortcuts

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