Skip to content

7ML7W Julia Day 3: Become One With Julia

Charlie Egan edited this page Aug 7, 2018 · 3 revisions

What day? What page? What PDF page?...

There will be no fans at this meeting - they have been 'removed'.

The chapter is pretty short.

The setup process can be quite long for all the tooling. Julia downloads some packages for you that are dependencies of the Julia things we're installing.

It's just quite surprising to see it go off and take an hour downloading it's own homebrew and everything else.

We remembered that factor did something similar - perhaps when running '+' in the repl it'd download a load of stuff in the background... we can't remember exactly.

Tom: if i'd made a programming language I'd try and make sure that it's easily possible to install and use a particular language.

James: how much of that is just homebrew though?

Seems the book does actually say which version the chapter uses...

What we're going to do

  • Macros in Julia
  • Building an image codec

One does not appear to be a pre-requisite for the other...

The Julia days appear to have sampled from the different features that Julia has, rather than having any particular thread throughout.

Murray: is what makes Julia interesting that it's got lots of stuff in it?

Elena: [paraphrasing] remember that it's meant to be an alternative to R. I find it easier to read. I'm sold.

Chris: [paraphrasing] in the docs there's more of an emphasis on the similarities with mathematical syntax.

Transforming Code, Instead of data

[repl] Investigation into what the Types of things like :(println("Hello!")) actually are. Can we annotate their types?

If you want, you can dynamically generate these expressions like so:

Expr(:call, +, 1, 2, myVariable)

Murray+James: Expressions are the first step towards building macros in Julia.

We can use interpolation if we want like this:

julia> quote

[Elena] I remember that there was an unquote in factor but not whether it was doing the same sort of thing...

julia> macro ...

Tom: macro is a special type of function that returns some code. When using it, we call the function, get the code back from it and run that code with the arguments we have.

Murray: it's an important feature that the code isn't eagerly evaluated.

Murray: Why do we invoke macros with the @ sign? It doesn't seem to be required, but is perhaps just a marker for a macro so we're aware of it.

[end macro section :wave:]

Elena: Aren't excel macros also preceded with a macro.

James: Excel macros aren't in the spreadsheet like functions. If a spreadsheet is source code then maybe they are macros.

Macros seems to just mean 'scripts' in a few other places.

Chris: do Julia programmers use and define macros a lot?

Murray: the concurrency stuff is all based on macros, and there are a load of them in the std lib. So I expect they use them a lot.

Murray: do lisp programmers even write lots of macros?

James: macros are probably most useful for expanding all the magic behind the scenes of julia's features.

Tom: And macros don't need to just be a source code string interpolation, it can have logic itself to change the behaviour.

[detour into expanding butts with the @parallel macro]

We see that the macro returns an error that "butts" can't be run in parallel.

Tom: [not paraphrasing] where does it evaluate butts?

We started playing with the @macroexpand macro to see what gets return from our @parallel "butts" call.

It seems to be doing some interesting numbering of the local variables - perhaps something about making them unique.

Calling the macro again it seems to increase the number prepended to variables.

Chris: can we macroexpand macroexpand?

Murray: will it ever end. Macroexpand seems to quote the result returned by the macro. Doing it again and again just inserts more quotes...

Moving on...

And now for something... completely different.

There's a bit of background on codec and compression.

We wonder if lossy compression can be generalised. It seems that throwing away the things we don't need is common but the means of choosing what to bin is dependent on the media.

James: Compressing a book might be removing all the vowels.

... and that's going to look very different from our JPEG code here.

Detour into the package manager

julia> Pkg.add("Images")
**INFO:** Initializing package repository /home/jack/.julia/v0.3
**INFO:** Cloning METADATA from git://

Murray: that ... implies a lot of output and a lot of time!

It creates its own homebrew installation and sometimes it breaks and it's a bit of a fiddle.

It create it's own private usr folder just with the packages julia needs. It also makes this available to the current Julia installation.

Tom: the downside of course being that you need to rebuild a whole load of stuff.

We expect there are some platform specific things going on here. What does it do on Linux? chroot? containers? We don't know, we may never know.

[James later on: seems that it has a custom tool to manage it's packages and downloads a bunch of stuff from git in the process.]

Back to image compression

Using the images library, what you get is an image thing:

julia> img = testimage("cameraman")
Gray Image with:

Now we get a large 2d array of pixels. It doesn't seem to have the Image type any more :( this makes following the book a little harder...

[detour of a few adjustments to see the camera man image, TODO: Murray?]

At the moment, we have a 2d array of Gray things. Gray is the color space of our image.

Just looking at the raw pixels it's hard to tell which pixels are the important ones. Luckily we can use Fourier transform to get the frequencies. If we trust that the result of the frequencies transform tells us the 'important' pixels, then we can see which values in the grid we'd like to keep in our image.

[we're missing some knowledge on what exactly is doing on in the transform here]

James: we should see this grid at the end as a just a list of color we might use, and given that one is much more common, we should use that one.

We subtracted the new array from the original pixels and found that it seems to have worked quite well.

julia> pixels1[1:8,1:8] - pixels2
[grid of very small differences that we've removed in pixels2]

[We see that it's removing the information but still using the same amount of space to store it...]

[I'm missing some notes as I struggle to understand this and eat some more bread

  • feel free to expand this section]

Murray: one of the exercises is to actually compress it.

James: we could use run length encoding to see this

[Tom: some of them are negative -0.0s, great]

some time later...

Murray shows the resulting image with clear JPEG-like artefacts. Then proceeds to clean the TV with his cursor alone.

Exercises & Wrapping Up Discussion

We could maybe do a session on implementing JPEG at some point. We're not sure which parts of it we missed out here in our Julia example.

Why Julia? It's fast and it's got lots of cool things. It's also got concurrency, 'real-macros' (AST transforming rather just strings).

Murray: Julia just feels like a more normal programming language than R which seems to have two ways of assigning to variables...

What are the downsides?

Murray: it seems to be doing a good job of saying this doesn't work any more - do this instead.

Tom: is it strange that it's not reached 1.0 yet? People seem to be using it.

Murray: [paraphrasing] they might not want the expectation that things will stabilize.


Elena: Julia 1.0 is IN STORES NOW [paraphrasing]

Julia con is happening as we speak!

James: [answering the homebrew question] Cairo within Julia is choosing what to install based on the current operating system. E.g. it'll use apt when it's available, or homebrew etc etc.

What next?

There are people, not naming any names, that would like a meeting on the remaining two languages.

We're not sure if these would be best following the book or having a DIY session.

We're also not sure if we want one or two meetings for the languages.

We can decide if we need another meeting at the first meeting.

We can likely do our usual thing but if someone did do some more leg work that'd be excellent.

Tom: Why is Benedict Cumberbatch in the bath?

Clone this wiki locally
You can’t perform that action at this time.