Tag Archives: julialang

#MonthOfJulia Day 7: Functional Programming

In computer science, functional programming is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It is a declarative programming paradigm, which means programming is done with expressions. In functional code, the output value of a function depends only on the arguments that are input to the function, so calling a function f twice with the same value for an argument x will produce the same result f(x) each time. Eliminating side effects, i.e. changes in state that do not depend on the function inputs, can make it much easier to understand and predict the behavior of a program, which is one of the key motivations for the development of functional programming.
Wikipedia: Functional Programming

Functional Programming is characterised by higher order functions which accept other functions as arguments. Typically a Functional Programming language has facilities for anonymous “lambda” functions and ways to apply map, reduce and filter operations. Julia ticks these boxes.

We’ve seen anonymous functions before, but here’s a quick reminder of the syntax:

julia> x -> x^2
(anonymous function)

Let’s start with map() which takes a function as its first argument followed by one or more collections. The function is then mapped onto each element of the collections. The first example below applies an anonymous function which squares its argument.

julia> map(x -> x^2, [1:5])
5-element Array{Int64,1}:
  1
  4
  9
 16
 25
julia> map(/, [16, 9, 4], [8, 3, 2])
3-element Array{Float64,1}:
 2.0
 3.0
 2.0

The analogues for this operation in Python and R are map() and mapply() or Map() respectively.

filter(), as its name would suggest, filters out elements from a collection for which a specific function evaluates to true. In the example below the function isprime() is applied to integers between 1 and 50 and only the prime numbers in that range are returned.

julia> filter(isprime, [1:50])
15-element Array{Int64,1}:
  2
  3
  5
  7
 11
 13
 17
 19
 23
 29
 31
 37
 41
 43
 47

The equivalent operation in Python and R is carried out using filter() and Filter() respectively.

The fold operation is implemented by reduce() which builds up its result by applying a bivariate function across a collection of objects and using the result of the previous operation as one of the arguments. Hmmmm. That’s a rather convoluted definition. Hopefully the link and examples below will illustrate. The related functions, foldl() and foldr(), are explicit about the order in which their arguments are associated.

julia> reduce(/, 1:4)
0.041666666666666664
julia> ((1 / 2) / 3) / 4
0.041666666666666664

The fold operation is applied with reduce() and Reduce() in Python and R respectively.

Finally there’s a shortcut to achieve both map and reduce together.

julia> mapreduce(x -> x^2, +, [1:5])
55
julia> (((1^2 + 2^2) + 3^2) + 4^2) + 5^2
55

A few extra bits and pieces about Functional Programming with Julia can be found on github.

The post #MonthOfJulia Day 7: Functional Programming appeared first on Exegetic Analytics.

#MonthOfJulia Day 6: Composite Types

I’ve had a look at the basic data types available in Julia as well as how these can be stashed in collections. What about customised-composite-DIY-build-your-own style types?

Composite types are declared with the type keyword. To illustrate we’ll declare a type for storing geographic locations, with attributes for latitude, longitude and altitude. The type immediately has two methods: a default constructor and a constructor specialised for arguments with data types corresponding to those of the type’s attributes. More information on constructors can be found in the documentation.

julia> type GeographicLocation
       	latitude::Float64
       	longitude::Float64
       	altitude::Float64
       end
julia> methods(GeographicLocation)
# 2 methods for generic function "GeographicLocation":
GeographicLocation(latitude::Float64,longitude::Float64,altitude::Float64)
GeographicLocation(latitude,longitude,altitude)

Creating instances of this new type is simply a matter of calling the constructor. The second instance below clones the type of the first instance. I don’t believe I’ve seen that being done with another language. (That’s not to say that it’s not possible elsewhere! I just haven’t seen it.)

julia> g1 = GeographicLocation(-30, 30, 15)
GeographicLocation(-30.0,30.0,15.0)
julia> typeof(g1)                              # Interrogate type
GeographicLocation (constructor with 3 methods)
julia> g2 = typeof(g1)(5, 25, 165)             # Create another object of the same type.
GeographicLocation(5.0,25.0,165.0)

We can list, access and modify instance attributes.

julia> names(g1)
3-element Array{Symbol,1}:
 :latitude 
 :longitude
 :altitude 
julia> g1.latitude
-30.0
julia> g1.longitude
30.0
julia> g1.latitude = -25                       # Attributes are mutable
-25.0

Additional “outer” constructors can provide alternative ways to instantiate the type.

julia> GeographicLocation(lat::Real, lon::Real) = GeographicLocation(lat, lon, 0)
GeographicLocation (constructor with 3 methods)
julia> g3 = GeographicLocation(-30, 30)
GeographicLocation(-30.0,30.0,0.0)

Of course, we can have collections of composite types. In fact, these composite types have essentially all of the rights and privileges of the built in types.

julia> locations = [g1, g2, g3]
3-element Array{GeographicLocation,1}:
 GeographicLocation(-25.0,30.0,15.0)
 GeographicLocation(5.0,25.0,165.0) 
 GeographicLocation(-30.0,30.0,0.0)

