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

(1 to 10) map { _ * 2 }

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

map(x->2x, 1:10)

2. Sum a List of Numbers

(1 to 1000).reduceLeft( _ + _ )
(1 to 1000).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.

reduce(+, 1:1000)
sum(1:1000)

3. Verify if Exists in a String

val wordList = List("scala", "akka", "play framework", "sbt", "typesafe")
val tweet = "This is an example tweet talking about scala and sbt."
(wordList.foldLeft(false)( _ || tweet.contains(_) ))
wordList.exists(tweet.contains)

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

wordlist = ["scala", "akka", "play framework", "sbt", "typesafe"]
tweet = "This is an example tweet talking about scala and sbt."
any(word->contains(tweet, word), wordlist)

4. Read in a File

val fileText = io.Source.fromFile("data.txt").mkString

In Julia this would just be

filetext = readstring(“data.txt”)`

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

val fileLines = io.Source.fromFile("data.txt").getLines.toList

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:

filelines = open(readlines, "data.txt")

5. Happy Birthday to You!

(1 to 4).map { i => "Happy Birthday " + (if (i == 3) "dear NAME" else "to You") }.foreach { println }

The Scala equivalent:

foreach(println, map(i->"Happy Birthday " * (i == 3 ? "dear NAME" : "to You"), 1:4))

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

lines = map(1:4) do i 
"Happy Birthday " *
if i == 3 "dear NAME" else "to You" end
end
print(join(lines, "\n"))

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

val (passed, failed) = List(49, 58, 76, 82, 88, 90) partition ( _ > 60 )

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

grades = [49, 58, 76, 82, 88, 90]
passed = filter(grade->grade > 60, grades)
failed = filter(grade->grade <= 60, grades)

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

function partition{T}(p, xs::Vector{T})
as = T[]
bs = T[]
for x in xs
if p(x)
push!(as, x)
else
push!(bs, x)
end
end
(as, bs)
end

Which then would allow us to do like in Scala:

passed, failed = partition(grade->grade > 60, grades)

7. Fetch and Parse a XML web service

val results = XML.load("http://search.twitter.com/search.atom?&q=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

List(14, 35, -7, 46, 98).reduceLeft ( _ min _ )
List(14, 35, -7, 46, 98).min
List(14, 35, -7, 46, 98).reduceLeft ( _ max _ )
List(14, 35, -7, 46, 98).max

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

reduce(min, [14, 35, -7, 46, 98])
min(14, 35, -7, 46, 98)
reduce(max, [14, 35, -7, 46, 98])
max(14, 35, -7, 46, 98)

9. Parallel Processing

val result = dataList.par.map( line => processItem(line) )

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:

result = pmap(process_item, data_list)

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

pmap(x->x^2, 1:10)

10. Sieve of Eratosthenes

(n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) => if (ps(x)) ps -- (x * x to n by x) else ps))

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

Geek dad, living in Oslo, Norway with passion for UX, Julia programming, science, teaching, reading and writing.