Skip to content

thoas/go-funk

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

go-funk

Build Status

GoDoc

Go report

go-funk is a modern Go library based on reflect.

Generic helpers rely on reflect, be careful this code runs exclusively on runtime so you must have a good test suite.

These helpers have started as an experiment to learn reflect. It may look like lodash in some aspects but it will have its own roadmap. lodash is an awesome library with a lot of work behind it, all features included in go-funk come from internal use cases.

You can also find typesafe implementation in the godoc.

Why this name?

Long story, short answer because func is a reserved word in Go, I wanted something similar.

Initially this project was named fn I don't need to explain why that was a bad idea for french speakers :)

Let's funk!

image

<3

Installation

Usage

These examples will be based on the following data model:

With fixtures:

You can import go-funk using a basic statement:

funk.Contains

Returns true if an element is present in a iteratee (slice, map, string).

One frustrating thing in Go is to implement contains methods for each type, for example:

this can be replaced by funk.Contains:

see also, typesafe implementations: ContainsInt, ContainsInt64, ContainsFloat32, ContainsFloat64, ContainsString

funk.Intersect

Returns the intersection between two collections.

see also, typesafe implementations: IntersectString

funk.Difference ..............

Returns the difference between two collections.

see also, typesafe implementations: DifferenceString

funk.IndexOf

Gets the index at which the first occurrence of a value is found in an array or return -1 if the value cannot be found.

see also, typesafe implementations: IndexOfInt, IndexOfInt64, IndexOfFloat32, IndexOfFloat64, IndexOfString

funk.LastIndexOf

Gets the index at which the last occurrence of a value is found in an array or return -1 if the value cannot be found.

see also, typesafe implementations: LastIndexOfInt, LastIndexOfInt64, LastIndexOfFloat32, LastIndexOfFloat64, LastIndexOfString

funk.ToMap

Transforms a slice or an array of structs to a map based on a pivot field.

funk.ToSet

Transforms an array or a slice to a set (a map with zero-size values).

funk.Filter

Filters a slice based on a predicate.

see also, typesafe implementations: FilterInt, FilterInt64, FilterFloat32, FilterFloat64, FilterString

funk.Reduce

Reduces an iteratee based on an accumulator function or operation rune for numbers.

funk.Find

Finds an element in a slice based on a predicate.

see also, typesafe implementations: FindInt, FindInt64, FindFloat32, FindFloat64, FindString

funk.Map

Manipulates an iteratee (map, slice) and transforms it to another type:

  • map -> slice
  • map -> map
  • slice -> map
  • slice -> slice

funk.FlatMap

Manipulates an iteratee (map, slice) and transforms it to to a flattened collection of another type:

  • map -> slice
  • slice -> slice

funk.Get

Retrieves the value at path of struct(s) or map(s).

funk.Get also support map values:

funk.Get also handles nil values:

funk.GetOrElse

Retrieves the value of the pointer or default.

funk.Set

Set value at a path of a struct

funk.MustSet

Short hand for funk.Set if struct does not contain interface{} field type to discard errors.

funk.Prune

Copy a struct with only selected fields. Slice is handled by pruning all elements.

funk.PruneByTag .......... Same functionality as funk.Prune, but uses struct tags instead of struct field names.

funk.Keys

Creates an array of the own enumerable map keys or struct field names.

funk.Values

Creates an array of the own enumerable map values or struct field values.

funk.ForEach

Range over an iteratee (map, slice).

Or update element in slice(Not map, reflect#Value#MapIndex#CanSet is false).

funk.ForEachRight ............

Range over an iteratee (map, slice) from the right.

funk.Chunk

Creates an array of elements split into groups with the length of the size. If array can't be split evenly, the final chunk will be the remaining element.

funk.FlattenDeep

Recursively flattens an array.

funk.Uniq

Creates an array with unique values.

see also, typesafe implementations: UniqInt, UniqInt64, UniqFloat32, UniqFloat64, UniqString

funk.UniqBy .........

Creates an array with unique values returned by a callback.

funk.Drop

Creates an array/slice with n elements dropped from the beginning.

see also, typesafe implementations: DropInt, DropInt32, DropInt64, DropFloat32, DropFloat64, DropString

funk.Initial

Gets all but the last element of array.

funk.Tail

Gets all but the first element of array.

funk.Shuffle

Creates an array of shuffled values.

see also, typesafe implementations: ShuffleInt, ShuffleInt64, ShuffleFloat32, ShuffleFloat64, ShuffleString

funk.Subtract

Returns the subtraction between two collections. It preserve order.

see also, typesafe implementations: SubtractString

funk.Sum

Computes the sum of the values in an array.

see also, typesafe implementations: SumInt, SumInt64, SumFloat32, SumFloat64

funk.Reverse

Transforms an array such that the first element will become the last, the second element will become the second to last, etc.

see also, typesafe implementations: ReverseInt, ReverseInt64, ReverseFloat32, ReverseFloat64, ReverseString, ReverseStrings

funk.SliceOf

Returns a slice based on an element.

funk.RandomInt

Generates a random int, based on a min and max values.

funk.RandomString

Generates a random string with a fixed length.

funk.Shard

Generates a sharded string with a fixed length and depth.

funk.Subset

Returns true if a collection is a subset of another

Performance

go-funk currently has an open issue about performance, don't hesitate to participate in the discussion to enhance the generic helpers implementations.

Let's stop beating around the bush, a typesafe implementation in pure Go of funk.Contains, let's say for example:

will always outperform an implementation based on reflect in terms of speed and allocs because of how it's implemented in the language.

If you want a similarity, gorm will always be slower than sqlx (which is very low level btw) and will use more allocs.

You must not think generic helpers of go-funk as a replacement when you are dealing with performance in your codebase, you should use typesafe implementations instead.

Contributing

Don't hesitate ;)

Authors

  • Florent Messa
  • Gilles Fabio
  • Alexey Pokhozhaev
  • Alexandre Nicolaie