The GeographicLocation type declared above is a “concrete” type because it has attributes and can be instantiated. You cannot derive subtypes from a concrete type. You can, however, declare an abstract type which acts as a place holder in the type hierarchy. As opposed to concrete types, an abstract type cannot be instantiated but it can have subtypes.

julia> abstract Mammal
julia> type Cow <: Mammal
       end
julia> Mammal()                            # You can't instantiate an abstract type!
ERROR: type cannot be constructed
julia> Cow()
Cow()

The immutable keyword will create a type where the attributes cannot be modified after instantiation.

Additional ramblings and examples of composite types can be found on github. Also I’ve just received an advance copy of Learn Julia by Chris von Csefalvay which I’ll be reviewing over the next week or so.

Learn_Julia_meap

The post #MonthOfJulia Day 6: Composite Types appeared first on Exegetic Analytics.

#MonthOfJulia Day 5: Collections

Julia caters for various collection types including tuples and arrays, dictionaries, sets, dequeues, priority queues and heaps. There’s a lot of functionality distributed across these different structures, so we’ll only skim the surface and pick out a few interesting bits and pieces.

An Array is really the most important workhorse collection (IMHO). Julia can handle arrays of arbitrary dimension, but we’ll only have a look at the most commonly used, which are 1D and 2D.

julia> x = [-7, 1, 2, 3, 5]
5-element Array{Int64,1}:
 -7
  1
  2
  3
  5
julia> typeof(x)
Array{Int64,1}
julia> eltype(x)
Int64
julia> y = [3, "foo", 'a']                 # Elements can be of mixed type
3-element Array{Any,1}:
   3     
    "foo"
 'a'     
julia> typeof(y)                           # Type of the Array itself
Array{Any,1}
julia> eltype(y)                           # Type of the elements in the Array
Any

Type promotion is applied to an array with mixed content (like the second example above, which contains an integer, a string and a character), elevating the element type to a common ancestor, which in the example is Any.

The usual indexing operations apply, noting that in Julia indices are 1-based.

julia> x[1]                                # First element
-7
julia> getindex(x, [1, 3])                 # Alternative syntax
2-element Array{Int64,1}:
 -7
  2
julia> x[end]                              # Last element
5
julia> x[end-1]                            # Penultimate element
3
julia> x[2:4]                              # Slicing
3-element Array{Int64,1}:
 1
 2
 3
julia> x[2:4] = [1, 5, 9]                  # Slicing with assignment
3-element Array{Int64,1}:
 1
 5
 9

An Array can be treated like a stack or queue, where additional items can be popped from or pushed onto the “end” of the collection. Functions shift!() and unshift!() do analogous operations to the “front” of the collection.

julia> pop!(x)                             # Returns last element and remove it from array.
5
julia> push!(x, 12)                        # Append value to end of array.
5-element Array{Int64,1}:
 -7
  1
  5
  9
 12
julia> append!(x, 1:3)                     # Append one array to the end of another array.
8-element Array{Int64,1}:
 -7
  1
  5
  9
 12
  1
  2
  3

What about a 2D array (or matrix)? Not too many surprises here. With reference to the examples above we can see that a 1D array is effectively a column vector.

julia> M = [1 2 3; 4 5 6; 7 8 9]
3x3 Array{Int64,2}:
 1  2  3
 4  5  6
 7  8  9
julia> N = [1 2; 2 3; 3 4]
3x2 Array{Int64,2}:
 1  2
 2  3
 3  4
julia> M[2,2]                      # [row,column]
5
julia> M[1:end,1]
3-element Array{Int64,1}:
 1
 4
 7
julia> M[1,:]                      # : is the same as 1:end
1x3 Array{Int64,2}:
 1  2  3

Collections are copied by reference. A shallow copy can be created with copy(). If you want a truly distinct collection of objects you need to use deepcopy().

And now a taste of the other collection types, starting with the tuple.

julia> a, b, x, text = 1, 2, 3.5, "Hello"
(1,2,3.5,"Hello")
julia> a, b = b, a                         # I never get tired of this!
(1,2)

A dictionary is just a collection of key-value pairs.

ulia> stuff = {"number" => 43, 1 => "zap!", 2.5 => 'x'}
Dict{Any,Any} with 3 entries:
  "number" => 43
  2.5      => 'x'
  1        => "zap!"
julia> stuff["number"]
43

Sets are unordered collections which are not indexed and do not allow duplicates.

julia> S1 = Set([1, 2, 3, 4, 5])           # Set{Int64}
Set{Int64}({4,2,3,5,1})
julia> S2 = Set({3, 4, 5, 6, 7})           # Set{Any}
Set{Any}({7,4,3,5,6})
julia> union(S1, S2)
Set{Any}({7,4,2,3,5,6,1})
julia> intersect(S1, S2)
Set{Int64}({4,3,5})
julia> setdiff(S2, S1)
Set{Any}({7,6})

We’ll see more about collections when we look at Julia’s functional programming capabilities, which will be in the next but one installment. In the meantime you can find the full code for today’s flirtation with Julia on github.

The post #MonthOfJulia Day 5: Collections appeared first on Exegetic Analytics.