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

State machines and scala.swing

June 4, 2009 at 10:49 pm (Scala, Scheme, Week 11) (, , , , )

After some morning refactoring on my Scala Tic-Tac-Toe (including moving my computer player’s minimax algorithm to a completely functional style) and speed tweaks, I moved on to thinking about how to integrate a GUI. Micah’s told me it’s not a requirement, but I’d like to have another crack at GUI development – I was very slow thinking things through in Java. Micah and I had talked yesterday about the possibility of a state machine to hold the game state so that my other application logic didn’t need to concern itself with whose turn it was. I have mixed feelings about it: on the one hand, state is what I’m trying to avoid in my functional programming study, but on the other, it’s apparently a very widely applicable design pattern that I ought to learn anyway.

At any rate, I spent the morning and most of the afternoon looking into SMC, the state machine compiler. Apparently there are numerous different ones (all named SMC) floating around on the internet. I believe all of them started with Uncle Bob‘s idea, and the one I’m using does give him credit on the website. It’s really like stepping into a second new language (DSL) writing state tables for the state machine compiler. I’m sure that the benefits will outweigh the time I needed to spend reading the documentation.

At the end of the workday, I fooled around with Scala’s Swing library (a wrapper for Java Swing). There are still some Javadocs I needed to dig into to understand what I was doing, but I was able to pick up the syntax pretty quickly. I have to say, Scala really does seem to be Java++. The Java libraries are still super-easy to grab, and the syntax is similar enough that it feels closer than it did with JRuby. The advantages I do see to Java are a familiar syntax and proliferation of tools, especially IDE support for refactoring – Scala’s IntelliJ plugin is good and has a few refactorings, but it has plenty of room for growth.

I spent some time tonight going back over my slides for tomorrow’s Lunch & Learn (“Scheme for Rubyists”), going through the presentation once for my wife and our dogs. I found a couple of outright mistakes and several extra points I needed to make, so it was good practice. I also went through a kata in Scheme that I’ll do tomorrow for the group if we have time. Wish me luck!

Permalink Leave a Comment

Hacking up a storm: Scala takes to the command line

June 3, 2009 at 10:22 pm (Scala, Scheme, Week 11) (, )

Today was a bit slow going, but I was in a pretty good state of mind, and Scala was feeling a little better. At some point something started to click a little bit on the micro level for functional programming, and I mentioned that I thought functional programming would make testing easier. But when I tried to explain it to Micah, I realized that the problems I had been having with testing in my normal imperative loop were more due to the fact that I hadn’t really thought out what I needed to test in that loop. In fact, my problems were cleared up pretty easily with mocks (hand-rolled — I’m not sure if there’s a mocking library for Scala or not).

Tonight I hacked out the minimax algorithm and have a working (albeit slow) computer player on the console. I got a little stuck a few times, but I remembered that the breakthrough last time (in Java) was to slooooow dowwwwn and make sure I understood precisely for WHOM I was calculating the score for a given board.

I did some Pomodoros when I got home: 25 minutes on, 5 minutes off. I ended up alternating between coding and reading this Programming in Scala book during my “on” sessions, but eventually I got so involved in the minimax problem that I forgot about the timer and just kept going (much to my wife’s chagrin).

I did a few rounds of Scheme code kata as well, in preparation for my Lunch & Learn talk on Friday. I’m realizing as I practice these more that there are a few more topics I need to introduce in the slides portion of the presentation in order to have the kata make sense; probably will get to that tomorrow.

Hooray for progress! Now I have lots of refactoring and GUI code to look forward to.

Permalink Leave a Comment

Language unfamiliarity and design decisions breed frustration

June 2, 2009 at 9:40 pm (Scala, Week 11) (, )

Today was all Scala, all the time. I came in thinking that I knew my stuff after my refactoring discoveries yesterday, but I quickly took a detour into Frustration-Land as I hacked away on Tic-Tac-Toe.

I struggled with some design decisions, as I wanted to do things from scratch and avoid too many parallels with my Java version. These problems I can’t really blame on my noob-ness with the language; I’m just trying to shift my paradigm to a more functional style. I don’t know yet if I’ll be implementing a GUI for my Scala version (apparently there’s a way I can hook into Swing), but I’m trying to think ahead, since I had lots of issues when moving my Java version from console to GUI. I’m debating whether to have my Player classes (driven by my Game class) waiting for input from my event listeners – I got the impression that this design (which I used in Java) was kind of unorthodox – or if I should instead have the application flow driven more by the events. I’m leaning in that direction, but need to do some more whiteboarding and reading. I did quite a bit of reading on and practice with the Actor model to see if I might be able to apply it to GUI event and game messages, but I’m concerned it might be an extra layer of complexity that’s unneeded.

My woes with Scala were basically due to me not knowing the libraries, and that will definitely come with time. I bought a book last night: Programming in Scala by Martin Odersky (the language creator), Lex Spoon, and Bill Venners. I’d checked out a bit of Dean Wampler and Alex Payne’s online book draft, which is great, but I just like having paper – I stare at the laptop enough during the day.

When I moaned that I had been a little frustrated today with my (lack of) progress on Tic-Tac-Toe, Micah pointed out tactfully that I had kind of jumped into things before really learning much of the language. It’s true – I felt like I needed to be constantly writing tests and typing code today, but what I should probably have done is take a step back and spend some time learning the language: reading, code kata, and typing examples from the book. I’ve spent some time tonight catching up on the language, and I’m going to continue some practicing now. There’s a lot to be said for applying a brand-new language (and paradigm) to a problem, but the less familiar the language, the more work I need to do before I feel comfortable in an actual project.

Permalink 1 Comment


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

After Caleb and I finished setting up a Slicehost account for a site we’d worked on, Micah met with me to talk about my apprenticeship challenges. I have three major projects to complete by the end of next week: a presentation at this coming Friday’s Lunch & Learn, an article on something I’d learned recently as part of my apprenticeship (more formal and expert-level than posts on this blog), and another version of Tic-Tac-Toe, this time in a language I’d never used before. After a bit of waffling on my part among Clojure, C++, Objective-C, and Scala, I finally settled on Scala.

I’d attended Dean Wampler’s excellent talk at this past weekend’s Chicago Code Camp, so I had some basic syntax knowledge coming in, and there are a lot of similarities to Java. But I thought I should spend some time getting a decent handle on the basics of the language before diving into Tic-Tac-Toe. I discovered what looks like the best test framework for Scala: ScalaTest. It’s pretty nifty, allowing several styles of testing, including one that looks suspiciously like RSpec. There’s also an IntelliJ plugin for Scala, which works well. Since I’m still getting the hang of all of IntelliJ’s features and options, it did take me awhile to get the IDE set up to run tests and write code, but by the end of the afternoon, I had my first failing test.

Things were pretty slow going since I was having to look up basic syntax at first, but I’m already seeing some really cool things in Scala. For one thing, I can mix functional and object-oriented ideas in the same program. It’s hard for me to think about things like a Board outside of an object-oriented space, which makes Scheme difficult for me (so far), but Scala deals with classes and objects without problems. My first implementation code (on the board class) looked basically like Java with a weird syntax and a fancy & functional map method:

def full: Boolean = { =>
     if(position == null)
       return false
   return true

And I thought this was pretty concise, so I was fairly proud. But I did some reading later this evening, and now I’m really psyched about the refactoring Scala allowed me to do:

def full: Boolean = {
  !positions.exists(_ == null)

This seems more like Ruby, but with a really beefed-up version of the Symbol#to_proc syntax that we get with Rails (1.1+) or Ruby 1.9. Nice, right?

Permalink 2 Comments