In the past few months I have been immersing myself into the world of iOS development. Whilst the Objective-C language takes a little getting used to, with its odd syntax and memory management rules, it is certainly a fun platform to develop for. I do miss a few C# language features, such as events, but for the most part I am a happy iOS developer.
... that is apart from Linq!
A few weeks back I was writing some examples for a future release of the ShinobiControls Grid. The example in question required a datasource of people grouped by the first letter of their surname. Despite the use of predicates and key valued sorting, the code looked like this:
That's a pretty unwieldy lump in my opinion. With Linq I am used to performing this sort of logic with a single query!
So, I decided to go ahead and implement a bunch of Linq methods for Objective-C, which results in a much cleaner implementation:
Not only is the above code more concise, which is nearly always a good thing (unless you are talking regex-concise!), it is also much more readable. The 'select, distinct, sort, select' query makes it much easier to determine what the resulting output of this code will be.
In this blog post I'll explain what Linq is (for those of you who have not heard of it before), and how I implemented Linq-to-Objective-C. If you just want to use the code, you can grab a copy from github.
An introduction to Linq
If you are a seasoned .NET developer, you can skip this section, however, if you have not heard of Linq before, then read on ...
Linq was added to the C# language back in 2007. The name 'linq' stands for Language Integrated Query, and as the name implies, its original aim was to more tightly integrate queries into the C# language. As an example, when querying database developers find themselves writing queries using SQL statements embedded within literal strings:
string query = "SELECT * FROM Person WHERE age < 10"
The problem with the above code is that the SQL statements are not checked by the compiler, and as a result, errors will only surface at runtime. This is not a problem that is peculiar to C# - you will find SQL statements embedded within strings in virtually every language.
With Linq queries are no longer literal strings, they are instead constructed using keywords that are part of the C# language itself:
This allows the compiler to check your query syntax, resulting in less error prone code.
C# provides a couple of different syntaxes for Linq, the query syntax, shown above - and the fluent syntax, where method invocations are chained together.
The two are functionally equivalent:
Whilst the application of Linq for querying database is obvious, Linq can be used to query practically anything. The .NET framework supplies Linq to SQL, XML and Objects - while the community has added Linq to CSV, JSON, DataSets, Facebook, and my own contribution Linq-to-VisualTree.
I've found myself using Linq in virtually every .NET application I have ever written - and I miss it!
Fortunately the ~25 different Linq extensions methods are actually quite easy to implement, as the C# guru Jon Skeet demonstrated in an epic series of blog posts. So, why not bring this API to Objective-C?
Linq to Objective-C
In this section I'll rattle through the implementation of a number of Linq methods, for the full set, head to github.
The Linq 'where' method filters an array of objects based on a predicate that returns true for any object that should be included in the output array. Using a block for the predicate, gives the following method signature:
Before diving straight into the implementation, I'll demonstrate its usage via a unit test. Given the following array of Person objects:
Person is a simple object with
age properties. The test I created for the where method is as follows:
Which simply tests that the simple where query successfully returns all
Person instances with an age of 25.
The implementation of where is really quite trivial:
We're off to a good start!
So, how does this compare to the existing Objective-C APIs? Through the use of predicates it is quite easy to perform the same 'people who are 25' query. You can use block syntax:
Or the more concise string-based predicates:
With the Objective-C support for predicates, this Linq-style where method might not be the most useful, but the others are more interesting.
The Linq 'select' method allows you to perform transformation, which is known as a projection. Each element is acted upon by a selector function, with the returned result being used to populate the output sequence. Here's the method signature:
Again, I'll demonstrate the using via a unit test. This example uses the select method to extract the
name property for each person objects in the source array:
Again, the implementation is really quite simple:
Linq doesn't actually have a sort method, instead it provides
ThenBy methods. Because I don't want to get into multi-property sorting, I'll substitute both of these for a single 'sort' function.
/** Sorts the elements of a sequence in ascending order.
@return An array whose elements are sorted in ascending order. */- (NSArray*) sort;
/** Sorts the elements of a sequence in ascending order by using a specified keySelector.
@param keySelector A selector that provides the 'key' which the array should by sorted by. @return An array whose elements are sorted in ascending order. */- (NSArray*) sort:(Selector)keySelector;
The first method simply sorts the array by invoking the compare method on each object. The second uses a selector to extract the key that is then used for the comparison.
The unit tests are as follows:
As you can see, one simply sorts an array of
NSNumber instances, whereas the other sorts an array of
People instances via the name property using the key selector.
The implementation is as follows:
The sort-with-keySelector method makes use of the
NSArray sort method that sorts via the result returned by a block. The 'natural' sort simply uses an identity selector in order to re-use the same implementation.
The Linq of-type method is use to filter a list of objects to remove all of those which are not of a specific type. Here's the method signature:
And here is the associated test:
Which filters out the
NSString instances from a mixed array.
The implementation is quite trivial, making use of the where method which was detailed above:
ofType method is quite useful for tasks such as finding all the subviews of a
UIView that are of a specific type.
The distinct method returns the distinct elements from an array, in other words, it removes duplicates based on an equality comparison. The signature is as follows:
The unit test is as follows:
The implementation uses an
NSMutableArray to construct an array of unique objects:
Note that the use of
containsObject on an
NSMutableArray is a potential performance issue for large arrays of data. A better method might make use of an
NSSet which uses the object's hash in order to more quickly determine whether an item is already contained within a set.
The Linq select-many method is projection where the selector returns an array of objects. The select-many function flattens the returned arrays into a single array. For a good visual explanation of select-many check out this blog post.
Here is the method signature:
The unit test takes an array of string, projecting each element to an array via the
A more realistic example might take an array of orders, projecting each order to the order-items that it contains, with the result being all of the order-items over all orders ... this could of course be followed by a 'distinct' query which would find all the distinct (i.e. unique) items ordered for a collection of orders.
The implementation involves a couple of nested for-in loops:
The next Linq method, aggregate, is an interesting one because it doesn't return an array, instead it applies an accumulator function to the elements of an array, returning a single result. The signature is as follows:
The unit test uses this method to convert an array of strings into a CSV:
Although you can use aggregate to do all sorts of things, such as find the maximum value for an array of integers.
The implementation is pretty simple, seeding the aggregate with the initial value and using the accumulator for subsequent items:
A brief note on deferred execution
In C# Linq methods do not act on arrays, instead they act upon the
IEnumerable interface. This allows them to query anything that can be enumerated, which not only includes arrays, but can also include infinite sources of data (such as a stream of random numbers). The implementation of Linq relies on the 'yield' keyword, which results in the C# compiler creating a state machine to implement the
The practical implication of all this very clever stuff is that Linq queries are not executed immediately. As an example, when you create a query as follows:
Nothing will actually happen until you start to enumerate the 'query' variable. In other words, you have to 'pull' data from the query.
This results in a feature which is often called 'deferred execution', where queries are executed on demand. This can yield tremendous performance benefits, allowing queries to terminate early.
Objective-C does not have 'yield' keyword, making it hard to implement Linq in such a way as to allow deferred execution. I did actually manage to create an implementation of a few query methods where each were presented as a state machine. Also, the Obj-C for-in iterator uses the
NSFastEnumeration, which I was able to implement giving deferred execution and early termination. However, I decided to stick with the much simpler
NSArray implementation here. If anyone is interested in the more complex version, give me a shout - I'd be happy to share.
Creating a Linq-style API for Objective-C has proven to be relatively simple (and fun!). I have provided implementations for some of the most common and useful Linq methods, however the .NET Linq APIs for querying collections have a great many more methods than the ones I have covered.
If you are interested, why not pop over to github, fork and add a few more? I'll probably add to this project myself over time.
Regards, Colin E.