Erik Engheim

Oct 15, 2017

4 min read

Julia One Liners to Impress Your Friends

This was inspired by the blog post 10 Scala One Liners to Impress Your Friends.

1. Multiple Each Item in a List by 2

Original Scala version

I think the Julia version is clearer. The x->2x notation for anonymous function looks very similar to regular mathematical notation.

2. Sum a List of Numbers

Sums numbers from 1 to 1000 in Scala either by using reduceLeft or sum.

Very similar in Julia, but the reduce seems more elegant in that you don’t have to explicitly give the arguments to +. reduce takes a binary operator. So you just pass that. You don’t have to tell Julia how to add numbers with it.

3. Verify if Exists in a String

Check if a word in a word list is present in a string.

Here I really think the Scala version is rather cryptic. I cannot really guess how this works without learning more about Scala. In Julia:

4. Read in a File

This is a line for reading a file as a string in Scala. While that is apparently impressive by Java standards it is rather verbose for other languages.

In Julia this would just be

There is another Scala example of reading the file and turning every line into an element in list.

This is also the sort of thing that is trivial in Julia. Julia’s file open command can take a function as an argument and provide that function with a stream object and then close the file after the function is finished. There are many functions like readstring, readlines etc which takes streams as arguments. So you can write:

5. Happy Birthday to You!

Actually didn’t think Julia had foreach when I first made the example, so I am showing two versions now:

The Scala equivalent:

In Julia it is clearer to do it in two steps:

One thing to notice though is that you can write this stuff in Julia with a lot less clutter of parenthesis and curly braces. In Scala need 4 parenthesis for the if statement, while Julia avoids this and use a single end keyword instead.

6. Filter list of numbers

Partition a list of students into two categories.

There is no function like partition in Julia so you would have to write this using multiple lines:

But of course nothing prevents us from defining a partition function in Julia:

Which then would allow us to do like in Scala:

7. Fetch and Parse a XML web service

Here’s an example fetching the Twitter search feed in Scala.

I don’t know anything about XML stuff in Julia, so I will not attempt and alternative.

8. Find minimum (or maximum) in a List

Another couple of examples using reduceLeft to iterate through a list and apply a function. Added simpler examples of the method min/max on the list.

Again this is the sort of thing were Julia is very succinct.

9. Parallel Processing

The following one-liner would give you parallel processing over the list in Scala.

This is not an area I am very familiar with in Julia, you can read more about parallel execution in Julia here. But here is a rough equivalent in Julia:

Here is a concrete example of squaring all the numbers from 1 to 10:

10. Sieve of Eratosthenes

We got this crazy line from Scala, but no I am not going to figure out what this really does and rewrite it in Julia.

If somebody can explain to me how this works, I’ll attempt to do it in Julia.