...

Text file src/github.com/thoas/go-funk/README.rst

Documentation: github.com/thoas/go-funk

     1go-funk
     2=======
     3
     4.. image:: https://secure.travis-ci.org/thoas/go-funk.svg?branch=master
     5    :alt: Build Status
     6    :target: http://travis-ci.org/thoas/go-funk
     7
     8.. image:: https://godoc.org/github.com/thoas/go-funk?status.svg
     9    :alt: GoDoc
    10    :target: https://pkg.go.dev/github.com/thoas/go-funk
    11
    12.. image:: https://goreportcard.com/badge/github.com/thoas/go-funk
    13    :alt: Go report
    14    :target: https://goreportcard.com/report/github.com/thoas/go-funk
    15
    16``go-funk`` is a modern Go library based on reflect_.
    17
    18Generic helpers rely on reflect_, be careful this code runs exclusively on runtime so you must have a good test suite.
    19
    20These helpers have started as an experiment to learn reflect_. It may look like lodash_ in some aspects but
    21it will have its own roadmap. lodash_ is an awesome library with a lot of work behind it, all features included in
    22``go-funk`` come from internal use cases.
    23
    24You can also find typesafe implementation in the godoc_.
    25
    26Why this name?
    27--------------
    28
    29Long story, short answer because ``func`` is a reserved word in Go, I wanted something similar.
    30
    31Initially this project was named ``fn`` I don't need to explain why that was a bad idea for french speakers :)
    32
    33Let's ``funk``!
    34
    35.. image:: https://media.giphy.com/media/3oEjHQKtDXpeGN9rW0/giphy.gif
    36
    37<3
    38
    39Installation
    40------------
    41
    42.. code-block:: bash
    43
    44    go get github.com/thoas/go-funk
    45
    46Usage
    47-----
    48
    49.. code-block:: go
    50
    51    import "github.com/thoas/go-funk"
    52
    53These examples will be based on the following data model:
    54
    55.. code-block:: go
    56
    57    type Foo struct {
    58        ID        int
    59        FirstName string `tag_name:"tag 1"`
    60        LastName  string `tag_name:"tag 2"`
    61        Age       int    `tag_name:"tag 3"`
    62    }
    63
    64    func (f Foo) TableName() string {
    65        return "foo"
    66    }
    67
    68With fixtures:
    69
    70.. code-block:: go
    71
    72    f := &Foo{
    73        ID:        1,
    74        FirstName: "Foo",
    75        LastName:  "Bar",
    76        Age:       30,
    77    }
    78
    79You can import ``go-funk`` using a basic statement:
    80
    81.. code-block:: go
    82
    83    import "github.com/thoas/go-funk"
    84
    85funk.Contains
    86.............
    87
    88Returns true if an element is present in a iteratee (slice, map, string).
    89
    90One frustrating thing in Go is to implement ``contains`` methods for each type, for example:
    91
    92.. code-block:: go
    93
    94    func ContainsInt(s []int, e int) bool {
    95        for _, a := range s {
    96            if a == e {
    97                return true
    98            }
    99        }
   100        return false
   101    }
   102
   103this can be replaced by ``funk.Contains``:
   104
   105.. code-block:: go
   106
   107    // slice of string
   108    funk.Contains([]string{"foo", "bar"}, "bar") // true
   109
   110    // slice of Foo ptr
   111    funk.Contains([]*Foo{f}, f) // true
   112    funk.Contains([]*Foo{f}, func (foo *Foo) bool {
   113        return foo.ID == f.ID
   114    }) // true
   115    funk.Contains([]*Foo{f}, nil) // false
   116
   117    b := &Foo{
   118        ID:        2,
   119        FirstName: "Florent",
   120        LastName:  "Messa",
   121        Age:       28,
   122    }
   123
   124    funk.Contains([]*Foo{f}, b) // false
   125
   126    // string
   127    funk.Contains("florent", "rent") // true
   128    funk.Contains("florent", "foo") // false
   129
   130    // even map
   131    funk.Contains(map[int]string{1: "Florent"}, 1) // true
   132    funk.Contains(map[int]string{1: "Florent"}, func(key int, name string) bool {
   133        return key == 1 // or `name == "Florent"` for the value type
   134    }) // true
   135
   136see also, typesafe implementations: ContainsInt_, ContainsInt64_, ContainsFloat32_, ContainsFloat64_, ContainsString_
   137
   138.. _ContainsFloat32: https://godoc.org/github.com/thoas/go-funk#ContainsFloat32
   139.. _ContainsFloat64: https://godoc.org/github.com/thoas/go-funk#ContainsFloat64
   140.. _ContainsInt: https://godoc.org/github.com/thoas/go-funk#ContainsInt
   141.. _ContainsInt64: https://godoc.org/github.com/thoas/go-funk#ContainsInt64
   142.. _ContainsString: https://godoc.org/github.com/thoas/go-funk#ContainsString
   143
   144funk.Intersect
   145..............
   146
   147Returns the intersection between two collections.
   148
   149.. code-block:: go
   150
   151    funk.Intersect([]int{1, 2, 3, 4}, []int{2, 4, 6})  // []int{2, 4}
   152    funk.Intersect([]string{"foo", "bar", "hello", "bar"}, []string{"foo", "bar"})  // []string{"foo", "bar"}
   153
   154see also, typesafe implementations: IntersectString
   155
   156.. IntersectString: https://godoc.org/github.com/thoas/go-funk#IntersectString
   157
   158
   159funk.Difference
   160..............
   161
   162Returns the difference between two collections.
   163
   164.. code-block:: go
   165
   166    funk.Difference([]int{1, 2, 3, 4}, []int{2, 4, 6})  // []int{1, 3}, []int{6}
   167    funk.Difference([]string{"foo", "bar", "hello", "bar"}, []string{"foo", "bar"})  // []string{"hello"}, []string{}
   168
   169see also, typesafe implementations: DifferenceString
   170
   171.. DifferenceString: https://godoc.org/github.com/thoas/go-funk#DifferenceString
   172
   173
   174funk.IndexOf
   175............
   176
   177Gets the index at which the first occurrence of a value is found in an array or return -1
   178if the value cannot be found.
   179
   180.. code-block:: go
   181
   182    // slice of string
   183    funk.IndexOf([]string{"foo", "bar"}, "bar") // 1
   184    funk.IndexOf([]string{"foo", "bar"}, func(value string) bool {
   185        return value == "bar"
   186    }) // 1
   187    funk.IndexOf([]string{"foo", "bar"}, "gilles") // -1
   188
   189see also, typesafe implementations: IndexOfInt_, IndexOfInt64_, IndexOfFloat32_, IndexOfFloat64_, IndexOfString_
   190
   191.. _IndexOfFloat32: https://godoc.org/github.com/thoas/go-funk#IndexOfFloat32
   192.. _IndexOfFloat64: https://godoc.org/github.com/thoas/go-funk#IndexOfFloat64
   193.. _IndexOfInt: https://godoc.org/github.com/thoas/go-funk#IndexOfInt
   194.. _IndexOfInt64: https://godoc.org/github.com/thoas/go-funk#IndexOfInt64
   195.. _IndexOfString: https://godoc.org/github.com/thoas/go-funk#IndexOfString
   196
   197funk.LastIndexOf
   198................
   199
   200Gets the index at which the last occurrence of a value is found in an array or return -1
   201if the value cannot be found.
   202
   203.. code-block:: go
   204
   205    // slice of string
   206    funk.LastIndexOf([]string{"foo", "bar", "bar"}, "bar") // 2
   207    funk.LastIndexOf([]string{"foo", "bar"}, func(value string) bool {
   208        return value == "bar"
   209    }) // 2
   210    funk.LastIndexOf([]string{"foo", "bar"}, "gilles") // -1
   211
   212see also, typesafe implementations: LastIndexOfInt_, LastIndexOfInt64_, LastIndexOfFloat32_, LastIndexOfFloat64_, LastIndexOfString_
   213
   214.. _LastIndexOfFloat32: https://godoc.org/github.com/thoas/go-funk#LastIndexOfFloat32
   215.. _LastIndexOfFloat64: https://godoc.org/github.com/thoas/go-funk#LastIndexOfFloat64
   216.. _LastIndexOfInt: https://godoc.org/github.com/thoas/go-funk#LastIndexOfInt
   217.. _LastIndexOfInt64: https://godoc.org/github.com/thoas/go-funk#LastIndexOfInt64
   218.. _LastIndexOfString: https://godoc.org/github.com/thoas/go-funk#LastIndexOfString
   219
   220funk.ToMap
   221..........
   222
   223Transforms a slice of structs to a map based on a ``pivot`` field.
   224
   225.. code-block:: go
   226
   227    f := &Foo{
   228        ID:        1,
   229        FirstName: "Gilles",
   230        LastName:  "Fabio",
   231        Age:       70,
   232    }
   233
   234    b := &Foo{
   235        ID:        2,
   236        FirstName: "Florent",
   237        LastName:  "Messa",
   238        Age:       80,
   239    }
   240
   241    results := []*Foo{f, b}
   242
   243    mapping := funk.ToMap(results, "ID") // map[int]*Foo{1: f, 2: b}
   244
   245funk.Filter
   246...........
   247
   248Filters a slice based on a predicate.
   249
   250.. code-block:: go
   251
   252    r := funk.Filter([]int{1, 2, 3, 4}, func(x int) bool {
   253        return x%2 == 0
   254    }) // []int{2, 4}
   255
   256see also, typesafe implementations: FilterInt_, FilterInt64_, FilterFloat32_, FilterFloat64_, FilterString_
   257
   258.. _FilterFloat32: https://godoc.org/github.com/thoas/go-funk#FilterFloat32
   259.. _FilterFloat64: https://godoc.org/github.com/thoas/go-funk#FilterFloat64
   260.. _FilterInt: https://godoc.org/github.com/thoas/go-funk#FilterInt
   261.. _FilterInt64: https://godoc.org/github.com/thoas/go-funk#FilterInt64
   262.. _FilterString: https://godoc.org/github.com/thoas/go-funk#FilterString
   263
   264funk.Reduce
   265...........
   266
   267Reduces an iteratee based on an accumulator function or operation rune for numbers.
   268
   269.. code-block:: go
   270
   271    // Using operation runes. '+' and '*' only supported.
   272    r := funk.Reduce([]int{1, 2, 3, 4}, '+', float64(0)) // 10
   273    r := funk.Reduce([]int{1, 2, 3, 4}, '*', 1) // 24
   274
   275    // Using accumulator function
   276    r := funk.Reduce([]int{1, 2, 3, 4}, func(acc float64, num int) float64 {
   277        return acc + float64(num)
   278    }, float64(0)) // 10
   279
   280    r := funk.Reduce([]int{1, 2, 3, 4}, func(acc string, num int) string {
   281        return acc + fmt.Sprint(num)
   282    }, "") // "1234"
   283
   284funk.Find
   285.........
   286
   287Finds an element in a slice based on a predicate.
   288
   289.. code-block:: go
   290
   291    r := funk.Find([]int{1, 2, 3, 4}, func(x int) bool {
   292        return x%2 == 0
   293    }) // 2
   294
   295see also, typesafe implementations: FindInt_, FindInt64_, FindFloat32_, FindFloat64_, FindString_
   296
   297.. _FindFloat32: https://godoc.org/github.com/thoas/go-funk#FindFloat32
   298.. _FindFloat64: https://godoc.org/github.com/thoas/go-funk#FindFloat64
   299.. _FindInt: https://godoc.org/github.com/thoas/go-funk#FindInt
   300.. _FindInt64: https://godoc.org/github.com/thoas/go-funk#FindInt64
   301.. _FindString: https://godoc.org/github.com/thoas/go-funk#FindString
   302
   303funk.Map
   304........
   305
   306Manipulates an iteratee (map, slice) and transforms it to another type:
   307
   308* map -> slice
   309* map -> map
   310* slice -> map
   311* slice -> slice
   312
   313.. code-block:: go
   314
   315    r := funk.Map([]int{1, 2, 3, 4}, func(x int) int {
   316        return x * 2
   317    }) // []int{2, 4, 6, 8}
   318
   319    r := funk.Map([]int{1, 2, 3, 4}, func(x int) string {
   320        return "Hello"
   321    }) // []string{"Hello", "Hello", "Hello", "Hello"}
   322
   323    r = funk.Map([]int{1, 2, 3, 4}, func(x int) (int, int) {
   324        return x, x
   325    }) // map[int]int{1: 1, 2: 2, 3: 3, 4: 4}
   326
   327    mapping := map[int]string{
   328        1: "Florent",
   329        2: "Gilles",
   330    }
   331
   332    r = funk.Map(mapping, func(k int, v string) int {
   333        return k
   334    }) // []int{1, 2}
   335
   336    r = funk.Map(mapping, func(k int, v string) (string, string) {
   337        return fmt.Sprintf("%d", k), v
   338    }) // map[string]string{"1": "Florent", "2": "Gilles"}
   339
   340funk.FlatMap
   341............
   342
   343Manipulates an iteratee (map, slice) and transforms it to to a flattened collection of another type:
   344
   345* map -> slice
   346* slice -> slice
   347
   348.. code-block:: go
   349
   350    r := funk.FlatMap([][]int{{1, 2}, {3, 4}}, func(x []int) []int {
   351        return append(x, 0)
   352    }) // []int{1, 2, 0, 3, 4, 0}
   353
   354    mapping := map[string][]int{
   355        "Florent": {1, 2},
   356        "Gilles": {3, 4},
   357    }
   358
   359    r = funk.FlatMap(mapping, func(k string, v []int) []int {
   360        return v
   361    }) // []int{1, 2, 3, 4}
   362
   363funk.Get
   364........
   365
   366Retrieves the value at path of struct(s) or map(s).
   367
   368.. code-block:: go
   369
   370    var bar *Bar = &Bar{
   371        Name: "Test",
   372        Bars: []*Bar{
   373            &Bar{
   374                Name: "Level1-1",
   375                Bar: &Bar{
   376                    Name: "Level2-1",
   377                },
   378            },
   379            &Bar{
   380                Name: "Level1-2",
   381                Bar: &Bar{
   382                    Name: "Level2-2",
   383                },
   384            },
   385        },
   386    }
   387
   388    var foo *Foo = &Foo{
   389        ID:        1,
   390        FirstName: "Dark",
   391        LastName:  "Vador",
   392        Age:       30,
   393        Bar:       bar,
   394        Bars: []*Bar{
   395            bar,
   396            bar,
   397        },
   398    }
   399
   400    funk.Get([]*Foo{foo}, "Bar.Bars.Bar.Name") // []string{"Level2-1", "Level2-2"}
   401    funk.Get(foo, "Bar.Bars.Bar.Name") // []string{"Level2-1", "Level2-2"}
   402    funk.Get(foo, "Bar.Name") // Test
   403
   404``funk.Get`` also support ``map`` values:
   405
   406.. code-block:: go
   407
   408    bar := map[string]interface{}{
   409        "Name": "Test",
   410    }
   411
   412    foo1 := map[string]interface{}{
   413        "ID":        1,
   414        "FirstName": "Dark",
   415        "LastName":  "Vador",
   416        "Age":       30,
   417        "Bar":       bar,
   418    }
   419
   420    foo2 := &map[string]interface{}{
   421        "ID":        1,
   422        "FirstName": "Dark",
   423        "LastName":  "Vador",
   424        "Age":       30,
   425    } // foo2.Bar is nil
   426
   427    funk.Get(bar, "Name") // "Test"
   428    funk.Get([]map[string]interface{}{foo1, foo2}, "Bar.Name") // []string{"Test"}
   429    funk.Get(foo2, "Bar.Name") // nil
   430
   431
   432``funk.Get`` also handles ``nil`` values:
   433
   434.. code-block:: go
   435
   436    bar := &Bar{
   437        Name: "Test",
   438    }
   439
   440    foo1 := &Foo{
   441        ID:        1,
   442        FirstName: "Dark",
   443        LastName:  "Vador",
   444        Age:       30,
   445        Bar:       bar,
   446    }
   447
   448    foo2 := &Foo{
   449        ID:        1,
   450        FirstName: "Dark",
   451        LastName:  "Vador",
   452        Age:       30,
   453    } // foo2.Bar is nil
   454
   455    funk.Get([]*Foo{foo1, foo2}, "Bar.Name") // []string{"Test"}
   456    funk.Get(foo2, "Bar.Name") // nil
   457
   458
   459
   460funk.GetOrElse
   461..............
   462
   463Retrieves the value of the pointer or default.
   464
   465.. code-block:: go
   466
   467    str := "hello world"
   468    GetOrElse(&str, "foobar")   // string{"hello world"}
   469    GetOrElse(str, "foobar")    // string{"hello world"}
   470    GetOrElse(nil, "foobar")    // string{"foobar"}
   471
   472funk.Set
   473........
   474Set value at a path of a struct
   475
   476.. code-block:: go
   477
   478    var bar Bar = Bar{
   479        Name: "level-0", 
   480        Bar: &Bar{
   481            Name: "level-1",
   482            Bars: []*Bar{
   483                {Name: "level2-1"},
   484                {Name: "level2-2"},
   485            },
   486        },
   487    }
   488
   489    _ = Set(&bar, "level-0-new", "Name")
   490    fmt.Println(bar.Name) // "level-0-new"
   491
   492    MustSet(&bar, "level-1-new", "Bar.Name")
   493    fmt.Println(bar.Bar.Name) // "level-1-new"
   494
   495    Set(&bar, "level-2-new", "Bar.Bars.Name")
   496    fmt.Println(bar.Bar.Bars[0].Name) // "level-2-new"
   497    fmt.Println(bar.Bar.Bars[1].Name) // "level-2-new"
   498
   499funk.MustSet
   500............
   501Short hand for funk.Set if struct does not contain interface{} field type to discard errors.
   502
   503funk.Prune
   504..........
   505Copy a struct with only selected fields. Slice is handled by pruning all elements.
   506
   507.. code-block:: go
   508
   509    bar := &Bar{
   510        Name: "Test",
   511    }
   512
   513    foo1 := &Foo{
   514        ID:        1,
   515        FirstName: "Dark",
   516        LastName:  "Vador",
   517        Bar:       bar,
   518    }
   519
   520    pruned, _ := Prune(foo1, []string{"FirstName", "Bar.Name"})
   521    // *Foo{
   522    //    ID:        0,
   523    //    FirstName: "Dark",
   524    //    LastName:  "",
   525    //    Bar:       &Bar{Name: "Test},
   526    // }
   527
   528funk.PruneByTag
   529..........
   530Same functionality as funk.Prune, but uses struct tags instead of struct field names.
   531
   532funk.Keys
   533.........
   534
   535Creates an array of the own enumerable map keys or struct field names.
   536
   537.. code-block:: go
   538
   539    funk.Keys(map[string]int{"one": 1, "two": 2}) // []string{"one", "two"} (iteration order is not guaranteed)
   540
   541    foo := &Foo{
   542        ID:        1,
   543        FirstName: "Dark",
   544        LastName:  "Vador",
   545        Age:       30,
   546    }
   547
   548    funk.Keys(foo) // []string{"ID", "FirstName", "LastName", "Age"} (iteration order is not guaranteed)
   549
   550funk.Values
   551...........
   552
   553Creates an array of the own enumerable map values or struct field values.
   554
   555.. code-block:: go
   556
   557    funk.Values(map[string]int{"one": 1, "two": 2}) // []string{1, 2} (iteration order is not guaranteed)
   558
   559    foo := &Foo{
   560        ID:        1,
   561        FirstName: "Dark",
   562        LastName:  "Vador",
   563        Age:       30,
   564    }
   565
   566    funk.Values(foo) // []interface{}{1, "Dark", "Vador", 30} (iteration order is not guaranteed)
   567
   568funk.ForEach
   569............
   570
   571Range over an iteratee (map, slice).
   572
   573.. code-block:: go
   574
   575    funk.ForEach([]int{1, 2, 3, 4}, func(x int) {
   576        fmt.Println(x)
   577    })
   578
   579funk.ForEachRight
   580............
   581
   582Range over an iteratee (map, slice) from the right.
   583
   584.. code-block:: go
   585
   586    results := []int{}
   587
   588    funk.ForEachRight([]int{1, 2, 3, 4}, func(x int) {
   589        results = append(results, x)
   590    })
   591
   592    fmt.Println(results) // []int{4, 3, 2, 1}
   593
   594funk.Chunk
   595..........
   596
   597Creates an array of elements split into groups with the length of the size.
   598If array can't be split evenly, the final chunk will be the remaining element.
   599
   600.. code-block:: go
   601
   602    funk.Chunk([]int{1, 2, 3, 4, 5}, 2) // [][]int{[]int{1, 2}, []int{3, 4}, []int{5}}
   603
   604funk.FlattenDeep
   605................
   606
   607Recursively flattens an array.
   608
   609.. code-block:: go
   610
   611    funk.FlattenDeep([][]int{[]int{1, 2}, []int{3, 4}}) // []int{1, 2, 3, 4}
   612
   613funk.Uniq
   614.........
   615
   616Creates an array with unique values.
   617
   618.. code-block:: go
   619
   620    funk.Uniq([]int{0, 1, 1, 2, 3, 0, 0, 12}) // []int{0, 1, 2, 3, 12}
   621
   622see also, typesafe implementations: UniqInt_, UniqInt64_, UniqFloat32_, UniqFloat64_, UniqString_
   623
   624.. _UniqFloat32: https://godoc.org/github.com/thoas/go-funk#UniqFloat32
   625.. _UniqFloat64: https://godoc.org/github.com/thoas/go-funk#UniqFloat64
   626.. _UniqInt: https://godoc.org/github.com/thoas/go-funk#UniqInt
   627.. _UniqInt64: https://godoc.org/github.com/thoas/go-funk#UniqInt64
   628.. _UniqString: https://godoc.org/github.com/thoas/go-funk#UniqString
   629
   630funk.Drop
   631.........
   632
   633Creates an array/slice with `n` elements dropped from the beginning.
   634
   635.. code-block:: go
   636
   637    funk.Drop([]int{0, 0, 0, 0}, 3) // []int{0}
   638
   639see also, typesafe implementations: DropInt_, DropInt32_, DropInt64_, DropFloat32_, DropFloat64_, DropString_
   640
   641.. _DropInt: https://godoc.org/github.com/thoas/go-funk#DropInt
   642.. _DropInt32: https://godoc.org/github.com/thoas/go-funk#DropInt64
   643.. _DropInt64: https://godoc.org/github.com/thoas/go-funk#DropInt64
   644.. _DropFloat32: https://godoc.org/github.com/thoas/go-funk#DropFloat32
   645.. _DropFloat64: https://godoc.org/github.com/thoas/go-funk#DropFloat64
   646.. _DropString: https://godoc.org/github.com/thoas/go-funk#DropString
   647
   648funk.Initial
   649............
   650
   651Gets all but the last element of array.
   652
   653.. code-block:: go
   654
   655    funk.Initial([]int{0, 1, 2, 3, 4}) // []int{0, 1, 2, 3}
   656
   657funk.Tail
   658.........
   659
   660Gets all but the first element of array.
   661
   662.. code-block:: go
   663
   664    funk.Tail([]int{0, 1, 2, 3, 4}) // []int{1, 2, 3, 4}
   665
   666funk.Shuffle
   667............
   668
   669Creates an array of shuffled values.
   670
   671.. code-block:: go
   672
   673    funk.Shuffle([]int{0, 1, 2, 3, 4}) // []int{2, 1, 3, 4, 0}
   674
   675
   676see also, typesafe implementations: ShuffleInt_, ShuffleInt64_, ShuffleFloat32_, ShuffleFloat64_, ShuffleString_
   677
   678.. _ShuffleFloat32: https://godoc.org/github.com/thoas/go-funk#ShuffleFloat32
   679.. _ShuffleFloat64: https://godoc.org/github.com/thoas/go-funk#ShuffleFloat64
   680.. _ShuffleInt: https://godoc.org/github.com/thoas/go-funk#ShuffleInt
   681.. _ShuffleInt64: https://godoc.org/github.com/thoas/go-funk#ShuffleInt64
   682.. _ShuffleString: https://godoc.org/github.com/thoas/go-funk#ShuffleString
   683
   684funk.Subtract
   685.............
   686
   687Returns the subtraction between two collections. It preserve order.
   688
   689.. code-block:: go
   690
   691    funk.Subtract([]int{0, 1, 2, 3, 4}, []int{0, 4}) // []int{1, 2, 3}
   692    funk.Subtract([]int{0, 3, 2, 3, 4}, []int{0, 4}) // []int{3, 2, 3}
   693
   694
   695see also, typesafe implementations: SubtractString_
   696
   697.. SubtractString: https://godoc.org/github.com/thoas/go-funk#SubtractString
   698
   699funk.Sum
   700........
   701
   702Computes the sum of the values in an array.
   703
   704.. code-block:: go
   705
   706    funk.Sum([]int{0, 1, 2, 3, 4}) // 10.0
   707    funk.Sum([]interface{}{0.5, 1, 2, 3, 4}) // 10.5
   708
   709see also, typesafe implementations: SumInt_, SumInt64_, SumFloat32_, SumFloat64_
   710
   711.. _SumFloat32: https://godoc.org/github.com/thoas/go-funk#SumFloat32
   712.. _SumFloat64: https://godoc.org/github.com/thoas/go-funk#SumFloat64
   713.. _SumInt: https://godoc.org/github.com/thoas/go-funk#SumInt
   714.. _SumInt64: https://godoc.org/github.com/thoas/go-funk#SumInt64
   715
   716funk.Reverse
   717............
   718
   719Transforms an array such that the first element will become the last, the second element
   720will become the second to last, etc.
   721
   722.. code-block:: go
   723
   724    funk.Reverse([]int{0, 1, 2, 3, 4}) // []int{4, 3, 2, 1, 0}
   725
   726see also, typesafe implementations: ReverseInt_, ReverseInt64_, ReverseFloat32_, ReverseFloat64_, ReverseString_, ReverseStrings_
   727
   728.. _ReverseFloat32: https://godoc.org/github.com/thoas/go-funk#ReverseFloat32
   729.. _ReverseFloat64: https://godoc.org/github.com/thoas/go-funk#ReverseFloat64
   730.. _ReverseInt: https://godoc.org/github.com/thoas/go-funk#ReverseInt
   731.. _ReverseInt64: https://godoc.org/github.com/thoas/go-funk#ReverseInt64
   732.. _ReverseString: https://godoc.org/github.com/thoas/go-funk#ReverseString
   733.. _ReverseStrings: https://godoc.org/github.com/thoas/go-funk#ReverseStrings
   734
   735funk.SliceOf
   736............
   737
   738Returns a slice based on an element.
   739
   740.. code-block:: go
   741
   742    funk.SliceOf(f) // will return a []*Foo{f}
   743
   744funk.RandomInt
   745..............
   746
   747Generates a random int, based on a min and max values.
   748
   749.. code-block:: go
   750
   751    funk.RandomInt(0, 100) // will be between 0 and 100
   752
   753funk.RandomString
   754.................
   755
   756Generates a random string with a fixed length.
   757
   758.. code-block:: go
   759
   760    funk.RandomString(4) // will be a string of 4 random characters
   761
   762funk.Shard
   763..........
   764
   765Generates a sharded string with a fixed length and depth.
   766
   767.. code-block:: go
   768
   769    funk.Shard("e89d66bdfdd4dd26b682cc77e23a86eb", 1, 2, false) // []string{"e", "8", "e89d66bdfdd4dd26b682cc77e23a86eb"}
   770
   771    funk.Shard("e89d66bdfdd4dd26b682cc77e23a86eb", 2, 2, false) // []string{"e8", "9d", "e89d66bdfdd4dd26b682cc77e23a86eb"}
   772
   773    funk.Shard("e89d66bdfdd4dd26b682cc77e23a86eb", 2, 3, true) // []string{"e8", "9d", "66", "bdfdd4dd26b682cc77e23a86eb"}
   774
   775funk.Subset
   776.............
   777
   778Returns true if a collection is a subset of another 
   779
   780.. code-block:: go
   781
   782    funk.Subset([]int{1, 2, 4}, []int{1, 2, 3, 4, 5}) // true
   783    funk.Subset([]string{"foo", "bar"},[]string{"foo", "bar", "hello", "bar", "hi"}) //true
   784   
   785    
   786Performance
   787-----------
   788
   789``go-funk`` currently has an open issue about performance_, don't hesitate to participate in the discussion
   790to enhance the generic helpers implementations.
   791
   792Let's stop beating around the bush, a typesafe implementation in pure Go of ``funk.Contains``, let's say for example:
   793
   794.. code-block:: go
   795
   796    func ContainsInt(s []int, e int) bool {
   797        for _, a := range s {
   798            if a == e {
   799                return true
   800            }
   801        }
   802        return false
   803    }
   804
   805will always outperform an implementation based on reflect_ in terms of speed and allocs because of
   806how it's implemented in the language.
   807
   808If you want a similarity, gorm_ will always be slower than sqlx_ (which is very low level btw) and will use more allocs.
   809
   810You must not think generic helpers of ``go-funk`` as a replacement when you are dealing with performance in your codebase,
   811you should use typesafe implementations instead.
   812
   813Contributing
   814------------
   815
   816* Ping me on twitter `@thoas <https://twitter.com/thoas>`_ (DMs, mentions, whatever :))
   817* Fork the `project <https://github.com/thoas/go-funk>`_
   818* Fix `open issues <https://github.com/thoas/go-funk/issues>`_ or request new features
   819
   820Don't hesitate ;)
   821
   822Authors
   823-------
   824
   825* Florent Messa
   826* Gilles Fabio
   827* Alexey Pokhozhaev
   828* Alexandre Nicolaie
   829
   830.. _reflect: https://golang.org/pkg/reflect/
   831.. _lodash: https://lodash.com/
   832.. _performance: https://github.com/thoas/go-funk/issues/19
   833.. _gorm: https://github.com/jinzhu/gorm
   834.. _sqlx: https://github.com/jmoiron/sqlx
   835.. _godoc: https://godoc.org/github.com/thoas/go-funk

View as plain text