Joe Rawlings Software Engineer

1May/110

Eclipse 3.7 and 4.1 M7 – New and Noteworthy

Some pretty cool new features are available in the upcoming Indigo release of Eclipse. A new particular feature that would be amazing for me right now would be the ability to export and install software/plugins from an eclipse installation. At home, I'm migrating from a 10.10 Ubuntu to an 11.04 Ubuntu installation and that feature would have saved me a bit of time. Likewise at work, we are migrating from XP to Windows 7. Check it out, the lightweight refresh sounds interesting as well!

Tagged as: , No Comments
6Mar/110

Scala Day 2 – Higher-Order Functions

Following up on Day 1, here's day 2 from 7 languages in 7 weeks. These problems are quite a bit more abstract but I believe the author just wanted us to get familiar with higher-order functions. The more I get into Scala the more I start to agree with Jim McBeath on the pros and cons. However, it seems like quite a bit of work is being put into the scala IDE for Eclipse and the IntelliJ scala plugin (which is what I'm using currently). Also, Martin Ordesky has created a company to address the 'commercial support' concern but many more companies would have to provide scala services for this to fall off the con list, I agree.

object FoldingStrings extends Application {
  val strings = List("this", "is", "a", "list", "of", "strings")

  // initValue + code block
  val sum1 = (0 /: strings) { (sum, string) => sum + string.length}
  // with currying
  val sum2 = strings.foldLeft(0)((sum, string) => sum + string.length)
  // should print 20/20 vision
  println(sum1 + "/" + sum2 + " vision")
  // foul sentence
  val sentence = List("Darn", " ", "it!", " ", "Get", " ", "at", " ", "em,", " ", "Shoot")

  val foulMouthPerson = new FoulMouth
  // should print 'Beans it! Get at em, Pucky'
  foulMouthPerson saySomethingCensored sentence
}

class FoulMouth extends Censor {

  def saySomethingCensored(sentence:List[String]) { println(censor(sentence)) }
}

trait Censor {

  val alternatives = Map("Shoot" -> "Pucky", "Darn" -> "Beans")

  def censor(words : List[String]) : String = {
    // wonder if this is what the author intended
    words.foldLeft("")((sentence, word) => sentence + alternatives.getOrElse(word, word))
  }
}
5Mar/110

Scala Day 1 Tic-Tac-Toe

I've been reading the book 7 Languages in 7 Weeks and I have been really enjoying it so far. I've finally hit the Scala section for which I have some long-term interest in. The first day had a couple of trivial problems, including a Tic-Tac-Toe one that I provided my amateur solution for :) As I go through more, I will be posting my solutions.

If any readers have any suggestions, please speak up. Also, if you couldn't tell, I come from a mostly java background :)

import collection.mutable.ArrayBuffer
import util.Random

object TicTacToe extends Application {
  val board = ArrayBuffer("-")

  val winningIndexTuples = List(List(0, 1, 2),
                                List(3, 4, 5),
                                List(6, 7, 8),
                                List(0, 4, 8),
                                List(2, 4, 6),
                                List(0, 3, 6),
                                List(1, 4, 7),
                                List(2, 5, 8))

  // 8 plus the one in the cell
  for (x <- 1 to 8) {
    board += "-"
  }

  var turn = true
  var winner = false

  var rnd = -1

  println(board)

  while((board contains "-") && !winner) {

    do {
      rnd = Random nextInt 9
    } while(board(rnd) == 0 || board(rnd) == 1)

    if(turn) { board.update(rnd, "X")}
    else { board.update(rnd, "O")}

    turn = !turn;

    winner = checkBoard
  }

  println(board)

  def checkBoard = {
    val winner = checkWinner("X") || checkWinner("O")
    if(winner) { println("winner!") }
    winner
  }

  def checkWinner(player : String) = {
    winningIndexTuples.exists(winningIndexTuple => winningIndexTuple.forall(board(_) == player))
  }
}
29Nov/100

Git – Peepcode Screencast Review

The other screencast i finally got around to watch was the Git - PeepCode Screencast. It's about 61 minutes long and can be purchased here. This is my review.

LIKES:
The screencast was a pretty high level, albeit detailed, overview of the git version control system and was at the level I needed (beginner). The flow from part 1 to part 8 was very seamless and covered about everything a regular user would need to know to use Git at work. Some features were covered that I were new to me (from an SVN perspective), including stashing files and applying hunks instead of the whole patch.

