collections

package module
v0.0.0-...-25ff99f Latest Latest
Warning

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

Go to latest
Published: Jul 25, 2015 License: MIT Imports: 0 Imported by: 1 module, 1 package

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

go-collections

This project ports map-filter-reduce feature to Go.

Supported features

Map

Given a collection it's trivial creating a new one editing each element on the fly.

planets := []interface{}{
    "Mercury",
    "Venus",
    "Earth",
    "Mars",
    "Jupiter",
    "Saturn",
    "Uranus",
    "Neptune",
    "Pluto",
}

coll := collections.NewFromSlice(planets)
coll = coll.Map(func(v interface{}) interface{} {
    return strings.Join([]string{ "Hello ", v.(string) })
})

fmt.Println(coll)

Output:

    "Hello Mercury"
    "Hello Venus"
    "Hello Earth"
    "Hello Mars"
    "Hello Jupiter"
    "Hello Saturn"
    "Hello Uranus"
    "Hello Neptune"
    "Hello Pluto"
Filter

Filtering a for even numbers only:

numbers := []interface{}{
    1,
    3,
    5,
    6,
    7,
    8,
    9,
    10,
}

coll := collections.NewFromSlice(numbers)
coll = coll.Filter(func(v interface{}) bool {
    return v.(int) % 2 == 0
})

Output:

    6,
    8,
    10,
Reduce

Reducing for minimum number:

numbers := []interface{}{
    1,
    3,
    5,
    6,
    7,
    8,
    9,
    10,
}

coll := collections.NewFromSlice(numbers)
min  := collections.Reduce(0, func(a, b interface{}) interface{} {
    if a < b { return a } else { return b }
})

Output:

1

A complete example

The following example assumes a list of ages and returns the average of the ages greater or equal to 21.

package main

import (
  "github.com/alediaferia/go-collections"
  "fmt"
)

func main() {
  ages := []interface{}{
    3,
    4,
    8,
    17,
    22,
    28,
    34,
    65,
    32,
    24,
  }

  coll := collections.NewFromSlice(ages)
  count := 0
  fmt.Printf("The average age of all the adult people at the party is: %d\n", coll.Filter(func(v interface{}) bool {
    return v.(int) >= 21
  }).Map(func(v interface{}) interface{} {
    count += 1
    return v
  }).Reduce(0, func(a, b interface{}) interface{} {
    return a.(int) + b.(int)
  }).(int) / count) 
}

License

The code in this codebase is provided as-is as per the MIT License included here.

Copyright (c) Alessandro Diaferia 2015

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Interfaces

type Interfaces struct {
	// contains filtered or unexported fields
}

func NewFromSlice

func NewFromSlice(values []interface{}) *Interfaces

func (*Interfaces) Filter

func (this *Interfaces) Filter(filter InterfacesFilter) *Interfaces

func (*Interfaces) Map

func (this *Interfaces) Map(mapper InterfacesMapper) *Interfaces

func (*Interfaces) Reduce

func (this *Interfaces) Reduce(identity interface{}, reducer InterfacesReducer) interface{}

type InterfacesFilter

type InterfacesFilter func(value interface{}) bool

type InterfacesMapper

type InterfacesMapper func(value interface{}) interface{}

type InterfacesReducer

type InterfacesReducer func(a, b interface{}) interface{}

Jump to

Keyboard shortcuts

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