Phillip Trelford's Array

POKE 36879,255

Pissed off owls

Last night Neil Danson gave a great presentation on F# on iPad and iPhone in Xamarin Studio at the F#unctional Londoners meetup hosted by Skills Matter. Xamarin announced that they were working towards first-class F# support at their recent Evolve conference, and that support is currently in Beta. Xamarin Studio is an IDE that runs on Mac, Linux and Windows and lets you target iOS and Android.

Easy to follow instructions: Use F# for iOS development

Neil showed how to build a movie database application using the IMDB API that runs on iPad and iPhone in just over 100 lines. It makes use of F# Object Expressions and Async Workflows. The source if available on Bitbucket: https://bitbucket.org/thedo666/imdb

One of the really impressive parts is the readability, the following statement taken from the code composes the application:

start ==> search ==> show_results ==> open_web_page ==> finish

The next demo was a game with the imaginative title “Pissed off owls”, that lets you launch incensed rotund avians at obstacles with physics. Again the source code is just over 100 lines and available on Bitbucket: https://bitbucket.org/thedo666/pissed-off-owls

Pissed off owls

It is written with MonoGame and uses the Farseer physics engine. Here’s the code to set up the scene:

let player = Actor("Owl",Position(30.f,8.f), Circle, Player, Dynamic)
let level =  [ Actor("Background", Position(160.f,120.f), Box, Ignore, None)
                Actor("Catapult", Position(70.f,32.f), Box, Ignore, None)
                Actor("Mouse", Position(185.f,15.f), Circle, Enemy, Dynamic)
                Actor("Mouse", Position(285.f, 15.f), Circle, Enemy, Dynamic)
                Actor("Tower", Position(225.f, 64.f), Box, Prop, Dynamic) ]

The video to the talk should be available later today: http://skillsmatter.com/podcast/scala/f-on-ipad-and-iphone-with-xamarin-studio

Neil Danson

Another demo showed briefly at the end was PacMan, written in F# and running on iPad. The source code is available on Bitbucket: https://bitbucket.org/thedo666/pacman

Pacman iOS

It’s a port of some code I wrote for a Coding Kata last year, it also runs on Windows in Silverlight, WPF, Windows 8 and Windows Phone. Play it here: http://trelford.com/blog/post/PacTile.aspx

Want to learn more about MonoTouch F#, I’d recommend Dave Thomas’s short series:

    Want to go to an event where YOU write games, Dublin GameCraft is coming to London in August!

Random Walker

This week our automated tools uncovered a fatal exception before it reached the client. Nick and Anton, 2 of the full-time testers on our team have written a testing tool they call Random Walker that simulates interactions with the user interface as clicks and gestures. It has helped uncover numerous issues, particularly NullReferenceExceptions, more often than not emanating from third-party libraries. It typically finds issues that are not easily found by manual testing or our extensive suite of unit and integration tests. Up-time is very important to us and our large client base who interact with our software throughout the day. 

Stack Trace

System.InvalidOperationException: GridViewColumnCollection is read-only now.
 at System.Windows.Controls.GridViewColumnCollection.VerifyAccess()
 at System.Windows.Controls.GridViewColumnCollection.RemoveItem(Int32 index)

Documentation

The exception is thrown after calling RemoveItem method of GridViewColumnCollection, although the MSDN documentation does not specify an exception is expected.

Disassembly

Using a disassembler we can see VerifyAccess will throw if the IsImmutable property is true:

private void VerifyAccess()
{
    if (this.IsImmutable)
    {
        throw new InvalidOperationException(
                      SR.Get("ListView_GridViewColumnCollectionIsReadOnly"));
    }
    base.CheckReentrancy();
}

 

The next question is when does this property get set to true. Using Telerik’s JustDecompile you can right click the property and select Find Usages, which shows its set from methods BlockWrite and UnblockWrite.

BlockWrite is called by the GridViewHeaderRowPresenter.StartHeaderDrag private method.

UnblockWrite is called by GridVIewHeaderRowPresenter.FinishHeaderDrag.

StartHeaderDrag is called from the MouseMove handler.

FinishHeadDrag is called from OnMouseLeftButtonUp, OnLostMouseCapture and OnColumnsPresenterKeyDown.

Problem

There appears to be an undocumented assumption that the columns can not be updated either during or on completion of a drag operation. However our front end lets you add or remove columns from a table via a separate menu, so it is possible for an update operation to come from outside the drag gesture.

Workaround

One workaround is to pre-empt the call to VerifyAccess by checking the IsImmutable property as it is possible to invoke private methods via refection:

bool IsImmutable()
{
    var flags = BindingFlags.Instance || BindingFlags.NonPublic;
    var property = columns.GetType().GetProperty("IsImmutable");
    return (bool)property.GetValue(columns, new object[] {});
}

 

Closing Thoughts

The Random Walker UI testing tool has really helped flush out those hard to find issues, if up-time is important to your customers, you may want to consider using one. Bugs exist even in mature frameworks like WPF, and you may need to set aside some time to working around them. Encapsulation seems good in theory, but I’m thankful that we can disassemble the source of third-party libraries and dynamically invoke private members when necessary.

Modern Art

A few backs I visited New York City taking in some of the many sites like the Empire State Building for the views, Grand Central Terminal for the oysters, Central Park for waffles & dinges, Harry’s Bar for steaks, the American Museum of Natural History for stars and stuffed animals and the MoMA for modern art.

MoMA

The department of Architecture and Design at the MoMA had 14 video games on display including PacMan. Are video games art? They sure are.

I also found an interesting piece by Philippe Decrauzat entitled D.T.A.B.T.W.H.A.H.E.

DTABTWHAHE

Here’s a short F# snippet to generate similar text blocks:

// Generate text from Philippe Decrauzat's D.T.A.B.T.W.H.A.H.E. 2010
let print (s:string) =
    [|
    for y in 0..s.Length-1 ->
        [|for x in 0..y-1 -> s.[x]
            for x in y..s.Length-1 -> s.[y]
        |]
        |> fun cs -> 
            System.String(cs) + 
            System.String(cs |> Array.rev |> Seq.skip 1 |> Seq.toArray) 
    |] 
    |> fun ys -> [|yield! ys; yield! (Array.rev ys |> Seq.skip 1)|] 
    |> String.concat "\r\n"
print "A HAPPY ENDING"

F# for Trading

While I was in New York I also gave a talk on F# for Trading at the local F# User Group:

Thanks for all the kind feedback.

Tate Modern

I work round from the corner from the Tate Modern in London. Last week I popped in and on my exit through the gift shop I picked up a book with the title Null Object and a postcard of a painting called Hyena Stomp by Frank Stella:

hyena stomp

I liked it as it reminded me of the end of level sequence in Robotron 2084 & Llamatron:


 

As an aside, I've been having a lot of fun recently making bleeps and beeps, more often found in 80s video games, with Korg's Monotribe analogue synth.

Inspired by Llamtron I’ve created a simple animated version of Hyena Stomp in Silverlight:


Music

Why not sit back and relax with some art inspired music: