Joe Rawlings Software Engineer

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))
  }
}