Scheme presentation and more fun with Scala/Swing

June 7, 2009 at 9:50 pm (Scala, Scheme, Week 11) (, , , , )

I gave my lunch & learn presentation on Scheme Friday, and it went pretty well. I found that I had forgotten to correct one of my slide mistakes, but aside from that I felt good about it. I went through the basics of Scheme/Lisp syntax and talked through some examples, and then finished up with the prime factors kata in Scheme. It went almost twice as long as I’d expected, since we stopped several times to discuss and answer questions. I took suggestions on the kata as well, which also slowed things down, but I felt like it was a better way to teach the process of refactoring to an inner recursive function (in place of what would be a loop in an imperative language). Everyone seemed interested and I got some great advice afterwards. The biggest things were that I didn’t always stand up (sometimes camping out in a chair) and that I kind of half-heartedly slapped a name on the presentation (“Scheme for Rubyists”) with only a few references to Ruby (where I could have had comparable Ruby examples for all my Scheme code).

Here are my [corrected] slides, if you want to get the basic gist (unfortunately, the formatting doesn’t translate perfectly to ppt file format – I used OpenOffice): Scheme Presentation

Friday afternoon and this weekend, I got a basic working version of Tic-Tac-Toe using Swing in Scala. I still have plenty to do: just as before, with Java Swing, I was basically spiking to learn the framework, and so there are some GUI tests missing. For now, I’ve put in TODO’s for implementation code I don’t have tests for; tomorrow I’ll start commenting out those pieces of code and driving them test-first. I feel good about my board and player classes; Friday I implemented some in-memory caching to speed up the computer player’s board-scoring computation. I still need to do some thinking about the technical article part of my apprenticeship challenges. I’m leaning towards something on functional programming, perhaps a few refactorings from imperative code to functional.

Permalink 2 Comments

Crazy Java/Windows problem and some Scheming

May 12, 2009 at 8:34 pm (Java, Limelight, Ruby, Scheme, Week 8) (, , , , , )

As soon as I came in this morning, Micah asked for my help with a bug he’d been working on tracking down in some Java JNA code in the Limelight source. The gist of the code is that it was calling some native code in Windows DLLs, and about half the time, one of the API hooks didn’t get properly installed. We looked and looked, eventually doing some print statements looking for threading issues and other possibilities. Take a look at the code and see if you can spot the problem:

new Thread()
{
  public void run()
  {
    W32API.HINSTANCE appInstance = Kernel32.INSTANCE.GetModuleHandle(null);
    final User32.HHOOK keystrokeHook = User32.INSTANCE.SetWindowsHookEx(User32.WH_KEYBOARD_LL, new KeyboardHandler(), appInstance, 0);
    hookThreadId = Kernel32.INSTANCE.GetCurrentThreadId();
    MsgLoop();
    User32.INSTANCE.UnhookWindowsHookEx(keystrokeHook);
  }
}

Well, if you knew right away that the new KeyboardHandler() might sometimes get garbage collected before the Windows code gets around to calling it, then you’re lying. Just kidding! I am very impressed if so. Once we looked back on the problem, we could see how the problem might have happened, but it was really bizarre. The idea is that because we don’t hold onto a reference to the new KeyboardHandler() that we create, the garbage collector could think we’re all done with it, even though the Windows code really needs to hold onto it. We didn’t look in depth into how JNA translates the Java code into native Windows code, but Micah understands it, and I think he said it passes a function pointer to the Windows code in this case, so when Windows tries to callback to our Java code residing in that pointer, that code is no longer there. Wacky.

We also made an installer using install4j, which seemed really easy. I’m not sure how much setup Micah had already done, but we were able to drop in some custom icons and a splash screen really easily for our application, and it looks good on both Mac and Windows. I did my first work in Photoshop CS 3, and it was weird not so have the Save Optimized As.. option that I’m used to from Photoshop/ImageReady CS.

I spent some time this afternoon practicing recursion by writing methods to identify palindromes. I did it in Scheme, Ruby, and Java (in that order), all TDD, all in different editors. Of course, Ruby was the easiest and clearest by the end (negative array indexing helped out!), but Scheme wasn’t as hard as I’d expected. Things may be getting easier!

By the way, if anyone else is going through SICP and feeling like you need some more interesting assignments than reading the online book and watching the lectures, check out the sample programming assignments on the website. Even the first one (Exercise 11) might blow your mind if you’re like me and not so used to functional programming.

Here’s a taste:

