In the last iOSDevWeekly Dave Verwer listed Art Sabintsev’s PrintLnMagic - a small function that reproduces the common DLog pattern that many use which not only prints a value, but the filename, the function name and the line of the call. Which is really handy in debugging.

I use a version of Dlog myself, which has the added benefit of only outputting to the console when in Debug configurations, which PrintLnMagic does not do.

So, I wrote this

I’m not overriding printLn() because I’m not sure clobbering such a widely used system function is a good idea.

Of course Swift projects don’t work the same way as Objective-C projects, so it isn’t enough to just have the debug configuration, this needs to have the -D DEBUG flag set in the “Other Swift Flags” section under Debug.

Installation is simple enough, this is just a single bare function, so just download the file and add it to your project. Call loggingPrintln() just as you would println(), only passing a value for the first parameter; the defaults will take care of the rest.

Update Feb 5, 2015

Rather than just passing an object or a value, the function can now take an expression for the first parameter. That way, the expression is only evaluated if the function body runs. Laziness is a virtue.

In August 2014 I gave a talk at NSLondon about various approaches to solving Fizz Buzz in Swift. The video is available on Vimeo and proved to be reasonably popular and even gained me my first mention in iOS Dev Weekly.

Just to make this more complete here is a link to the slides and a Swift Playground that you can use to play around with the examples.

The gist of the problem is to work out whether buying additional capacity for cookie production would result is reaching the quota faster than not buying additional capacity.

Haskell is suited to this for a couple of reasons. Firstly, its’ easy to work with infinite lists. so I can create list of the cumulative times for creating factories and reaching the target. And to calculate the cumulative target I’m using the scanl1 function to turn the infinite list of factory times into an infinite list of partial sums. I’m using scanl1 because it starts at 0, which is important as one of the gotcha’s with this is that it may be faster to just generate cookies rather than buying a factory in the first instance.

Secondly, pattern matching. With the infinite list I’m stopping when the time to reach the target starts growing again. Pattern matching makes this very easy to do.

When this blog was created using Octopress, a new post could be created by simply running rake new_post in the Terminal. There is no such convenience in Hakyll, which is currently used as the generator. A small thing, but I wanted to fix it.

I could have created a similar Rake task, or shell script to do this, and there are even examples to be found on Hakyll’s site. But since I’m usually in Emacs when I want to write a new post, I thought it would be a good excuse to write a little lisp. And so, here is my first attempt.

I’m not much of a lisper, and it probably took me longer to write than the time it will save me, but that doesn’t matter. Firstly; it might be useful to somebody else, and so the cumulative time saved could be greater.

Secondly, my day job means I spend most of my coding time in Xcode, which can’t be customised as Emacs can be, and if I don’t spend time writing and learning lisp, I’ll have no chance of getting better at it.

So, I think it’s not bad as a first attempt, although it could obviously be refactored, and you can follow the history of it in my dotfiles repository if you want to see how it could be developed (or even, you know, help me out with it).