Help us shape the future of Go! Take the 2026 Go Developer Survey

ugo

package module
v0.0.0-...-33de225 Latest Latest
Warning

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

Go to latest
Published: Jun 30, 2016 License: MIT Imports: 5 Imported by: 0

README

GoDoc Go Report Card Travis CI

ugo

Simple and expressive toolbox written with love and care in Go.

Deeply inspired by underscore.js and has the same syntax and behaviour

Fully covered with tests, no surprises

Quick start

Installation

go get -u  github.com/alxrm/ugo

Import

import (
	u "github.com/alxrm/ugo"
)
Usage

It works with some special type, named Seq, which is an alias for []interface{}

So let's make some

	
// creates the new string Seq
strSeq := u.Seq{ "nineteen", "eigth", "four" } // ["nineteen" "eigth" "four"]
	
// creates the new Seq with given length
emptySeq := u.NewSeq(0); // []
	
// copies reflectively all of the values from int slice to Seq
intSlc := []int{ 4, 6, 2, 7, 8, 10, 9, 9, 120, 10, 2, 17 }
intSeq := u.From(intSlc, len(intSlc)) // [4 6 2 7 8 10 9 9 120 10 2 17]

Actually reflection approach in u.From can have an impact on performance 🐌 so use it with care.

Okay, now we have some Seq's,

What if I want to leave only unique elements in my int slice?

Go and get it ⚡


initialSeq := u.Seq{ 4, 6, 2, 7, 8, 10, 9, 9, 120, 10, 2, 17 }
uniquedSeq := u.Uniq(initial, func(l, r u.Object) int { return l.(int) - r.(int) })

fmt.Println(uniquedSeq) // [4 6 2 7 8 10 9 120 17]

And I want to leave only odd ones

Nice choice! 🔥

oddsSeq := u.Filter(uniquedSeq, func(cur, _, _ u.Object) bool { return cur.(int) % 2 != 0 })

fmt.Println(oddsSeq) // [7 9 17]

Well, sometimes you may want to use many method one by one, and it can be a bit ugly

Here we are, fluent syntax, just like in underscore.js 🚀

initialSeq := u.Seq{ 4, 6, 2, 7, 8, 10, 9, 9, 120, 10, 2, 17 }

res := u.Chain(initialSeq).Uniq(func(l, r u.Object) int {
	return l.(int) - r.(int)
}).Filter(func(cur, _, _ u.Object) bool {
	return cur.(int) % 2 != 0
}).Value()

fmt.Println(res) // [7 9 17]
Try it by yourself!

Explore all of the features and get your slice routine done faster

Contribute

We highly appreciate any of your pull requests and issues!

Be welcome ☕

Some of the TODOs:

  • Aliases from underscore
  • Chaining like map(...).uniq(...).result()
  • Better documentation
License
The MIT License (MIT)

Copyright (c) 2016 Alexey Derbyshev

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Documentation

Overview

Package ugo is a toolbox, inspired by underscore.js This package provide some of the underscore most used functions

Usage:

 package main

 import (
   u "github.com/alxrm/ugo"
 )

 func main() {
	  strArr := u.Seq{ "nineteen", "three", "eleven", "five", "seventy", "six", "seven", "one" }

	  lengths := u.Map(strArr, func(cur, _, _ u.Object) u.Object {
	  	return len(cur.(string))
	  })

	  fmt.Println(lengths) // Output: [8 5 6 4 7 3 5 3]
 }

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func All

func All(seq Seq, cb Predicate) bool