(define foo6
  (lambda (x)
    (x (lambda (y) (y y))))

Give a Curried application of foo6 that evaluates to 3. Keep in mind this means that at some point you have to evaluate an expression which is a function applied to itself! Yikes. I’ll post my solution later, if anybody’s interested, but I don’t want to give it away too easily. I probably spent a good hour thinking this last one over, and learned to think in a different way in the process.

I need to find a new way of embedding code in this blog. Since it’s hosted on WordPress, I can’t include Javascript embeds of a Gist or Pastie, and I also don’t really have control over the styling. Any suggestions?

Permalink 1 Comment

Prime factors and the first client project

May 6, 2009 at 6:34 pm (Flex, Java, Limelight, Ruby, Scheme, Week 7) (, , , , , , )

I worked with Doug almost the whole day today, which was great. This morning when I came in, I happened to mention that last night, I’d done the prime factors kata, inspired by tweets from Doug and Caleb, but I used Scheme: the results are in a Gist. It was pretty frustrating trying to do this in a functional style, purposefully avoiding defining anything other than functions, but I sure learned how poorly I understood Scheme’s list-building constructs (cons, cdr, and car). I have a feeling there are clearer ways to do this, and I’ll definitely try again at some point to make it more fluid (I was completely stumped several times).

So, we spent some time in the morning working through the kata in C++, which was awesome. I hadn’t written any C++ since my only CS course in college, around 10 years ago, and what I wrote then was of course very simple. We used CPPUTest as our unit testing framework, but luckily, Doug already had the tests constructed, so we uncommented one at a time and concentrated on the implementation. I’d like to look into testing C++ a bit more at some point, but it may be awhile, considering all the other things I’m learning. We found several variations on the process of solving this problem, and the process of solving it became clearer, slowly but surely, as we worked through it several times. Then Doug said I should “perform” the kata for Caleb, which I did in Java (and in the process learned how much I rely on my IntelliJ Live Templates when I’m writing JUnit test code!) At some point I may try my hand at screencasting and record myself performing it, but I think I need a little more practice first!

Later on, after I struggled through setting up FlexUnit for the small AIR/Flex I’m building, Doug helped me get my bearings in the client project I’m working on now. We were writing Ruby, HTML, and JavaScript code most of the day, though Objective-C is also part of the project, and Flex/ActionScript may be eventually. It was awesome to be working on a bigger project and finding myself able to figure our what’s going on relatively quickly. My Limelight experience with Tic-Tac-Toe definitely paid off – if I hadn’t spent some time with that, there’s no way I would’ve known what was going on today. Of course, the technology itself wasn’t the sole aim of the Limelight Tic-Tac-Toe project – visual design was a big part of that, too.

At any rate, I’ll obviously have to be a bit more vague about project details now, but I’m excited to be getting into some client work, and I know I’m going to learn a lot from seeing Doug and Micah code on a regular basis.

Permalink 2 Comments

Week 5, Weekend

April 26, 2009 at 8:20 pm (Reading, Watching, Week 5) (, , , , , , , )

I’m constantly amazed by the amount of information available online to help people improve and learn. To my wife’s chagrin, I spent several hours watching video lectures by Hal Abelson and Gerald Jay Sussman (from a 1986 course presentation at Hewlett Packard) and by Douglas Crockford (at Yahoo, I believe).

These aren’t directly related to my assignments for the apprenticeship, but I’m learning a lot. The Abelson/Sussman lectures (also on Google Video) are based on their book, The Structure and Interpretation of Computer Programs (SICP), which I understand was the introductory course at MIT for many years. I recommend the 256k mp4 versions if you’re downloading – the 64k are too hard to make out, and the high-res ones are huge! The language, Scheme, has got some pretty cool features, but to be blunt, the parentheses are pretty ugly. The really interesting part of this language, so far, is that functions are first-class objects, which can be saved into variables, passed into other functions, etc. This isn’t a completely foreign concept to me, as Ruby has closures like Procs and lambdas, but I think it’s the area of Ruby where I feel the least comfortable. Another pretty crazy thing is the way control structures like loops must work: recursion (a function might call itself with a modified index). I feel like I’d have had an easier time beginning to understand the minimax algorithm if I’d watched these videos first – the tree structure in particular was clearly explained, in one example, as it related to recursion.

I’d seen Caleb Cornman‘s tweets about the Crockford videos (they’re also available for download from the YUI Theater site), and realized (for the umpteenth time) that even though I’ve written a decent amount of Javascript in my day, I know basically jack about how the language works. I’ve generally just let the Rails helpers, JQuery, or Prototype to do the dirty work for me, hacking in the rest as needed. So that can work, as it does for many others, but trust me when I say that you don’t want to be debugging ugly Javascript. I’m a little weirded out that I just happened to be learning about lambdas and Lisp, and Crockford mentions several times that how Javascript is the first language with lambdas to really hit the mainstream. Javascript has no concept of classes, only objects! I’m surprised, but intrigued to learn a lot more about the subject. Currently debating whether to start with Crockford’s Javascript: The Good Parts (recommended highly by Jim Suchy) and David Flanagan’s Javascript: The Definitive Guide (the only book Crockford recommended other than his own). The local library has the 4th edition of the Flanagan book – I wonder how much has changed in the most recent (5th) edition… Anyone familiar with these and have advice?

I spent some time during and after each of the videos fooling around with practicing Scheme and Javascript, in Emacs and Firebug (a Firefox add-on), respectively. I’m starting to get the hang of Emacs, which is encouraging. I’d need to do a lot more reading and practicing to get as good with it as I am with Vim, but I feel like I should at least have a rudimentary knowledge of how to get around in Emacs, since it’s one of the most-used editors on Unix machines (and has such strong Lisp integration).

I also did a lot of reading, from Refactoring by Martin Fowler et al., and a book I’m reviewing for the Pragmatic Programmers. I won’t say much about the review book, but it’s very good so far, and I think it’ll turn out wonderfully. They were looking for someone who wasn’t an expert, and as I told Micah, that’s me!

Refactoring is so much more readable than I’d imagined. I had always thought of it in the same category as the Gang of Four Design Patterns book, which I flipped through once at a bookstore and found it to be far beyond my understanding. I think at this point it’d be easier, but Refactoring is extremely clear and easy to read. I wish I’d read it sooner, because it describes a lot of the things I’ve done to clean up my Tic-Tac-Toe (and to make changes to it as they came up). The automated tools in IntelliJ made the process easier, of course, but I’m also discovering that some of my bigger changes could have been made much easier by applying the steps in the refactorings catalog.

It’s always a good sign when you’re excited to get back to work and do some coding, but I’m wondering if I should be doing more actual coding at home. I may get into some Limelight Tic-Tac-Toe tonight – I think that things should fall into place more quickly now that at least some of the game logic is hooked up between Java and Ruby.

Permalink 2 Comments