Skip to main content

Finished Exercise #1 for Scala

I've just finished exercise #1 from: knowing.net. Please feel free to critique this code, I am serious about learning Scala and getting introduced to members of the community.

Calculator.scala

package activeactive
class Calculator {
  def main(args:Array[String]): Double = eval(args.toList)
    def eval(args:List[String]): Double = args match {
      case null =>
        throw new IllegalArgumentException(
          "args cannot be null-valued")
      case Nil =>
        throw new IllegalArgumentException(
          "You must provide a function name as the first argument")
      case "sum" :: rest => sum(convertList(rest))
      case "prod" :: rest => product(convertList(rest))
      case "mean" :: rest => mean(convertList(rest))
      case "sqrt" :: rest => sqrt(convertList(rest))
      case _ => throw new IllegalArgumentException(
        "invalid function name. Use 'sum', 'prod', 'mean', or 'sqrt'.")
    }
    def convertList(list: List[String]): List[Double] = list match {
      case Nil => Nil
      case x :: subList => x.toDouble :: convertList(subList)
    }
    def sum(list: List[Double]) =
      (0D :: list) reduceLeft ((x, y) => x+y)
    def product(list: List[Double]) =
      (1D :: list) reduceLeft ((x, y) => x*y)
    def mean(list: List[Double]) = list match {
      case Nil => throw new IllegalArgumentException(
        "The mean function requires at least one operand")
      case _ => sum(list) / list.size
    }
    def sqrt(list: List[Double]) =
      if (sum(list) <= 0)
        throw new IllegalArgumentException(
          "the input values '" + list.toString +
            "' resulted in a sum <= zero.")
      else if (sum(list) == 1D) 1D
      else Math.sqrt(sum(list))
}
EDIT: updated knowing.net link.

Comments

Unknown said…
Good job! I like the link to knowing.net - useful. I'll try them out. Lisp isn't dead yet, is it?

Popular posts from this blog

Using MonoDevelop to Create an ASP.NET Web Service

NOTE : instructions below are for MonoDevelop 2.6 Beta 2 - built on 2011-04-06 03:37:58+0000 Getting Started Create a new ASP.NET Web Application in MonoDevelop: From the menu, select: File → New → Solution… Expand C# . Select ASP.NET → Web Application . Enter a name for the ASP.NET project that will be created in the solution in Name: . Change the root location for the solution in Location: , if desired. Change the name of the root solution in Solution Name: , if desired. The Results – I What you have after executing the new ASP.NET Web Application project wizard is a solution containing one ASP.NET Web Application project. In the default project view in MonoDevelop, you'll find the following items: Default.aspx – This is the default web form rendered and presented in the browser when http://<server>:<port>/ is accessed. Default.aspx.cs – This C# file contains the developer-created common code and event handlers which can be used to affect the process

Testing Toolbelt: SpringJUnit4ClassRunner

The org.springframework.test.context.junit4.SpringJUnit4ClassRunner class is another implementation of the JUnit TestRunner class which is used to enable various features of Spring for every run of the test class and every test within it. To use the features provided by the SpringJUnit4ClassRunner class, you need to mark the class using the RunWith annotation using SpringJUnit4ClassRunner as its parameter. In addition to the custom test runner, you will want to mark the class with the ContextConfiguration annotation. The ContextConfiguration annotation is used to mark classes which will automatically read a Spring configuration file and use it to create an ApplicationContext . By default, this file located at <package path>/<test class name>-context.xml . Use the locations argument to over-ride. The ApplicationContext used by the Spring-integrated test will only be loaded once for the whole test class. This behavior can be over-ridden by annotating a test metho

Getting Started with .NET on the Mac

I'm setting out to learn .NET and get some experience creating a non-trivial project. Microsoft does provide Express (free, Windows-only) editions of the Visual Studio application in a few flavors as well as basic version of IIS with ASP.NET and SQL Server. But, since my current personal development environment is a MacBook Pro (OSX 10.6.7), getting started with development on .NET can actually cost money (mostly due to the Windows tax) . The primary development tool for .NET developers on non-Windows systems seems to be Mono with MonoDevelop . The latest stable release of Mono (2.10.1) supports much of the functionality of the .NET 4.0 platform and some portions of Microsoft's extended .NET eco-system: F#, IronRuby, IronPython, ASP.NET MVC(1, 2, and portions of 3) . The latest beta build of MonoDevelop (2.6 beta 2) provides a lot of support for developing applications using C# and the rest of the CLR. I'll be using these in the coming months to do some experimen