GRIPES:
Like the clojure screencast, things moved around pretty fast (I'm starting to get accustomed to it, though). I didn't quite get the advantage of rebasing before a merge (this and this provide some good rationale). I know this wasn't a comparison screencast between VCSs (notably, SVN), but I was left wondering where the advantages were when moving from SVN to Git. This led me to some searching with links in the resources section that helped. I was mostly confused on the distributed workflow part (part 8 ) because he was going through pretty arbitrary changes to branches and I had a hard time mapping those activities to a work environment. When he was switching between authors/committers, that made a bunch more sense to me. One last thing, the speaker was about 2-3 seconds behind the commands going to the CLI which made understanding difficult especially when a 'clear' was issued and a concept was being wrapped up.

TAKEAWAYS:
git add -i ; for interactive staging, afterwards don't do 'git commit -a'
partial commit of files w/ patching
git log --stat, --shortstat, --pretty=full for logging statistics
In general, git log is extremely full featured (searching)
git blame file to see line numbers and who modified it and when
git instaweb --httpd=webrick ; to browse the repo
git stash to put away files that aren't ready to be committed in order to switch branches
run git add on a file after you have cleared out the merge conflicts
git reset --hard will revert changes back to the latest good version of the branch

CONCLUSION:
This screencast was similar to the Clojure one in that as a developer would go through software development lifecycle, various features were touched on in order (e.g. checking out a repo, adding files, committing, checking status, etc). I enjoyed this screencast less than the Clojure one but still found it valuable from a beginner's perspective. Still, at $12, this screencast provides good value for those that want to get an easy jump start on Git.

RESOURCES:
Some links I used to supplement my understanding with the PeepCode Screencast
Git - SVN Crash Course
Git Tutorial
GitSvn Comparison

27Nov/100

Functional Programming with Clojure – PeepCode Screencast Review

I finally got around to watch the PeepCode Screencast titled 'Functional Programming with Clojure'. It's about 65 minutes long and can be purchased here. This is my review.

LIKES:
The screencast was well done with pretty high production value. I thought it had just the right amount of background information with the rest being purely technical. It had a good introduction to the language. The Mire example was pretty sweet which covered what would appear to me to be a majority of the functionality I would need to get started (e.g. basic collection usage and manipulation, concurrency, exception handling, testing, etc). The simple command line examples helped quite a bit, although they went quite fast.

GRIPES:
For a beginner, some of the earlier topics were covered fairly quickly (the let vs binding syntax, alter), but reviewing afterwards, I understood what was going on. Some more explanation on why certain imports were used (for what specific purpose like duck-stream) would've. Also, at first, I didn't understand the dot at the end of 'java.util.Date.' and how that worked but it was covered later when more java functions were used. The only thing that would have really made this screencast amazing would have been some independent work examples for the listener to do. I'm a visual learner but doing things hands-on is very appropriate for a lot of people (I like it as well :)). A quick review of the 'answer' would have really sealed some concepts.

TAKEAWAYS:
REPL: Read-eval-print loop
Binding are thread-local
Usually a bad idea to call def inside a function body
Transactions work like database transactions (dosync ...)
Only modify refs in transactions to guarantee predictable behavior
@ is a shortcut for deref
It's OK to use java libraries for system level operations (e.g file io)
Names can have punctuations in them, such as a question mark
(doc map) or some other function or type to get the documentation on it in the REPL
Lazy sequences are only evaluated when their return value is used
test-is is Clojure's test library
Function order matters
You can't compile code in a namespace less than 2 segments

CONCLUSION:
All in all, the screencast was very well-made and informative. After finishing the screencast, I'm sort of glad they went over material fast which allowed more material to be covered. Then, as I start to program my own stuff, I can just reference the screencast as needed. Although, seeing the same code over and over help solidify the concepts quite a bit. The $12 price tag for an hour's worth of technical content is well worth it. I would recommend this to others.

26Nov/101

Java Puzzle 6

What does the program print?

public class Elementary {
  public static void main(String... args) {
    System.out.println(12345 + 5432l);
    System.out.println(01234 + 43210);
  }
}

a) 17777 44444
b) 17777 43878
c) 66666 44444
d) 66666 43878

Tagged as: , 1 Comment
26Nov/101

Java Puzzle 5

What does the program print?

public class LongDivision {
  private static final long MILLIS_PER_DAY
    = 24 * 60 * 60 * 1000;
  private static final long MICROS_PER_DAY
    = 24 * 60 * 60 * 1000 * 1000;

  public static void main(String... args) {
    System.out.println(MICROS_PER_DAY / MILLIS_PER_DAY);
  }
}

a) 5
b) 1000
c) 5000
d) Throws an exception

Tagged as: , 1 Comment
26Nov/101

Java Puzzle 4

What does this program print?

public class Size {
  private enum Sex { MALE, FEMALE }

  public static void main(String... args) {
    printSize(new HashMap<Sex, Sex>());
    printSize(new EnumMap<Sex, Sex>(Sex.class));
  }

  private static void printSize(Map<Sex, Sex> map) {
    map.put(Sex.MALE, Sex.FEMALE);
    map.put(Sex.FEMALE, Sex.FEMALE);
    map.put(Sex.MALE, Sex.MALE);
    map.put(Sex.FEMALE, Sex.MALE);
    Set<Map, Entry<Sex, Sex>> set =
      new HashSet<Map, Entry<Sex, Sex>>(map.entrySet());
    System.out.println(sex.size());
  }
}

a) 1 2
b) 2 1
c) 2 2
d) None of the above

Tagged as: , , 1 Comment
26Nov/101

Java Puzzle 3

What does it print?

public class Match {
  public static void main(String... args) {
    Pattern p = Pattern.compile("(aa|aab?)+");
    int count = 0;
    for(String s = ""; s.length() < 200; s+= "a")
      if (p.matcher(s).matches())
        count++;
    System.out.println(count);
  }
}

a) 99
b) 100
c) Throws an exception
d) None of the above

Tagged as: , , 1 Comment
26Nov/101

Java Puzzle 2

If you pay 2.00 for a gasket that costs $1.10. How much change do you get?

public class Change {
  public static void main(String... args) {
    BigDecimal payment = new BigDecimal(2.00);
    BigDecimal cost = new BigDecimal(1.10);
    System.out.println(payment.subtract(cost));
  }
}

a) .9
b) .90
c) .89999999999999
d) None of the above