Skip to content

Instantly share code, notes, and snippets.

@bkamins
Last active September 4, 2018 22:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bkamins/65f23936aa79376676355a357b7268ea to your computer and use it in GitHub Desktop.
Save bkamins/65f23936aa79376676355a357b7268ea to your computer and use it in GitHub Desktop.
Support materials for Warszawskie Forum Julia meeting @ September 5, 2018

What is new in Julia 1.0

Bogumił Kamiński, September 5, 2018

Agenda

  1. Package management in projects
  2. Handling missing data
  3. Broadcasting
  4. Heterogeneous collections

Package management in projects

  1. Start Julia 1.0 REPL
  2. Type ; mkdir TmpDir
  3. Type cd("TmpDir")
  4. Type pwd()
  5. Type ] (prompt should change to blue (v1.0) pkg>)
  6. Type help to see the list of available package management commands
  7. Type generate ExampleProject
  8. Inspect contents of ExampleProject directory
  9. Type activate ExampleProject (prompt should change to blue (ExampleProject) pkg>))
  10. Type status
  11. Type add DataFrames (now DataFrames package will be available )
  12. Inspect contents of ExampleProject directory again
  13. Type status again
  14. Type add DataFrames#master and status
  15. Type free DataFrames and status
  16. Type pin DataFrames and status (now you ware ready for production)
  17. Type activate (leave project environment) and status
  18. Try running using StaticArrays (or any other package you do not have installed)
  19. Now activate your project and add StaticArrays, try loading it; it should work
  20. Exit Julia and run it again, using StaticArrays will fail again (unless you activate your project back)

Handling missing data

What grade did you get yesterday during the class?

  1. missing isa Missing: I got a grade, but I will not tell you what.
  2. nothing isa Nothing: I did not get any grade yesterday.
  3. #undef: the array holding the grades points at mutable objects and was not initialized.
  4. undef isa UndefInitializer: it is a concrete value used to create uninitialized arrays.

Useful functions for working with missing: ismissing, coalesce, skipmissing.

Broadcasting

Try running this Julia code:

function f()
    a, b, c, d = (rand(10^8) for i in 1:4)
    extrema(@. (a^2+b^2) * (c^2+d^2) - (a*c+b*d)^2 - (a*d-b*c)^2)
end

and this R code:

f <- function() {
    a <- runif(10^8)
    b <- runif(10^8)
    c <- runif(10^8)
    d <- runif(10^8)
    range((a^2+b^2) * (c^2+d^2) - (a*c+b*d)^2 - (a*d-b*c)^2)
}

Function inlining and constant propagation

Have the following definitions in Julia:

f(x) = x + 1
g(x) = f(x) + x
h(x) = g(x) * x
i() = h(1.0)

Now run: @code_warntype i() and @code_warntype h(1.0).

Heterogenous collections

Try running this code:

x = ones(2*10^6);
y = repeat(Real[1.0, 1], 10^6);
z = repeat(Union{Int, Float64}[1.0, 1], 10^6);

using BenchmarkTools

@btime sum($x)
@btime sum($y)
@btime sum($z)

Now run this code (typical in Agent Based Modeling):

struct A1 end
struct A2 end
const As = Union{A1, A2}

a = As[A1(), A2()]

f(a::A1) = 1
f(a::A2) = 2

function g(a)
    s = 0
    for v in a
        s += f(v)
    end
    return s
end

@code_warntype g(a)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment