Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mikolalysenko/foreach-combination
Visit all k-combinations of an array in lexicographic order
https://github.com/mikolalysenko/foreach-combination
Last synced: about 2 months ago
JSON representation
Visit all k-combinations of an array in lexicographic order
- Host: GitHub
- URL: https://github.com/mikolalysenko/foreach-combination
- Owner: mikolalysenko
- License: mit
- Created: 2014-05-14T18:10:27.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2014-05-14T18:14:19.000Z (over 10 years ago)
- Last Synced: 2024-10-20T14:26:33.313Z (2 months ago)
- Language: JavaScript
- Size: 121 KB
- Stars: 10
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
foreach-combination
===================
Enumerate all k length subsets in an array.# Example
```javascript
var kcomb = require("foreach-combination")var array = [1, 2, 3, "a", "b", "c"]
kcomb(array, 3, function(x,y,z) {
console.log(x, y, z)
})
```Output:
```javascript
1 2 3
1 2 'a'
1 2 'b'
1 2 'c'
1 3 'a'
1 3 'b'
1 3 'c'
1 'a' 'b'
1 'a' 'c'
1 'b' 'c'
2 3 'a'
2 3 'b'
2 3 'c'
2 'a' 'b'
2 'a' 'c'
2 'b' 'c'
3 'a' 'b'
3 'a' 'c'
3 'b' 'c'
'a' 'b' 'c'
```# Install
```
npm install foreach-combination
```If you want to use this in a browser, then you should use [browserify](http://browserify.org/).
# API
#### `require("foreach-combination")(array, k, visit(x1,x2,...,xk)`
Visits all `k` size combinations in array in lexicographic order.* `array` is an array
* `k` is the size of the combination to visit
* `visit(x1,x2,...xk)` is a callback that gets called once for each size k-subset of the array. If `visit` returns a non-undefined value then the iteration is immediately terminated, and the result of `visit` is returned.**Returns** The result of the last call to `visit`, or `undefined` if no value was ever returned.
**Note** If you know `k` in advance, you can avoid an extra dispatch by calling `require("foreach-combination")[k]` directly for any `k<32`
# Credits
(c) 2014 Mikola Lysenko. MIT License