# Can you make this shorter, more elegant, and more julia-esque

**URL:** <https://discourse.julialang.org/t/can-you-make-this-shorter-more-elegant-and-more-julia-esque/44848>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [August 13, 2020, 2:53am UTC](https://discourse.julialang.org/t/can-you-make-this-shorter-more-elegant-and-more-julia-esque/44848 "2020-08-13T02:53:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)\
**Post date:** [August 13, 2020, 2:53am UTC](https://discourse.julialang.org/t/can-you-make-this-shorter-more-elegant-and-more-julia-esque/44848/1 "2020-08-13T02:53:45Z")

</div>

Hi  
Can you make this code more elegant

```julia
flag1 = (0,1)
flag2 = (0,1)
flag3 = (0,1)
flag4 = (0,1)
flag5 = (0,1)

for i1 in flag1
    for i2 in flag2
        for i3 in flag3
            for i4 in flag4 
                for i5 in flag5
                    println("$i1 , $i2, $i3, $i4, $i5")
                end
            end
        end
    end
end

```

Back ground information, I was writing a case statement for a sql query  
that check 5 flags (each flag is boolean, yes or no, 1 or 0)  
And based on the combination of the 5 flags I decide the record final status  
I wanted to make sure that my SQL Case covered all cases  
Since I have 5 flags then its 2 to the power of 5 case, 32 cases  
I was about the type the 32 cases in excel and go through them one by one to see if I missed any  
I then decided to write those few line to help me (since I am learning Julia anyway on the side)

This obviously look, not so elegant, and that is only because I am nice to me 🙂  
But it works, I copied the output to excel and it helped me make sure I covered all the cases  
And provided nice docs for the query

---

<div class="post-metadata">

**Author:** ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)\
**Post date:** [August 13, 2020, 3:02am UTC](https://discourse.julialang.org/t/can-you-make-this-shorter-more-elegant-and-more-julia-esque/44848/2 "2020-08-13T03:02:39Z")

</div>

well, first you don’t need 5 `(0,1)`, they are the same thing, and then, you can put them in just one line:

```julia
for a∈(0,1),b∈(0,1),c∈(0,1)

```

* * *

if you are willing to use `Combinatorics.jl`

```julia
julia> Iterators.flatten(permutations.(with_replacement_combinations([0,1], 5)) .|> unique) |> collect
32-element Array{Array{Int64,1},1}:
 [0, 0, 0, 0, 0]
 [0, 0, 0, 0, 1]
 [0, 0, 0, 1, 0]
 [0, 0, 1, 0, 0]
 [0, 1, 0, 0, 0]
 [1, 0, 0, 0, 0]
 [0, 0, 0, 1, 1]
 [0, 0, 1, 0, 1]
 [0, 0, 1, 1, 0]
 [0, 1, 0, 0, 1]
 [0, 1, 0, 1, 0]
 [0, 1, 1, 0, 0]
 [1, 0, 0, 0, 1]
 ⋮
 [1, 0, 0, 1, 1]
 [1, 0, 1, 0, 1]
 [1, 0, 1, 1, 0]
 [1, 1, 0, 0, 1]
 [1, 1, 0, 1, 0]
 [1, 1, 1, 0, 0]
 [0, 1, 1, 1, 1]
 [1, 0, 1, 1, 1]
 [1, 1, 0, 1, 1]
 [1, 1, 1, 0, 1]
 [1, 1, 1, 1, 0]
 [1, 1, 1, 1, 1]

```

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 13, 2020, 3:20am UTC](https://discourse.julialang.org/t/can-you-make-this-shorter-more-elegant-and-more-julia-esque/44848/3 "2020-08-13T03:20:40Z")

</div>

### Input

```julia
foreach(println, Iterators.product(Iterators.repeated((0, 1), 5)...))

```

### Output

```julia-auto
(0, 0, 0, 0, 0)
(1, 0, 0, 0, 0)
(0, 1, 0, 0, 0)
(1, 1, 0, 0, 0)
(0, 0, 1, 0, 0)
(1, 0, 1, 0, 0)
(0, 1, 1, 0, 0)
(1, 1, 1, 0, 0)
(0, 0, 0, 1, 0)
(1, 0, 0, 1, 0)
(0, 1, 0, 1, 0)
(1, 1, 0, 1, 0)
(0, 0, 1, 1, 0)
(1, 0, 1, 1, 0)
(0, 1, 1, 1, 0)
(1, 1, 1, 1, 0)
(0, 0, 0, 0, 1)
(1, 0, 0, 0, 1)
(0, 1, 0, 0, 1)
(1, 1, 0, 0, 1)
(0, 0, 1, 0, 1)
(1, 0, 1, 0, 1)
(0, 1, 1, 0, 1)
(1, 1, 1, 0, 1)
(0, 0, 0, 1, 1)
(1, 0, 0, 1, 1)
(0, 1, 0, 1, 1)
(1, 1, 0, 1, 1)
(0, 0, 1, 1, 1)
(1, 0, 1, 1, 1)
(0, 1, 1, 1, 1)
(1, 1, 1, 1, 1)

```

---

<div class="post-metadata">

**Author:** ![systems](https://avatars.discourse-cdn.com/v4/letter/s/65b543/32.png) [@systems](https://discourse.julialang.org/u/systems)\
**Post date:** [August 13, 2020, 3:24am UTC](https://discourse.julialang.org/t/can-you-make-this-shorter-more-elegant-and-more-julia-esque/44848/4 "2020-08-13T03:24:02Z")

</div>

Cool, but will need to remove the braces ( and ) from each line  
From my code I do a

```julia
julia script.jl >> file1.csv

```

and open it in excel 🙂

---

<div class="post-metadata">

**Author:** ![CameronBieganek](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cameronbieganek/32/6915_2.png) [@CameronBieganek](https://discourse.julialang.org/u/CameronBieganek)\
**Post date:** [August 13, 2020, 3:29am UTC](https://discourse.julialang.org/t/can-you-make-this-shorter-more-elegant-and-more-julia-esque/44848/5 "2020-08-13T03:29:16Z")

</div>

Ok, here’s a small modification that writes the result as a CSV file:

```julia
using DelimitedFiles # This is a standard library module.

writedlm(
    "test.csv",
    Iterators.product(Iterators.repeated((0, 1), 5)...),
    ','
)

```
