# Is there a way to create a comprehensive set of conditional combinations

**URL:** https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368
**Category:** General Usage
**Tags:** question
**Created:** [August 5, 2022, 8:00pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368 "2022-08-05T20:00:46Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)
#### Post date: [August 5, 2022, 8:00pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/1 "2022-08-05T20:00:46Z")

</div>

Hi I was just wondering if there was a way to generate a combinatorial collection subject to some condition. For example using the Combinatorics package If I can generate all combinations of some vector

```julia
collect(combinations(1:6,2))
15-element Vector{Vector{Int64}}:
 [1, 2]
 [1, 3]
 [1, 4]
 [1, 5]
 [1, 6]
 [2, 3]
 [2, 4]
 [2, 5]
 [2, 6]
 [3, 4]
 [3, 5]
 [3, 6]
 [4, 5]
 [4, 6]
 [5, 6]

```

However what if I only want the combinations where a certain condition is met, e.g. the second number minus the first is greater than one or the sum of the selected set is equal to some value. Is there a way to input these constraints as a parameter into combinations?

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [August 5, 2022, 8:03pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/2 "2022-08-05T20:03:23Z")

</div>

how about using something like

`filter(x -> (x[2] - x[1])>0 || sum(x) == value, collect(combinations(1:6,2)))`

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [August 5, 2022, 8:04pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/3 "2022-08-05T20:04:22Z")

</div>

You should drop the `collect` to avoid allocating memory to store the discarded combos. If you must, add the `collect` outside `filter`.

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [August 5, 2022, 8:05pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/4 "2022-08-05T20:05:24Z")

</div>

> [@jbytecode](#):
>
> filter(x → (x[2] - x[1])\>0 || sum(x) == value, collect(combinations(1:6,2)))

oh sorry, just copy-pasted!

note: filter() does not work without using collect() in this case without using `collect`.

---

<div class="post-metadata">

### Author: ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)
#### Post date: [August 5, 2022, 8:11pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/5 "2022-08-05T20:11:14Z")

</div>

It appears that you need `Iterators.filter` if the second argument is an iterator.

`Iterators.filter(x → (x[2] - x[1])>0 || sum(x) == value, combinations(1:6,2))`

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [August 5, 2022, 8:17pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/6 "2022-08-05T20:17:47Z")

</div>

yes this is a more memory friendly version just because it consumes memory when needed only.

---

<div class="post-metadata">

### Author: ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)
#### Post date: [August 5, 2022, 8:50pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/7 "2022-08-05T20:50:48Z")

</div>

> [@Jeff\_Emanuel](#):
>
> Iterators.filter(x → (x[2] - x[1])\>0 || sum(x) == value, combinations(1:6,2))

thanks guys! this is really helpful. When I try to run it with Iterators.filter without collect rather than the actual combinations I get this. `Iterators.filter(x -> (x[2] - x[1])>0 ||sum(x) == 6 ,combinations(1:6,2)) Base.Iterators.Filter{var"#15#16", Base.Generator{Combinatorics.Combinations, Combinatorics.var"#10#13"{Combinatorics.var"#reorder#11"{UnitRange{Int64}}}}}(var"#15#16"(), Base.Generator{Combinatorics.Combinations, Combinatorics.var"#10#13"{Combinatorics.var"#reorder#11"{UnitRange{Int64}}}}(Combinatorics.var"#10#13"{Combinatorics.var"#reorder#11"{UnitRange{Int64}}}(Combinatorics.var"#reorder#11"{UnitRange{Int64}}(1:6)), Combinatorics.Combinations(6, 2)))`

Is this normal?

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [August 5, 2022, 8:54pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/8 "2022-08-05T20:54:50Z")

</div>

> [@Jeff\_Emanuel](#):
>
> Iterators.filter(x → (x[2] - x[1])\>0 || sum(x) == value, combinations(1:6,2))

how about

`Iterators.filter(x -> (x[2] - x[1])>0 || sum(x) == value, combinations(1:6,2)) |> collect`

---

<div class="post-metadata">

### Author: ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)
#### Post date: [August 5, 2022, 9:05pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/9 "2022-08-05T21:05:18Z")

</div>

awesome thanks! quick question what is the piping` |>` operator doing and how does that differ from your initial code ?

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [August 5, 2022, 9:09pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/10 "2022-08-05T21:09:05Z")

</div>

Yes it only collects the result returned by the filtered one.

---

<div class="post-metadata">

### Author: ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)
#### Post date: [August 5, 2022, 9:10pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/11 "2022-08-05T21:10:09Z")

</div>

gotcha thanks again!

---

<div class="post-metadata">

### Author: ![jbytecode](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jbytecode/32/17719_2.png) [@jbytecode](https://discourse.julialang.org/u/jbytecode)
#### Post date: [August 5, 2022, 9:11pm UTC](https://discourse.julialang.org/t/is-there-a-way-to-create-a-comprehensive-set-of-conditional-combinations/85368/12 "2022-08-05T21:11:09Z")

</div>

Your welcome!
