Kotlin, please allow return from closures

Edit: After writing this, I was helped by Andrey on Twitter and now I know about the return@

The simple answer is:

sequence.flatMap {
if (it.containsKey("connnected") {
return@flatMap Observable.just(it.getBoolean("connected"))
return@flatMap Observable.empty()

Here is the original post.

Today’s kotlin frustration… trying to map a Bundle into types. E.g. if the bundle has the key “connected” get it out as a boolean. otherwise, skip the event.

so I do

sequence.flatMap {
if (it.containsKey("connnected") {
return Observable.just(it.getBoolean("connected"))
return Observable.empty()

Except, oh Kotlin, you back-stabbing-friend… closures don’t allow `return`

So I edit it…

sequence.flatMap {
if (it.containsKey("connnected") {

That’s probably obviously wrong to you now, but it took me a long time to see the problem.

And the fix.

sequence.flatMap {
if (it.containsKey("connnected") {
return Observable.just(it.getBoolean("connected"))
} else {
return Observable.empty()

I don’t see why Kotlin disallows the return — and it makes this kind of error much less likely. The fact that closures evaluate to the last expression is handy for doing tiny bits of work, but I find that reactive networks in RxJava are much easier to express with blocks — and if they get more than a few lines long, you sometimes really want the ability to do an early return without introducing the nested scope of an `if` statement.

How to create a git repo with the SQLite amalgamation source

Suppose you use GIT for your source control and would like to include SQLite as a third-party component via `git submodule` or `git subtree`. How does one go about getting access to the SQLite amalgamation as a GIT repo? When I tried this I found one on github.com, but it wasn’t being maintained any longer. It occurred to me that I didn’t need to depend on anyone else — I could do this myself. These steps use the bash command line shell. I’ve tried it on Mac OS, but should also work on Linux or using a bash shell on Windows. SQLite source is kept in a source control management tool called Fossil (http://fossil-scm.org). (It’s written by the authors of SQLite and uses SQLite internally, naturally.) The authors courteously tag each major of release in Fossil. So it’s simply a matter of cloning the SQLite source, checking out each tag, running the makefile to produce the amalgamation and checking in the amalgamated code into a new git repo.


  1. Install Fossil from http://www.fossil-scm.org/download.html
  2. Install dev tools necessary for building fossil’s amalgamation (make, etc..)
  3. Install git
  4. Clone the SQLite source repo using fossil:

    mkdir sqlite-fossil
    cd sqlite-fossil
    fossil clone http://www.sqlite.org/cgi/src sqlite.fossil
    fossil open sqlite.fossil
    cd ..
  5. Create a new git repo to hold the amalgamation:

    mkdir sqlite-amalgamation
    cd sqlite-amalgamation
    git init
  6. Optional: Create a README.md file with a copy of these instructions or a link to this page and commit it.

In the future you won’t need to clone from fossil fresh, nor create a new repo — you’ll still have them. To add new SQLite releases to your repo you will use `fossil update` and `git pull` to update the two repos.

Adding one release:

  1. Perform these steps, replacing $1 with the release name — OR just copy this into a file called build-sqlite.sh next to your two repos, make it executable using `chmod +x build-sqlite.sh` and invoke it with the name of the release.
    cd sqlite-fossil
    fossil update $1
    rm -rf ../sqlite_build
    mkdir ../sqlite-build
    cd ../sqlite-build/
    make sqlite3.c
    cd ../sqlite-amalgamation/
    mv ../sqlite-build/sqlite3.c .
    mv ../sqlite-fossil/src/shell.c .
    mv ../sqlite-build/sqlite3.h .
    mv ../sqlite-fossil/src/sqlite3ext.h .
    git commit -a -m $1
    cd ..
  2. For example:

    ./build-sqlite.sh version-

    Not sure what all the tags are? Use `fossil tags` to display all the tags. Search for the ones that start with 3. as the major releases. Warning: The releases do not sort in alphabetical order (e.g. 3.8.11 sorts before 3.8.9 but is a later release) so use care when picking which tags to build, otherwise your git repo could end up with the commits not in the correct order.

Monitor Progress of dd Command

If you ever use the “dd” command do duplicate a drive, you’re probably familiar with the unnerving wait that accompanies its use. The command generally takes on the order of *minutes* to complete, and gives no feedback.

If you read the MAN page, you’ll see you can monitor the progress by sending a SIGINFO signal.

Open another terminal window, and do

kill -SIGINFO <dd pid>

Where <dd pid> is the process id of your dd command. For example, when I was formatting the SD card for use in my Raspberry Pi via dd on the command line, I was able to monitor progress this way:

sudo dd if=~/Downloads/2013-02-09-wheezy-raspbian.img of=/dev/disk2 bs=2048k
193+0 records in
192+0 records out
402653184 bytes transferred in 314.132226 secs (1281795 bytes/sec)

UPDATE: OMG! I just realized that the moment in time when I sampled my software installation on my Raspberry Pi was 100π to 4 digits of precision. What a wonderful omen!

CSS tricks

I’m trying to learn more about CSS, so here are a few tricks that I’ve picked up….

From http://phark.typepad.com/phark/2003/08/accessible_imag.html

A picture of Orion, my D&D figurine

The above image is actually an H2 text element that has been restyled to hide the text and show an image. This technique is useful for skinning a web page — you can simply put in placeholder text in <div> element with an appropriate ID. Then the CSS converts that to an image.

A block (from a div element) with position attribute bottom set to negative 10.

Note that in the previous example, there seems to be no way to push a contained box to the bottom of its container.

A block (from a div element) with float left
A block (from a div element) with float left
A block (from a div element) with float left
A block (from a div element) with float left

Next : how do you center horizontally in a box: use margin:auto;

A block (from a div element) with margin: auto

Next : How do you center vertically? The containing block must have display: table-cell and vertical-align: middle.

A block (from a div element) with margin: auto

once again welcoming your comments. and what is lua.

I’ve upgraded my moveable type installation to version 3.2. I hope this solves the spam comment problem I was having with moveable type 3.0.1.
In the next few months, I’ll be introducing you to the programming language “lua”.
Lua can be downloaded from http://lua.org. Here is the direct link : http://luaforge.net/frs/download.php/1106/lua5_1b1_Darwin83.tar.gz for Mac OS X.
Lots of people comment how they “used to program” or would “like to program” and yet none of these people ever do. What a shame. Lua is a simple programming language, so why not get started with a little teaser to make you see how easy it is to play.

function shuffle()
local suits={"spades", "clubs", "diamonds", "hearts"}
local cards={"ace", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King"}
deck = {}
local index = 1
for suitIndex, suit in ipairs( suits ) do
for cardIndex, card in ipairs( cards ) do
deck[ index ] = { suit = suit, card = card, value = { cardIndex } }
if cardIndex == 1 then
-- in blackjack, the ace is worth either 1 or 11
table.insert( deck[ index ].value, 11 )
index = index + 1
for i = 1, 52 do
local randomIndex = math.random( 52 )
deck[ randomIndex], deck[ i ] = deck[ i ], deck[ randomIndex ]
curCard = 1
function draw()
print( string.format("%s of %s", deck[ curCard ].card, deck[ curCard ].suit))
curCard = curCard + 1

And here is example output:

< shuffle()
< draw()
4 of clubs
< draw()
6 of clubs

Ok. Who am I kidding. You guys don’t want to learn lua. But if you do, here are some good questions: what is a function and how do you call one? What does call a function mean? What does it mean when the word “local” appears before a variable name? Are the words “for”, “function”, “end”, “do” and “if” special in some way? What is math.random? What is the significance of the curly braces “{” and “}”? Brackets “[” and “]” and parenthesis “(” and “)”?
I await with baited breath to see if anyone really asks me any programming questions.

Backup this File, but only if it changes…

Have you ever wanted to try make a backup of a large file, or a large number of files, but didn’t want to waste the disk space and time it took to do it? For example, I recently ran iPhoto 5 for the first time, and it needed to upgrade my iPhoto library. But I still wanted to be able to run iPhoto 4 with it.
On Mac OS X, this is one task that is made easy by the UNIX underpinnings of that operating system.
You see, on a UNIX system, each file is actually simply a pointer to something called an INODE. It is possible that two files can share an inode. In that case, the files are identical. However, if one of the files is deleted or rewritten, then a new inode is created. The other file that shared the inode will be untouched.
I’ve leveraged this fact to make my “upgrade” to iPhoto 5 without modifying my original iPhoto library.
First, I renamed my iPhoto Library
mv "iPhoto Library" "old iPhoto Library"
Then I created a mirror directory structure
cd "old iPhoto Library"
find . -type d -exec mkdir -p ../iPhoto\ Library/{} ';'
Then I used the ln command to create new files pointing to the INODES of the existing files
find . -type f -exec ln {} ../iPhoto\ Library/{} ';'
This task will consume very little disk space, even if the images are really huge. If I ever want to revert to the old library, simply swap the names of the directories around.

AppleScript trick

If you have multiple copies of the same application on your hard disk, it can be a bit unpredictable which one will launch if you say
tell application "MyApp" to activate
in AppleScript.
Here is a code snippet I figured out for making sure it is the one in the /Applications directory that gets launched.
set mypath to ""
tell application "Finder" to set mypath to path to the "apps" from system domain as string
set mypath to mypath & "MyApp.app"
tell application mypath to activate