Let’s say you have a structure with two very similar properties and you need to choose one of the two. You don’t know which one at compile time, so you need to have a parameter to change that at runtime. This post shows this in a very simple and contrived example, given the data structures:
Learning various programming paradigms is very useful to extend your mind and also design approaches in your main language. For example, this post describes a simple example where infinite lists, as in Haskell, allow us to solve a problem in swift more elegantly.
The simple task
Let’s say we have some basic data structure in our domain model:
We have a list of Labels, which can be of any length, and we need to add a color to every Label, create a new ColoredLabel and send it to another system. We have a predefined set of colors, let’s say only three of them:
It all started with the need to reinstall my OSX. I have Time Machine backups, so I planned on making the final one before update, but there is no way to actually lock it. So a good idea is to make an image of your drive with another tool. I needed to clean the old TM backups first, so I ran:
# get the earliest ones$ tmutil listbackups | head -10
# remove them$ tmutil listbackups | xargs sudo tmutil delete
…and I missed the | head part there, which meant it’d remove all the backups. Since it’s very slow at removing them (not just rm -rf), I stopped the process at the first backup, so the rest was still there, but tmutil listbackups displayed an empty list from then on. I tried remounting the drive, running tmutil inheritbackup, tmutil associatedisk, disabling and reenabling the backup destination, selecting the backup directory by Option-clicking the TM icon — nothing worked to repopulate the list, only the new backups were visible. The official Time Machine UI displayed the same situation.
I’ve been using vim occasionally for over six years now, and more and more recently. Basically, any text editing in the terminal is in vim, so I got used to the convenience of using shortcuts to navigate and edit files quickly. And there are vim modes available in other apps as well. This short post is about those that I use (almost) every day:
There is one operation that’s completed very often when programs connect to servers on the internet — that’s resolving DNS names (finding out the server’s IP address by its name). Typically, you use a DNS server of your ISP. It’s relatively close to your network, so it may be fast. This short post is about setting up a local caching DNS server on OS X for even faster DNS responses.
An annoying thing that was introduced a few releases ago in the iOS Simulator is that your apps’ bundle and container directories change their names (a random UUID) every time you run them in Xcode. It makes it very annoying to test your app if you need to check the output files regularly or restore the app’s container to a known state before launching. I’ve found a relatively painless workaround though!
I occasionally conduct trainings on git and gerrit at my company. Using git is very easy, even for remote commands, there is GitHub, just cloning from another local directory, or even serving your own local repo on the network with git serve. Gerrit, on the other hand, requires a server somewhere to run it. This article describes the steps to setup a demo Gerrit server on an AWS EC2 instance (virtual private server) with HTTP authentication. NB: This guide is not for production usage.
Note: I run this server for a few days max on a month, so EC2 is cheaper than DigitalOcean. If you need to run it the whole month, the latter option is cheaper.
Note: you can do the same stuff (with fewer steps) to setup Gerrit on an Ubuntu virtual machine if you need to test it only locally.
Today’s post is a trivial script to pick a random screensaver every day on OS X. Yes, if you go the “Desktop & Screen Saver” preference pane, there is the last item called “Random”, but it’s very limited — a screensaver is picked randomly among all available every time when it’s requested. To show why that’s not what I need, let’s install a pack of great screensavers, which has been ported to OSX — XScreenSaver:
# run this command if you don't have `brew cask` installed yet$ brew tap caskroom/cask
$ brew cask install xscreensaver