Category Archives: Uncategorized

Progress deploying Clojure to Google App Engine

I have been struggling for the last couple of days trying to deploy a test Clojure application to Google App Engine, with helpful prods from @perezd. I am using appengine-magic. The HEAD is broken, and the stable 0.5.0 branch has old dependencies. I fixed up some of the breakage in appengine-magic HEAD, coming up with the same solution as this unpulled pull request. Still, that didn’t quite do it. lein appengine-prepare was building a jar which lacked the needed Clojure language dependencies, so when uploaded to App Engine, the app page would just give a 500 error. lein uberjar would build a jar file including those dependencies, so I used that and followed the key steps in appengine_prepare.clj to put the jar in the right place.

Here’s how I finally got it to work:

Add the following to the project.clj file:

:aot [foobar.core, foobar.app_servlet]

Build, prepare and deploy with:

lein compile
lein uberjar
mv target/foobar-0.1.0-SNAPSHOT-standalone.jar war/WEB-INF/lib/
appcfg.sh --enable_jar_splitting --oauth2 update war

Yay.

No Lovejoy

I got up at 6am today to try to catch Comet Lovejoy, which should be visible in binoculars around 30 degrees above the horizon at that time. Unfortunately, seeing was quite bad despite a cloudless sky. I could not even reliably make out ΞΆ Herculis, which has magnitude +2.81, compared to Lovejoy’s current +5 or so.

So, I will have to wait until I am at a darker location. I found a usefulchart, showing Lovejoy’s position throughout December.

Position of Comet Lovejoy 2013 throughout December.
Position of Comet Lovejoy 2013 throughout December.

On Clojure

The longer I work as a software engineer, the more I am convinced that we are doing it completely wrong.

Programming. You're doing it completely wrong.

Really, programming is a lot of typing.

There is a lot of boiler-plate. For example, when you write a for loop, a lot of what you type is for the compiler’s benefit. It has little to do with the expression of an algorithm. Similarly for other constructs.

Related program logic is often broken up and placed in different methods and classes. This is a kind of coupling, which is bad.

Program comprehension and refactoring have limited tool support.

I have wandered through the programming language landscape, as I said above, only increasing my dissatisfaction with mainstream languages. I pondered Scala recently, but the syntax felt a little bulky.

Finally, I come to Clojure! The syntax is minimal – close to lambda calculus, with the additional benefit of rich collection types, variable arity functions, amongst other benefits. Leiningen seems to be a very nice package manager.

I don’t know yet about refactoring tools for Clojure. A little search just now found the following page for visualizing the Clojure core.

Back on the horse!

I had to take down this blog a while ago, because it had been hacked. I removed suspicious php files, changed all the passwords, but took the whole thing offline to be on the safe side.

What has been happening?

Well… I started working for Google as a Software Engineer on the iOS Google+ app.

And I have become very interested in Clojure and in machine learning (and the combination of the two). I just rediscovered Incanter, an R-alike data analysis platform for Clojure.

That is all for now!

Automatic @synthesis and refactoring

With the addition of automatic property synthesis to Objective C, the @synthesize statement is usually no longer necessary. However, the XCode refactoring operation has not been appropriately updated. Renaming ‘foo’ to ‘bar’ in the code below works – the property gets renamed to bar, but it remains linked to an ivar called _foo. Remove the @synthesize statement, however, and refactoring breaks as the implicit ivar will – after refactoring – now be called _bar, but the init body will still try to assign to _foo.


@interface Test
@property id foo;
@end
@implementation Test
@synthesize foo = _foo;
- (id)init {
   if ((self = [super init])) {
      _foo = self;
   }
   return self;
}
@end

A few useful (for me) Erlang functions for 3D work

Here are some functions I have written for working with 3D points, vectors and angles. If you spot any issues, please let me know.

