Last active
September 7, 2016 14:29
-
-
Save gobwas/f6fcf0e3b08f2937668fd741ddaae0e4 to your computer and use it in GitHub Desktop.
Combination
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func loop(v []int, r func([]int)) { | |
var i, j, n uint | |
n = uint(len(v)) | |
a := make([]int, 0, len(v)) | |
for ; i < (1 << n); i++ { | |
a = a[:0] | |
for j = 0; j < n; j++ { | |
if i&(1<<j) != 0 { | |
a = append(a, v[j]) | |
} | |
} | |
r(a) | |
} | |
} | |
func recursion(v []int, rcv func([]int)) { | |
var rec func(a, b []int) | |
rec = func(a, b []int) { | |
if len(b) == 0 { | |
rcv(a) | |
} else { | |
rec(append(a, b[0]), b[1:]) | |
rec(a, b[1:]) | |
} | |
} | |
a := make([]int, 0, len(v)) | |
rec(a, v) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment