scheme cond in scala language

I guess you're looking for match (or just simply if/else if/else).


case class Paired(x: Int, y: Int)

def foo(x: Any) = x match {
  case string : String => println("Got a string")
  case num : Int if num < 100 => println("Number less than 100")
  case Paired(x,y) => println("Got x and y: " + x + ", " + y)
  case unknown => println("??: " + unknown)
}

The first two case statements show type based pattern matching. The third shows the use of an Extractor to break data down into constituent parts and to assign those parts to variables. The third shows a variable pattern match which will match anything. Not shown is the _ case:

case _ => println("what")

Which like the variable pattern match, matches anything, but does not bind the matched object to a variable.

The case class at the top is Scala shorthand for creating an extractor as well as the class itself.


Of course, neither match nor if does exactly the same thing as cond. One possibility is to do like this:

object Cond {
  def apply(clauses: Iterable[(()=>Boolean, ()=>Any)]): Any = {
    clauses find (_._1()) map (_._2()) getOrElse ()
  }
}

This object accepts something Iterable where each item is a pair of a function returning Boolean and a function returning Any. It tries to find an item whose first function returns true, stops looking if it finds one, calls the second function on a found item and returns the result of that function (or () if none was found).

Examples:

val clauses = Seq(
  ({()=>false}, {()=>println("foo")}),
  ({()=>true}, {()=>println("bar")})
)
Cond(clauses)

def checkYear(year: Int) = {
  Cond(Seq(
    ({()=>year % 400 == 0}, {()=>42}),
    ({()=>true}, {()=>{c:Char => (c.toString * 3)}})
  ))
}

ETA: Yes, I know it is ugly, but it works.

Tags:

Scala