/*
Copyright (c) 2011, 2012 Julian Richardson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

spherical_cartesian( { Phi, Theta, R } ) ->
    { R*sin(Phi)*cos(Theta), R*cos(Phi), R*sin(Phi)*sin(Theta) }.

cartesian_spherical( { X, Y, Z } ) ->
    R = math:sqrt(X*X+Y*Y+Z*Z),
    S = math:sqrt(X*X+Z*Z),
    AZS = if S > 0.0 -> math:asin(Z/S) ; true -> 0.0 end,
    { math:acos(Y/R), if 0.0 =< X -> AZS ; true -> math:pi() - AZS end, R }.

rotate_about_Z(Phi, V) ->
    RotationByPhiAboutZ = { { cos(Phi), sin(Phi), 0.0 }, % was -sin(Phi)
                            { -sin(Phi), cos(Phi), 0.0 }, % was sin(Phi)
                            { 0.0, 0.0, 1.0 } },
    mvmul3(RotationByPhiAboutZ, V).

rotate_about_Y(Theta,V) ->
    RotationByThetaAboutY = { { cos(Theta), 0.0, -sin(Theta) },
                              { 0.0,      1.0, 0.0 },
                              { sin(Theta), 0.0, cos(Theta) } },
    mvmul3(RotationByThetaAboutY, V).

mvmul3( { { A, B, C },
          { D, E, F },
          { H, I, J } },
        { K, L, M } ) ->
    { A*K+B*L+C*M, D*K+E*L+F*M, H*K+I*L+J*M }.

mmmul3( { { A, B, C },
          { D, E, F },
          { H, I, J } },
        { { K, L, M },
          { N, O, P },
          { Q, R, S} } ) ->
    { { A*K+B*N+C*Q, A*L+B*O+C*R, A*M+B*P+C*S },
      { D*K+E*N+F*Q, D*L+E*O+F*R, D*M+E*P+F*S },
      { H*K+I*N+J*Q, H*L+I*O+J*R, H*M+I*P+J*S } }.

smmul3(X, { A, B, C })  ->
    { svmul3(X, A), svmul3(X, B), svmul3(X,C) }.

mdet3( { { A, B, C },
         { D, E, F },
         { G, H, I } } ) ->
    A*E*I+B*F*G+C*D*H-A*F*H-B*D*I-C*E*G.

svmul2(S, {X,Y}) ->
    {S*X,S*Y}.

vvplus2({A,B},{C,D}) ->
    {A+C,B+D}.

vvminus2({A,B},{C,D}) ->
    {A-C,B-D}.

vvplus3({A,B,C},{D,E,F}) ->
    {A+D,B+E,C+F}.

vvminus3({A,B,C},{D,E,F}) ->
    {A-D,B-E,C-F}.

mvmul2({{A,B},{C,D}},{X,Y})->
    {A*X+B*Y,C*X+D*Y}.

svmul3(S,{X,Y,Z}) ->
    {S*X, S*Y, S*Z}.

normv3(V) ->
    svmul3(1/len(V),V).

len({X,Y,Z}) ->
    math:sqrt(X*X+Y*Y+Z*Z).

vdot2({A,B},{C,D}) ->
    A*C+B*D.

vdot3({A1,A2,A3},{B1,B2,B3}) ->
    A1*B1+A2*B2+A3*B3.

mid3() ->
    { { 1.0, 0.0, 0.0 },
      { 0.0, 1.0, 0.0 },
      { 0.0, 0.0, 1.0 } }.

crossProduct3( { A1, A2, A3 }, { B1, B2, B3 } ) ->
    { A2*B3-A3*B2, A3*B1-A1*B3, A1*B2-A2*B1 }.

vlen3(V) ->
    math:sqrt(distancev3(cartesian, V, { 0.0, 0.0, 0.0 } )).

vlen4({ X, Y, Z, W }) ->
    math:sqrt(X*X+Y*Y+Z*Z+W*W).

qlen(Q) ->
    vlen4(Q).

qnorm(Q) ->
    S = qlen(Q),
    { X, Y, Z, W } = Q,
    { X/S, Y/S, Z/S, W/S }.

angle_between(V1,V2) ->
    math:acos(vdot3(normv3(V1),normv3(V2))).

axis_angle_to_quaternion(V,Angle) ->
    { Ax, Ay, Az } = normv3(V),
    SinA = math:sin(Angle/2.0),
    CosA = math:cos(Angle/2.0),
    qnorm({ Ax*SinA, Ay*SinA, Az*SinA, CosA }).

quaternion_to_matrix(Q) ->
    { X, Y, Z, W } = Q,
    { { 1 - 2 * (Y*Y+Z*Z), 2 * (X*Y + Z*W), 2 * (X*Z - Y*W) },
      { 2 * (X*Y - Z*W), 1 - 2 * (X*X+Z*Z), 2 * (Y*Z + X*W) },
      { 2 * (X*Z + Y*W), 2 * (Y*Z-X*W), 1 - 2 * (X*X+Y*Y) } }.

My take on RIM

RIM is obviously in deep trouble. It’s stock price has fallen 75% in the last year and it just lost its dual CEO’s.

The value in RIM has always been twofold:

  • its technology: back end, phone OS, buy especially secure email and messaging.
  • its customer base

Its technology has failed to keep pace with innovation in iPhone and Android phones, and now in tablets. Its user base has been steadily eroded. I don’t know by how much.

Going forward, RIM’s value depends on how well it can retain its customer base, and how well it can dramatically improve its technology. Retaining its customer base relies on improving the technology, so in the end it is all about the technology.

There are two possible routes to take with the technology:

  • Follow the example of Nokia and others and ditch its proprietary OS in favour of Android or Windows OS. I don’t think that this way it retain anywhere near the revenue it needs to support its market cap. In addition, it would be a lot of work to integrate RIM’s proprietary network into another OS – it’s deep stuff.
  • Double down on its own technology. I think this is the way to go.

So, my advice to RIM is: double down on your technology. Use some cash or sell some assets to hire a wonder-team of developers and product people.

git

I started using version control using SCCS – a single user version control system. Then I moved to CVS, which almost made me through a CRT monitor out of a 3rd floor window. Then came SVN and that was better. Now I am using git. Since git’s version control model is distributed, it is taking bit of getting used to.

I found this succinct tutorial, which nicely explains commits, branches, staging etc.

Today I need to add a branch to a project, but I have already made some uncommitted changes. I need the stash.

That is all.

Not abusing UIViewControllers

I having been mulling for a while an interesting conundrum. I have some code, which is designed to work as a library. It makes no assumptions about any view controller which is active when it is used, and so directly adds things to the view using addSubview: on the application’s main window (view). The initial problem with this approach is that the newly added views have no view controller.

I fixed that by defining a view controller with an appropriate view, but then how to get that displayed. Without any assumptions about the context in which the code is used, the best I could do was instantiate the view controller and then add its view as a subview of the application’s main window as before. An immediate undesirable I discovered was that the new view controller’s viewDidLoad: was never called.

After some fishing around, I found a few useful articles:

Abusing UIViewControllers – presents the case that you should NOT do what I initially do above, force the new view controller’s view into the view hierarchy.

Writing high-quality view controller containers – discusses the same problem and how to write custom view controllers which manage multiple view controllers, like the iOS SplitViewController.

The solution I am going to adopt now is to require the code calling the library to provide a view controller onto which the new view can be pushed using pushModalViewController: