Yesterday I had a look at Julia’s support for Functional Programming. Naturally it also has structures for conventional program flow like conditionals, iteration and exception handling.
Conditionals
Conditionals allow you to branch the course of execution on the basis of one or more logical outcomes.
n = 8;
if (n > 7) # The parentheses are optional.
println("high")
elseif n < 3
println("low")
else
println("medium")
end
high
The ternary conditional operator provides a compact syntax for a conditional returning one of two possible values.
if n > 3 0 else 1 end # Conditional.
0
n > 3 ? 0 : 1 # Ternary conditional.
0
I’m still a little gutted that R does not have a ternary operator. Kudos to Python for at least having something similar, even if the syntax is somewhat convoluted.
Iteration
Julia has various mechanisms for iteration. The simplest of these is the humble for
loop.
for n in [1:10]
println("number $n.")
end
number 1.
number 2.
number 3.
number 4.
number 5.
number 6.
number 7.
number 8.
number 9.
number 10.
In the code above we used the range operator, :
, to construct an iterable sequence of integers between 1 and 10. This might be a good place to take a moment to look at ranges, which might not work in quite the way you’d expect. To get the range to actually expand into an array you need to enclose it in []
, otherwise it remains a Range
object.
typeof(1:7)
UnitRange{Int64} (constructor with 1 method)
typeof([1:7])
Array{Int64,1}
1:7
1:7
[1:7]
7-element Array{Int64,1}:
1
2
3
4
5
6
7
A for
loop can iterate over any iterable object, including strings and dictionaries. Using enumerate()
in conjunction with a for loop gives a compact way to number items in a collection.
The while
construct gives a slightly different approach to iteration and is probably most useful when combined with continue
and break
statements which can be used to skip over iterations or prematurely exit from the loop.
Exceptions
The details of exception handling are well covered in the documentation, so I’ll just provide a few examples. Functions generate exceptions when something goes wrong.
factorial(-1)
ERROR: DomainError
in factorial_lookup at combinatorics.jl:26
in factorial at combinatorics.jl:35
super(DomainError)
Exception
All exceptions are derived from the Exception
base class.
An exception is explicitly launched via throw()
. To handle the exception in an elegant way you’ll want to enclose that dodgy bit of code in a try
block.
!(n) = n < 0 ? throw(DomainError()) : n < 2 ? 1 : n * !(n-1)
! (generic function with 7 methods)
!10
3628800
!0
1
!-1
ERROR: DomainError
in ! at none:1
try
!-1
catch
println("Well, that did't work!")
end
Well, that did't work!
Exceptional conditions can be flagged by the error()
function. Somewhat less aggressive are warn()
and info()
.
I’ve dug a little deeper into conditionals, loops and exceptions in the code on GitHub.