join

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2017 License: MIT Imports: 1 Imported by: 0

Documentation

Overview

Package join implements a generic interface for matching elements from two slices similar in spirit to a database Join.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type List

type List interface {
	Len() int
	Get(int) interface{}
}

List simply requires implementing types to allow access to their contained values by integer index.

type Pair

type Pair struct {
	L, R interface{}
}

A Pair represents an element from the left slice and an element from the right slice, that have been matched by a join.

func HashJoin

func HashJoin(lSlice, rSlice List, lKey, rKey func(interface{}) interface{}) (
	pairs []Pair, lonelyLefts, lonelyRights []interface{})

HashJoin attempts to match each element in `lSlice` with an element in `rSlice` by performing a hash join. If such a match is found for a given element of `lSlice`, it is returned as an element of `pairs`, while leftover elements from `lSlice` and `rSlice` that couldn`t be matched are returned as elements of `lonelyLefts` and `lonelyRights` respectively. The join keys for `lSlice` and `rSlice` are defined by the passed in `lKey` and `rKey` functions, respectively.

If `lKey` or `rKey` are nil, the elements of the respective slices are used directly as keys instead.

func Join

func Join(lSlice, rSlice interface{}, score func(left, right interface{}) int) (
	pairs []Pair, lonelyLefts, lonelyRights []interface{})

Join attempts to match each element in `lSlice` with an element in `rSlice` in accordance with a score function. If such a match is found, it is returned as an element of `pairs`, while leftover elements from `lSlice` and `rSlice` that couldn`t be matched, are returned as elements of `lonelyLefts` and `lonelyRights` respectively. Both `lSlice` and `rSlice` must be slice or array types, but they do not necessarily have to have the same type.

Matches are made in accordance with the provided `score` function. It takes a single element from `lSlice`, and a single element from `rSlice`, and computes a score suggesting their match preference. The algorithm prefers to match pairs with the the score closest to zero (inclusive). Negative scores are never matched.

Example
lefts := []string{"a", "bc", "def"}
rights := []int{0, 2, 4}
score := func(left, right interface{}) int {
	return len(left.(string)) - right.(int)
}
pairs, lonelyLefts, lonelyRights := Join(lefts, rights, score)

fmt.Println(pairs, lonelyLefts, lonelyRights)
Output:

[{a 0} {bc 2}] [def] [4]

type StringSlice

type StringSlice []string

StringSlice is an alias for []string to allow for joins

func (StringSlice) Get

func (ss StringSlice) Get(ii int) interface{}

Get returns the value contained at the given index

func (StringSlice) Len

func (ss StringSlice) Len() int

Len returns the number of items in the slice

Jump to

Keyboard shortcuts

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