Debug Logging In Swift

in

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.

Thanks to rob_rix and jl_hfl for the suggestion.

Fizz Buzz in Swift

in

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.

http://downloads.abizern.org/FizzBuzzery.zip

Have fun!

GCJ B - Cookie Clicker Alpha

in

Another problem from This year’s Google Code Jam.

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.

module Main where

import Control.Monad
import Numeric


-- https://code.google.com/codejam/contest/dashboard?c=2974486#s=p1
-- Input and output with standard redirection operators

factoryTimes :: Double -> Double -> [Double]
factoryTimes c f = 0.0 : [ c / (2.0 + k * f) | k <- [0.0, 1.0 ..]]

productionTimes :: Double -> Double -> [Double]
productionTimes x f = [ x / (2.0 + k * f) | k <- [0.0, 1.0 ..]]

times :: Double -> Double -> Double -> [Double]
times c f x = zipWith (+) production factory
  where production = productionTimes x f
        factory = scanl1 (+) $ factoryTimes c f

firstMinimum :: [Double] -> Double
firstMinimum (x:y:ys) = if x < y
                        then x
                        else firstMinimum (y:ys)

solve :: Double -> Double -> Double -> Double
solve c f x = firstMinimum $ times c f x

main :: IO ()
main = do
  t <- getLine
  forM_ [1..read t :: Int] $ \i -> do
    [c, f, x] <- fmap (map read . words) getLine
    let result = solve c f x
    putStrLn $ concat ["Case #", show i, ": ", Numeric.showFFloat (Just 7) result ""]
    

GCJ A - Magic Trick

in

I got through the qualification round for Google Code Jam 2014. I usually manage to get through this stage; it’s the first round that I haven’t managed to get past yet.

The easiest question was A-Magic Trick. Nothing much to do here but count the number of similarities between two arrays, easily achieved with the interact method.

The only gotcha in this problem was that the index of the rows to check is 1-based, and most programming languages use 0-based indexing.

module Main where

import Control.Monad
import Data.List

-- https://code.google.com/codejam/contest/dashboard?c=2974486#s=p0
-- Input and output with standard redirection operators

solve :: [Int] -> [Int] -> String
solve a b
  | l == 1 = show $ head common
  | l > 1  = "Bad magician!"
  | otherwise = "Volunteer cheated!"
  where common = a `intersect` b
        l = length common

main :: IO ()
main = do
  t <- getLine
  forM_ [1..read t :: Int] $ \i -> do
    choice1 <- fmap read getLine
    initial <- replicateM 4 $ fmap (map read . words) getLine
    choice2 <- fmap read getLine
    final <- replicateM 4 $ fmap (map read . words) getLine
    let r1 = initial !! (choice1 - 1)
        r2 = final !! (choice2 - 1)
    putStrLn $ concat ["Case #", show i, ": ", solve r1 r2]

Hakyll New Post With Emacs

in

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.

(defun hakyll-site-location ()
  "Return the location of the Hakyll files."
  "~/Sites/hblog/")

(defun hakyll-new-post (title tags)
  "Create a new Hakyll post for today with TITLE and TAGS."
  (interactive "sTitle: \nsTags: ")
  (let ((file-name (hakyll-post-title title)))
    (set-buffer (get-buffer-create file-name))
    (markdown-mode)
    (insert
     (format "---\ntitle: %s\ntags: %s\ndescription: \n---\n\n" title tags))
    (write-file
     (expand-file-name file-name (concat (hakyll-site-location) "posts")))
    (switch-to-buffer file-name)))

(defun hakyll-new-note (title)
  "Create a new Note with TITLE."
  (interactive "sTitle: ")
  (let ((file-name (hakyll-note-title title)))
    (set-buffer (get-buffer-create file-name))
    (markdown-mode)
    (insert (format "---\ntitle: %s\ndescription: \n---\n\n" title))
    (write-file
     (expand-file-name file-name (concat (hakyll-site-location) "notes")))
    (switch-to-buffer file-name)))

(defun hakyll-post-title (title)
  "Return a file name based on TITLE for the post."
  (concat
   (format-time-string "%Y-%m-%d")
   "-"
   (replace-regexp-in-string " " "-" (downcase title))
   ".markdown"))

(defun hakyll-note-title (title)
  "Return a file name based on TITLE for the note."
  (concat
   (replace-regexp-in-string " " "-" (downcase title))
   ".markdown"))

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).