In as few lines as possible describe why you love julia

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?

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

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

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.

1 Like

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

4 Likes

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

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])
4 Likes

For someone coming from Matlab, something as simple as:

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

How pretty is that :wink:

10 Likes

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

2 Likes

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

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.

9 Likes

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.

1 Like

This one is brilliant

@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}
6 Likes

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)

5 Likes

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.

2 Likes

Sorry! I did not see what you mean! :slight_smile:

Wiki show other example which you could use in real life because it is almost accurate. :wink:

Experimental mathematics is neat idea!

1 Like

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"

12 Likes

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.

13 Likes
n=4
randcoord=[collect(i) for i in zip(rand(0:20,n),rand(0:20,n))]
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?

1 Like
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> rand(1:20, (n, 2))
4×2 Array{Int64,2}:
 12   8
  6  20
 19   3
  1   8
3 Likes

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.

1 Like

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

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> 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]

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

2 Likes