-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcombination.go
47 lines (37 loc) · 1.42 KB
/
combination.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
package golist
import (
"github.com/emylincon/golist/core"
)
// Combinations returns Combinations of n number of elements for a given string array.
// adapted from https://github.com/mxschmitt/golang-combinations
// e.g if n=2 it will return only 2 combined elements :=
// furthermore NewList([]string{"a", "b", "c"}).Combinations(2, "") = ["ab", "ac", "bc"]
// For n < 1, it equals to All and returns all Combinations.
// for n > len(list); n = len(list)
func (arr *List) Combinations(n int, joiner string) (*List, error) {
switch list := arr.list.(type) {
case []string:
set := core.SetString(list)
list = core.CombinationsString(set, n, joiner)
return NewList(list), nil
default:
return nil, ErrTypeNotsupported
}
}
// CombinationsMax returns combinations of n number of elements for a given string array.
// adapted from https://github.com/mxschmitt/golang-combinations
// e.g if n=2 it will return combinations <= 2
// furthermore NewList([]string{"a", "b", "c"}).CombinationsMax(2, "") = ["a", "b", "c", "ab", "ac", "bc"]
// For n < 1, it equals to All and returns all combinations.
// for n > len(list); n = len(list)
func (arr *List) CombinationsMax(n int, joiner string) (*List, error) {
switch arr.list.(type) {
case []string:
list := arr.list.([]string)
set := core.SetString(list)
list = core.CombinationsStringMax(set, n, joiner)
return NewList(list), nil
default:
return nil, ErrTypeNotsupported
}
}