# In as few lines as possible describe why you love julia

**URL:** <https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179>\
**Category:** New to Julia\
**Created:** [January 10, 2020, 2:07am UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179 "2020-01-10T02:07:29Z")\
**Posts on this page:** 20\
**Page:** 4

<div class="post-metadata">

**Author:** ![Crown421](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/crown421/32/9984_2.png) [@Crown421](https://discourse.julialang.org/u/Crown421)\
**Post date:** [January 18, 2020, 4:48pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/63 "2020-01-18T16:48:09Z")

</div>

Thank you for your feedback! I just ran the code and found no measurable change?  
Your function being `getnbs2`, minus two `)` at the end of the first row, was there something there?

```julia
display(@benchmark getnbs(M, testIdx))
display(@benchmark getnbs2(M, testIdx))
display(@benchmark getneighbours(M, testIdx))

BenchmarkTools.Trial: 
  memory estimate: 400 bytes
  allocs estimate: 4
  --------------
  minimum time: 184.539 ns (0.00% GC)
  median time: 216.637 ns (0.00% GC)
  mean time: 321.486 ns (17.86% GC)
  maximum time: 25.639 μs (98.64% GC)
  --------------
  samples: 10000
  evals/sample: 651

BenchmarkTools.Trial: 
  memory estimate: 400 bytes
  allocs estimate: 4
  --------------
  minimum time: 177.356 ns (0.00% GC)
  median time: 214.571 ns (0.00% GC)
  mean time: 322.807 ns (18.19% GC)
  maximum time: 27.517 μs (98.18% GC)
  --------------
  samples: 10000
  evals/sample: 666

BenchmarkTools.Trial: 
  memory estimate: 416 bytes
  allocs estimate: 8
  --------------
  minimum time: 212.362 ns (0.00% GC)
  median time: 247.916 ns (0.00% GC)
  mean time: 351.569 ns (24.86% GC)
  maximum time: 42.359 μs (99.25% GC)
  --------------
  samples: 10000
  evals/sample: 525

```

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [January 18, 2020, 5:30pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/64 "2020-01-18T17:30:32Z")

</div>

I very much doubt that the decision about an abortion is an appropriate subject to demonstrate the features of a programming language.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [January 18, 2020, 5:36pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/65 "2020-01-18T17:36:42Z")

</div>

```julia
pv = [1, 2, 3, 4, 5]; unique([shuffle(pv) for i in 1:1000])

```

Brute force solution to get all permutations of a tuple in one line of code. Not much background knowledge in combinatorics is required.

---

<div class="post-metadata">

**Author:** ![Elrod](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/elrod/32/22461_2.png) [@Elrod](https://discourse.julialang.org/u/Elrod)\
**Post date:** [January 18, 2020, 5:38pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/66 "2020-01-18T17:38:31Z")

</div>

You’re right. I made a mistake when benchmarking.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [January 19, 2020, 7:23pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/67 "2020-01-19T19:23:42Z")

</div>

> [@zweiglimmergneis](#):
>
> ```julia
> pv = [1, 2, 3, 4, 5]; unique([shuffle(pv) for i in 1:1000])
> 
> ```
> 
> Brute force solution to get all permutations of a tuple in one line of code. Not much background knowledge in combinatorics is required.

Are you sure that your one line of code return **all** permutations?

```julia
julia> pv = [1, 2, 3, 4, 5]; Set(length(unique([shuffle(pv) for i in 1:1000])) for j in 1:1000)
Set([120, 118, 119])

```

---

<div class="post-metadata">

**Author:** ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)\
**Post date:** [January 20, 2020, 6:32am UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/68 "2020-01-20T06:32:00Z")

</div>

For someone coming from Matlab, something as simple as:

```julia
f(x, y=2) = 3x + y

```

How pretty is that 😉

---

<div class="post-metadata">

**Author:** ![JeffreySarnoff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeffreysarnoff/32/1980_2.png) [@JeffreySarnoff](https://discourse.julialang.org/u/JeffreySarnoff)\
**Post date:** [January 20, 2020, 7:46am UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/69 "2020-01-20T07:46:30Z")

</div>

Julia provides a shorter path from conceptual clarity to clean code.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 20, 2020, 7:55am UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/70 "2020-01-20T07:55:59Z")

</div>

> [@zweiglimmergneis](#):
>
> ```julia
> pv = [1, 2, 3, 4, 5]; unique([shuffle(pv) for i in 1:1000])
> 
> ```

I wouldn’t do this. It is _not_ guaranteed to return all permutations, _and_ it’s slow. Use `Combinatorics.permutations` instead:

```julia
julia> using Combinatorics: permutations

julia> collect(permutations(pv))
120-element Array{Array{Int64,1},1}:
 [1, 2, 3, 4, 5]
 [1, 2, 3, 5, 4]
...

```

It’s correct, and _much_ faster.

And actually, you don’t really need to `collect` them in most cases, just iterate over `permutations(pv)` when you need them.

---

<div class="post-metadata">

**Author:** ![zweiglimmergneis](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zweiglimmergneis/32/7892_2.png) [@zweiglimmergneis](https://discourse.julialang.org/u/zweiglimmergneis)\
**Post date:** [January 20, 2020, 8:00pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/71 "2020-01-20T20:00:20Z")

</div>

Obviously I misunderstood the purpose of this discussion. I thought it was also about not so serious things you can do in Julia. Well, what I like about Julia is that it is up to the programmer, to decide, whether he needs to be smart or not. Anyway, thank you for `Combinatorics.permutations`.

---

<div class="post-metadata">

**Author:** ![Skoffer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/skoffer/32/378_2.png) [@Skoffer](https://discourse.julialang.org/u/Skoffer)\
**Post date:** [January 20, 2020, 8:42pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/72 "2020-01-20T20:42:24Z")

</div>

This one is brilliant

```julia
@code_typed reverse((1, 2, 3, 4, 5))

CodeInfo(
1 ─ %1 = (getfield)(t, 1)::Int64
│ %2 = (getfield)(t, 2)::Int64
│ %3 = (getfield)(t, 3)::Int64
│ %4 = (getfield)(t, 4)::Int64
│ %5 = (getfield)(t, 5)::Int64
│ %6 = Core.tuple(%5, %4, %3, %2, %1)::NTuple{5,Int64}
└── return %6
) => NTuple{5,Int64}

```

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [January 20, 2020, 8:49pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/73 "2020-01-20T20:49:14Z")

</div>

I like that one can [“patch into any little square of the space of dispatch possibilities”] ([JuliaCon 2019 | The Unreasonable Effectiveness of Multiple Dispatch | Stefan Karpinski - YouTube](https://youtu.be/kc9HwsxE1OY?t=755))

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [January 20, 2020, 9:33pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/74 "2020-01-20T21:33:57Z")

</div>

> [@zweiglimmergneis](#):
>
> Obviously I misunderstood the purpose of this discussion

I don’t think you misunderstood. But in spite of the levity of the subject, I think it should be OK to point it out if a piece of code can actually give the wrong answer. Especially if there is a correct way that is even more elegant.

---

<div class="post-metadata">

**Author:** ![Liso](https://avatars.discourse-cdn.com/v4/letter/l/898d66/32.png) [@Liso](https://discourse.julialang.org/u/Liso)\
**Post date:** [January 21, 2020, 3:55am UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/75 "2020-01-21T03:55:59Z")

</div>

Sorry! I did not see what you mean! 🙂

Wiki show other [example](https://en.wikipedia.org/wiki/Experimental_mathematics#Plausible_but_false_examples) which you could use in **real life** because it is almost accurate. 😉

Experimental mathematics is neat idea!

---

<div class="post-metadata">

**Author:** ![kirtsar](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kirtsar/32/7222_2.png) [@kirtsar](https://discourse.julialang.org/u/kirtsar)\
**Post date:** [January 21, 2020, 7:05am UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/76 "2020-01-21T07:05:11Z")

</div>

As the time passed I’ve come to realize that multiple dispatch " **is** a wonderful **gift** which **we neither understand** , **nor deserve**". Just imagine if there was a unique “plus” sign for each addition operation in mathematics: one for interegs, other for real numbers (different entities – so use different operations!), one for monomials, one for polynomials etc etc. As I think, the great progress in our understanding of the world is that there is very concise language to represent the fenomena – and Julia with its multiple dispatch is right in the middle of this " _ **Das Glasperlenspiel** _"

---

<div class="post-metadata">

**Author:** ![bashonubuntu](https://avatars.discourse-cdn.com/v4/letter/b/f19dbf/32.png) [@bashonubuntu](https://discourse.julialang.org/u/bashonubuntu)\
**Post date:** [January 27, 2020, 5:24pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/77 "2020-01-27T17:24:28Z")

</div>

> [@xiaodai](#):
>
> Would u say the same about python? Why and why not? Just curious

Learning Python is easy too but I was never ever confident of what I was learning. Am I learning tricks or principles? With Julia, I consistently get the sense that there are some basic principles that apply for any code you write. Ultimately, I think it is not so much about the learning curve either: what you are learning matters a lot more. Are you learning to work around things or are you learning a tool to achieve more? Good programming languages should make things easy for not so good programmers. This is where Julia shines.

---

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [January 27, 2020, 6:29pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/78 "2020-01-27T18:29:46Z")

</div>

```julia
n=4
randcoord=[collect(i) for i in zip(rand(0:20,n),rand(0:20,n))]

```

```julia
randcoord is a Vector{Array{Int64,1}} with 4 elements
Vector{Int64} with 2 elements
9
9
Vector{Int64} with 2 elements
8
5
Vector{Int64} with 2 elements
8
17
Vector{Int64} with 2 elements
10
5

```

It is cool!!  
Is there a better way?

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [January 27, 2020, 7:11pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/79 "2020-01-27T19:11:35Z")

</div>

> [@rapasite](#):
>
> Is there a better way?

```julia
julia> using StatsBase

julia> n = 4
4

julia> sample(0:20, (n, 2))
4×2 Array{Int64,2}:
 11 18
 12 20
 18 12
  7 2

```

Edit:

It occurs to me, regular old `rand()` actually works too:

```julia
julia> rand(1:20, (n, 2))
4×2 Array{Int64,2}:
 12 8
  6 20
 19 3
  1 8

```

---

<div class="post-metadata">

**Author:** ![rapasite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rapasite/32/3719_2.png) [@rapasite](https://discourse.julialang.org/u/rapasite)\
**Post date:** [January 27, 2020, 7:56pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/80 "2020-01-27T19:56:05Z")

</div>

Ok thanks you!  
this is better , sorry for my poor example then ;).  
note: you get the matrix lines by using `M[1,:]` , `M[2,:]` …( where M is the matrix)  
the format is still slightly different but still usable.

---

<div class="post-metadata">

**Author:** ![kevbonham](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kevbonham/32/216165_2.png) [@kevbonham](https://discourse.julialang.org/u/kevbonham)\
**Post date:** [January 27, 2020, 8:07pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/81 "2020-01-27T20:07:50Z")

</div>

True - if you want the same format of the output, you could do instead:

```julia
julia> n = 4; [rand(0:20, 2) for _ in 1:n]
4-element Array{Array{Int64,1},1}:
 [1, 6]
 [19, 6]
 [14, 2]
 [10, 16]

```

or

```julia
julia> collect(eachrow(rand(0:20, n, 2)))
4-element Array{SubArray{Int64,1,Array{Int64,2},Tuple{Int64,Base.Slice{Base.OneTo{Int64}}},true},1}:
 [3, 2]
 [19, 19]
 [10, 19]
 [11, 12]

```

---

<div class="post-metadata">

**Author:** ![anon92994695](https://avatars.discourse-cdn.com/v4/letter/a/ce7236/32.png) [@anon92994695](https://discourse.julialang.org/u/anon92994695)\
**Post date:** [January 27, 2020, 11:54pm UTC](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179/82 "2020-01-27T23:54:43Z")

</div>

There are no poor examples here! We’re talking about things we love, its all preference.

[Previous page](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179.md?page=3)

[Next page](https://discourse.julialang.org/t/in-as-few-lines-as-possible-describe-why-you-love-julia/33179.md?page=5)
