10 Julia Recipes You Can't Miss

The second solution seems to be more efficient. Isn’t it.

 @time b = foo(1:100) #first one
  0.000023 seconds (1 allocation: 896 bytes)
 @time b = foo(1:100) #second one
  0.000008 seconds (1 allocation: 896 bytes)

There should be no difference, or at most a negligible difference.

Recall that to compute times, either use @time and execute the function at least twice (the first run is always polluted by compilation time). But to do serious benchmarks, use the package BenchmarkTools and @btime. This computes multiple times the calculation to provide a more accurate benchmark.

2 Likes

Something I find extremely useful for reading files: eachline I/O and Network · The Julia Language

Also note that reverse needs to be qualified as Iterators.reverse in recent Julia versions.

Edit: Note however that reverse will not work with eachline, unlike Iterators.reverse.

1 Like

Updated in the article.

For h. - see also Why are there all these strange stumbling blocks in Julia? - #67 by Eben60

e.g.

function retMultipleValues()
    v1,v2 = "India","Iran"
    return (; v1,v2)
end

Also, you might want to link this thread: Seven Lines of Julia (examples sought)

1 Like

That doesn’t sound right. You might mean that Iterators.reverse knows how to reverse eachline iterators in recent Julia versions. reverse from Base has never known how to do that.

2 Likes

Yes, that’s right, my bad

1 Like

Now updated to include around 21 recipes. Please give feedback on items 11-21.