All is an alias for Every (see #Every)

func Any

func Any(seq Seq, cb Predicate) bool

Any is an alias for Some (see #Some)

func Contains

func Contains(seq Seq, target Object, isSorted bool, cb Comparator) bool

Contains returns true if slice contains element, which equals to passed one(target) NOTE: if slice is sorted, this method can use better search algorithm

func CountBy

func CountBy(seq Seq, cb Callback) (result map[string]int)

CountBy returns map, which values are count of certain kind of values, and keys are names of this kinds

func Each

func Each(seq Seq, cb Action)

Each Calls cb Action on each element

func EqualsNotStrict

func EqualsNotStrict(seqLeft, seqRight Seq, cb Comparator) bool

EqualsNotStrict checks whether both of the given slices are equal, but not strictly, e. g. they the same values, but positions can be different

func EqualsStrict

func EqualsStrict(seqLeft, seqRight Seq, cb Comparator) bool

EqualsStrict checks whether both of the given slices are strictly equal, e. g. they got the same values in the same positions

func Every

func Every(seq Seq, cb Predicate) bool

Every returns true if every element in slice have passed the predicate test

func FindIndex

func FindIndex(seq Seq, cb Predicate) int

FindIndex returns first found index, which value passed the predicate check

func FindLastIndex

func FindLastIndex(seq Seq, cb Predicate) int

FindLastIndex returns last found index, which value passed the predicate check

func ForEach

func ForEach(seq Seq, cb Action)

ForEach is an alias for Each (see #Each)

func GroupBy

func GroupBy(seq Seq, cb Callback) map[Object]Seq

GroupBy returns map, which keys are results of Callback calculation, and the value is the slice of elements, which gave such result

func Includes

func Includes(seq Seq, target Object, isSorted bool, cb Comparator) bool

Includes is an alias for Contains (see #Contains)

func IndexOf

func IndexOf(seq Seq, target Object, isSorted bool, cb Comparator) int

IndexOf founds index of the first element, which equals to passed one(target) NOTE: if slice is sorted, this method can use better search algorithm

func IsEmpty

func IsEmpty(seq Seq) bool

IsEmpty returns true if given slice has zero length or it's to nil

func IsSlice

func IsSlice(target Object) bool

IsSlice returns true if given object has type of slice

func LastIndexOf

func LastIndexOf(seq Seq, target Object, cb Comparator) int

LastIndexOf founds index of the last element, which equals to passed one(target)

func Random

func Random(min, max float64) int

Random returns the random number in given range

func Some

func Some(seq Seq, cb Predicate) bool

Some returns true if at least one element passed the predicate check

Types

type Action

type Action func(current, currentKey, src Object)

Action is an alias type for function, used to do some action based on given values, which has following args:

* Object current

* int index

* Seq list

type Callback

type Callback func(current, currentKey, src Object) Object

Callback is an alias type for function, used to get calculated result, which has following args:

* Object current

* int index

* Seq list

* returns Object: modified Seq element

type ChainWrapper

type ChainWrapper struct {
	Mid Seq    // Mid is for middleware calculations
	Res Object // Res if for resulting data
}

ChainWrapper is the special struct, containing resulting and middleware data

func Chain

func Chain(target Seq) *ChainWrapper

Chain method is a start point for chaining behaviour like that: u.Chain(Seq).Map(...).Filter(...).Reduce(...).Value()

func (*ChainWrapper) All

func (wrapper *ChainWrapper) All(cb Predicate) *ChainWrapper

All is a chaining wrapper for #All

func (*ChainWrapper) Any

func (wrapper *ChainWrapper) Any(cb Predicate) *ChainWrapper

Any is a chaining wrapper for #Any

func (*ChainWrapper) Collect

func (wrapper *ChainWrapper) Collect(cb Callback) *ChainWrapper

Collect is a chaining wrapper for #Collect

func (*ChainWrapper) Concat

func (wrapper *ChainWrapper) Concat(next Seq) *ChainWrapper

Concat is a chaining wrapper for #Concat

func (*ChainWrapper) Contains

func (wrapper *ChainWrapper) Contains(target Object, isSorted bool, cb Comparator) *ChainWrapper

Contains is a chaining wrapper for #Contains

func (*ChainWrapper) CountBy

func (wrapper *ChainWrapper) CountBy(cb Callback) *ChainWrapper

CountBy is a chaining wrapper for #CountBy

func (*ChainWrapper) Detect

func (wrapper *ChainWrapper) Detect(cb Predicate) *ChainWrapper

Detect is a chaining wrapper for #Detect

func (*ChainWrapper) Difference

func (wrapper *ChainWrapper) Difference(other Seq, cb Comparator) *ChainWrapper

Difference is a chaining wrapper for #Difference

func (*ChainWrapper) Each

func (wrapper *ChainWrapper) Each(cb Action) *ChainWrapper

Each is a chaining wrapper for #Each

func (*ChainWrapper) EqualsNotStrict

func (wrapper *ChainWrapper) EqualsNotStrict(other Seq, cb Comparator) *ChainWrapper

EqualsNotStrict is a chaining wrapper for #EqualsNotStrict

func (*ChainWrapper) EqualsStrict

func (wrapper *ChainWrapper) EqualsStrict(other Seq, cb Comparator) *ChainWrapper

EqualsStrict is a chaining wrapper for #EqualsStrict

func (*ChainWrapper) Every

func (wrapper *ChainWrapper) Every(cb Predicate) *ChainWrapper

Every is a chaining wrapper for #Every

func (*ChainWrapper) Filter

func (wrapper *ChainWrapper) Filter(cb Predicate) *ChainWrapper

Filter is a chaining wrapper for #Filter

func (*ChainWrapper) Find

func (wrapper *ChainWrapper) Find(cb Predicate) *ChainWrapper

Find is a chaining wrapper for #Find

func (*ChainWrapper) FindIndex

func (wrapper *ChainWrapper) FindIndex(cb Predicate) *ChainWrapper

FindIndex is a chaining wrapper for #FindIndex

func (*ChainWrapper) FindLast

func (wrapper *ChainWrapper) FindLast(cb Predicate) *ChainWrapper

FindLast is a chaining wrapper for #FindLast

func (*ChainWrapper) FindLastIndex

func (wrapper *ChainWrapper) FindLastIndex(cb Predicate) *ChainWrapper

FindLastIndex is a chaining wrapper for #FindLastIndex

func (*ChainWrapper) FoldL

func (wrapper *ChainWrapper) FoldL(cb Collector, initial Object) *ChainWrapper

FoldL is a chaining wrapper for #FoldL

func (*ChainWrapper) FoldR

func (wrapper *ChainWrapper) FoldR(cb Collector, initial Object) *ChainWrapper

FoldR is a chaining wrapper for #FoldR

func (*ChainWrapper) ForEach

func (wrapper *ChainWrapper) ForEach(cb Action) *ChainWrapper

ForEach is a chaining wrapper for #ForEach

func (*ChainWrapper) GroupBy

func (wrapper *ChainWrapper) GroupBy(cb Callback) *ChainWrapper

GroupBy is a chaining wrapper for #GroupBy

func (*ChainWrapper) Includes

func (wrapper *ChainWrapper) Includes(target Object, isSorted bool, cb Comparator) *ChainWrapper

Includes is a chaining wrapper for #Includes

func (*ChainWrapper) IndexOf

func (wrapper *ChainWrapper) IndexOf(target Object, isSorted bool, cb Comparator) *ChainWrapper

IndexOf is a chaining wrapper for #IndexOf

func (*ChainWrapper) Inject

func (wrapper *ChainWrapper) Inject(cb Collector, initial Object) *ChainWrapper

Inject is a chaining wrapper for #Inject

func (*ChainWrapper) Insert

func (wrapper *ChainWrapper) Insert(tg Object, pos int) *ChainWrapper

Insert is a chaining wrapper for #Insert

func (*ChainWrapper) Intersection

func (wrapper *ChainWrapper) Intersection(other Seq, cb Comparator) *ChainWrapper

Intersection is a chaining wrapper for #Intersection

func (*ChainWrapper) LastIndexOf

func (wrapper *ChainWrapper) LastIndexOf(target Object, cb Comparator) *ChainWrapper

LastIndexOf is a chaining wrapper for #LastIndexOf

func (*ChainWrapper) Map

func (wrapper *ChainWrapper) Map(cb Callback) *ChainWrapper

Map is a chaining wrapper for #Map

func (*ChainWrapper) Max

func (wrapper *ChainWrapper) Max(cb Comparator) *ChainWrapper

Max is a chaining wrapper for #Max

func (*ChainWrapper) Min

func (wrapper *ChainWrapper) Min(cb Comparator) *ChainWrapper

Min is a chaining wrapper for #Min

func (*ChainWrapper) Reduce

func (wrapper *ChainWrapper) Reduce(cb Collector, initial Object) *ChainWrapper

Reduce is a chaining wrapper for #Reduce

func (*ChainWrapper) ReduceRight

func (wrapper *ChainWrapper) ReduceRight(cb Collector, initial Object) *ChainWrapper

ReduceRight is a chaining wrapper for #ReduceRight

func (*ChainWrapper) Reject

func (wrapper *ChainWrapper) Reject(cb Predicate) *ChainWrapper

Reject is a chaining wrapper for #Reject

func (*ChainWrapper) Remove

func (wrapper *ChainWrapper) Remove(pos int) *ChainWrapper

Remove is a chaining wrapper for #Remove

func (*ChainWrapper) Reverse

func (wrapper *ChainWrapper) Reverse() *ChainWrapper

Reverse is a chaining wrapper for #Reverse

func (*ChainWrapper) Select

func (wrapper *ChainWrapper) Select(cb Predicate) *ChainWrapper

Select is a chaining wrapper for #Select

func (*ChainWrapper) Shuffle

func (wrapper *ChainWrapper) Shuffle() *ChainWrapper

Shuffle is a chaining wrapper for #Shuffle

func (*ChainWrapper) Some

func (wrapper *ChainWrapper) Some(cb Predicate) *ChainWrapper

Some is a chaining wrapper for #Some

func (*ChainWrapper) SortBy

func (wrapper *ChainWrapper) SortBy(cb Comparator) *ChainWrapper

SortBy is a chaining wrapper for #SortBy

func (*ChainWrapper) Union

func (wrapper *ChainWrapper) Union(other Seq, cb Comparator) *ChainWrapper

Union is a chaining wrapper for #Union

func (*ChainWrapper) Uniq

func (wrapper *ChainWrapper) Uniq(cb Comparator) *ChainWrapper

Uniq is a chaining wrapper for #Uniq

func (*ChainWrapper) Unique

func (wrapper *ChainWrapper) Unique(cb Comparator) *ChainWrapper

Unique is a chaining wrapper for #Unique

func (*ChainWrapper) Value

func (wrapper *ChainWrapper) Value() Object

Value returns result of calculations, you've done through chaining calls

func (*ChainWrapper) Without

func (wrapper *ChainWrapper) Without(nonGrata Object, cb Comparator) *ChainWrapper

Without is a chaining wrapper for #Without

type Collector

type Collector func(memo, current, currentKey, src Object) Object

Collector is an alias type for function, used in Reduce based methods, which has following args:

* Object memo

* Object current

* int index

* Seq list

* returns Object: memo, modified after some iteration

type Comparator

type Comparator func(left, right Object) int

Comparator is an alias type for function, used to compare one value with another one, which has following args:

* Object left

* Object right

* returns int: -1 for less, 0 for equals, 1 for larger

type Object

type Object interface{}

Object is an alias type for interface{}

func Detect

func Detect(seq Seq, cb Predicate) Object

Detect is an alias for Find (see #Find)

func Find

func Find(seq Seq, cb Predicate) Object

Find returns first found value, passed the predicate check

func FindLast

func FindLast(seq Seq, cb Predicate) Object

FindLast returns last found value, passed the predicate check

func FoldL

func FoldL(seq Seq, cb Collector, initial Object) Object

FoldL is an alias for Reduce (see #Reduce)

func FoldR

func FoldR(seq Seq, cb Collector, initial Object) Object

FoldR is an alias for Reduce (see #ReduceRight)

func Inject

func Inject(seq Seq, cb Collector, initial Object) Object

Inject is an alias for Reduce (see #Reduce)

func Max

func Max(seq Seq, cb Comparator) Object

Max returns max value from slice, calculated in comparator

func Min

func Min(seq Seq, cb Comparator) Object

Min returns min value from slice, calculated in comparator

func Reduce

func Reduce(seq Seq, cb Collector, initial Object) Object

Reduce makes single value from all of the slice elements, iterating from left

func ReduceRight

func ReduceRight(seq Seq, cb Collector, initial Object) Object

ReduceRight makes single value from all of the slice elements, iterating from right

type Predicate

type Predicate func(current, currentKey, src Object) bool

Predicate is an alias type for function, used to check value for some condition, which has following args:

* Object current

* int index

* Seq list

* returns bool: true if check has been passed

type Seq

type Seq []interface{}

Seq is an alias type for (generic) interface{} slice e. g. []interface{}

func Collect

func Collect(seq Seq, cb Callback) Seq

Collect is an alias for Map (see #Map)

func Concat

func Concat(seq, next Seq) Seq

Concat adds another slice to the end of given slice

func Difference

func Difference(seq, other Seq, cb Comparator) Seq

Difference returns the values from slice that are not present in the other slice

func Filter

func Filter(seq Seq, cb Predicate) Seq

Filter creates new slice, contains only elements that passed Predicate check

func From

func From(target Object, size int) Seq

From returns Seq from given object, filling the resulting Seq via reflection NOTE: You should possibly avoid using this one

func Insert

func Insert(seq Seq, target Object, position int) Seq

Insert pushes an element into given position in slice

func Intersection

func Intersection(seq, other Seq, cb Comparator) Seq

Intersection returns the values that are intersection of two slices Each value in the result is present in each of the arrays.

func Map

func Map(seq Seq, cb Callback) Seq

Map creates new slice same size, every element is the result of Callback

func NewSeq

func NewSeq(size int) Seq

NewSeq creates a new Seq instance with given size

func Reject

func Reject(seq Seq, cb Predicate) Seq

Reject creates new slice, contains only elements that haven't passed Predicate check

func Remove

func Remove(seq Seq, position int) Seq

Remove takes an element from given position in slice

func Reverse

func Reverse(seq Seq) Seq

Reverse returns reversed slice

func ReversedCopy

func ReversedCopy(seq Seq) Seq

ReversedCopy returns reversed copy of slice

func Select

func Select(seq Seq, cb Predicate) Seq

Select is an alias for Filter (see #Filter)

func Shuffle

func Shuffle(seq Seq) Seq

Shuffle returns shuffled slice

func ShuffledCopy

func ShuffledCopy(seq Seq) Seq

ShuffledCopy returns shuffled copy of slice

func SortBy

func SortBy(seq Seq, cb Comparator) Seq

SortBy returns sorted slice, uses very powerful timsort* algorithm *timsort obtained from: https://github.com/psilva261/timsort

func Union

func Union(seq, other Seq, cb Comparator) Seq

Union returns the unique values that are union of two slices each value in the result appears at least once in one of the passed slices

func Uniq

func Uniq(seq Seq, cb Comparator) Seq

Uniq returns slice, which contains only unique elements, calculated by Comparator

func Unique

func Unique(seq Seq, cb Comparator) Seq

Unique is an alias for Uniq (see #Uniq)

func Without

func Without(seq Seq, nonGrata Object, cb Comparator) Seq

Without returns the Slice without all instances of nonGrata value

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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