Skip to content

Instantly share code, notes, and snippets.

View whaley's full-sized avatar

Jason Whaley whaley

  • Asheville, NC
View GitHub Profile
@whaley
whaley / gist:1240822
Created September 25, 2011 16:46
TicTacToe Scala Case Classes
package com.jasonwhaley.tictactoe
sealed abstract class GameState {
val nextTurn : TicTacToeSpace
val message : String
val isGameFinished : Boolean
}
case class XMovesNext extends GameState {
@whaley
whaley / gist:1240823
Created September 25, 2011 16:47
TicTacToe Scala - Pattern Matching
def getGameState(board: TicTacToeBoard): GameState = {
board.getWinner match {
case Some("X") => return new XWins
case Some("O") => return new OWins
case _ => //noop
}
board.spacesUsed match {
case 9 => return new StaleMate
case number if number % 2 == 0 => return new XMovesNext
case _ => return new OMovesNext
@whaley
whaley / gist:1240826
Created September 25, 2011 16:48
TicTacToe Scala - Pattern Matching for Winner
def getWinner() : Option[String] = {
val topRow = List( grid(0)(0), grid(0)(1), grid(0)(2) )
val middleRow = List( grid(1)(0), grid(1)(1), grid(1)(2) )
val bottomRow = List( grid(2)(0), grid(2)(1), grid(2)(2) )
val leftColumn = List( grid(0)(0), grid(1)(0), grid(2)(0) )
val middleColumn = List( grid(0)(1), grid(1)(1), grid(2)(1) )
val rightColumn = List( grid(0)(2), grid(1)(2), grid(2)(2) )
val diagonalTopLeft = List( grid(0)(0), grid(1)(1), grid(2)(2))
val diagonalTopRight = List( grid(2)(0), grid(1)(1), grid(0)(2))
String[] qa = new String[] {"Who was the first pres of the USA","George Washington"};
//... and later on
String question = qa[0];
String answer = qa[1];
#!/bin/bash
rootDirectory="$1"
stringToMatch="$2"
for jarfile in $(find "$rootDirectory" -name '*.jar')
do
grepOut=$(jar tvf "$jarfile" | grep "$stringToMatch")
if [[ $grepOut ]]
then
@whaley
whaley / gist:1240834
Created September 25, 2011 16:53
findInJar.sh output
[whaley@sunspot ~/inbox]
> ./findInJar ~/opt/jetty-6.1.15 Main
Matching file(s) found in /Users/whaley/opt/jetty-6.1.15/lib/jetty-6.1.15.jar
org/mortbay/jetty/Main.class
Matching file(s) found in /Users/whaley/opt/jetty-6.1.15/lib/jsp-2.0/ant-1.6.5.jar
org/apache/tools/ant/Main.class
org/apache/tools/ant/helper/ProjectHelper2$MainHandler.class
Matching file(s) found in /Users/whaley/opt/jetty-6.1.15/lib/jsp-2.0/jasper-compiler-jdt-5.5.15.jar
org/eclipse/jdt/internal/compiler/batch/Main$1.class
org/eclipse/jdt/internal/compiler/batch/Main$2.class
@whaley
whaley / gist:1246120
Created September 27, 2011 20:23
itertools and boto
import itertools
import boto
ec2 = boto.connect_ec2()
chain = itertools.chain.from_iterable
existing_instances = list(chain([res.instances for res in ec2.get_all_instances()]))
print existing_instances #gives you a list of all instances
@whaley
whaley / gist:1278159
Created October 11, 2011 14:04
`brew install quassel` output
>
koan("Using Option to avoid if checks for null") {
//the ugly version
def makeFullName(firstName: String, lastName: String) = {
if (firstName != null) {
if (lastName != null) {
firstName + " " + lastName
} else {
null
}
} else {
def gcd(a : Int, b : Int) : Int = {
(a,b) match {
case (_,0) => a
case (0,_) => b
case (_,_) => gcd(math.min(a,b), math.abs(a - b))
}
}