Press "Enter" to skip to content

Custom Operators with Generics in Swift

This weekend I decided to sit back a play with Swift. About two weeks ago I took some time off from work and while relaxing I started watching Swift Talk. Swift Talk is a weekly video series by Chris Eidhof and Florian Kugler who during the show do a bit a pair programming and refactoring. Rather than go over a solution, they walk you through the thought process, live coding as they discuss the topic of the week. Even if you are not interested in Swift, as a coder I strongly suggest watching some of the free episodes to get a feel for it. Needless to say after the first few I found it very easy to subscribe and support these guys.

The past few episodes have been about parser combinators (Wikipedia, paper), and they’ve been fantastic. After consuming all available episodes I really wanted to get back to writing Swift for fun. Nothing too difficult, just do something to play with language features that I had yet to explore. So this weekend I decided to do just that. Spend some time in a Playground and play. Here are the results of my last coding session, the entire Playground can be found on GitHub.

As the title of the post hints at, the main thing I wanted to learn how to do was create my own operators, specifically with generics. I was already toying with the idea of creating a simple graph demo so it seemed reasonable to me to use that as vehicle to experiment with. Let me start by showing you what I wanted in the end with respect to the operator. I wanted a clear way to define the relationship between two nodes, specifically, using an arrow of some kind.

In this example we can see that a --> b means that “a” is connected to “b”, however “b” is not connected to “a”. Looking a bit lower we see that “c” is connected to “g”, but using the c <--> g operator, means “g” is also connected to “c”. Before we look at my implementation, go over to Swift Talk’s GitHub page to see their parser combinator. It’s super cool and you can see how generics with your own operators can be used to make some very powerful and elegant code. What I have is a much more simple example, but I do like the result.

Here is my Node class and the operators. Originally I though because I defined the Node as Node I would need to define the extension to the Node class as also being extension Node where E: Comparable, however that was not the case.

Though this is simple it does give me some building blocks. For instance, the next step might be to chain connections. I could say a --> b --> c <--> d as a means to define a sub graph. Here “a” connects to “b”, which connects to “c” and so on. Another convenience might be to define the subscript operation on the Graph so I can say graph["a"] --> graph["b"]. If “a” or “b” does not exist it will add them then do the connection, otherwise it will just connect them up.

With the graph constructed it was time to test it out. So, I wrote a simple Depth-First Search and a Breadth-First Search algorithms to see if the graph functioned properly. These test are included in the Playground. Really enjoyed just playing with code for the sake of experimenting. Don’t think I’m ready to make my own parser combinator just yet, but weekends like this are something I am hoping for more in the future.

Next up: Pointers and pointer arithmetic in Swift.

*Bonus : Also in the repository is BST using enums. First thing I wrote yesterday because I simply wanted the practice using enum for more that primitive data types. Good amount of fun.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *