The Artima Developer Community
Sponsored Link

Java Buzz Forum
Finished Exercise #1 for Scala

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Ryan Ransford

Posts: 71
Nickname: rdransfo
Registered: Jul, 2008

Ryan Ransford is a Java developer working in the enterprise looking to make the world a better place
Finished Exercise #1 for Scala Posted: Jul 29, 2008 11:47 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Ryan Ransford.
Original Post: Finished Exercise #1 for Scala
Feed Title: Active-Active Configuration
Feed URL: http://active-active.blogspot.com/feeds/posts/default
Feed Description: Active-Active Configuration is a blog about making my place in the enterprise world better. This blog is primarily focused on Java articles, but be prepared to be challenged by posts about dynamic languages, agile tools, and the lighter side of geek culture.
Latest Java Buzz Posts
Latest Java Buzz Posts by Ryan Ransford
Latest Posts From Active-Active Configuration

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

Read: Finished Exercise #1 for Scala

Topic: Blogging Dead, or, The Message Becomes Just Another Medium, or, Being Beaten to Death by... Previous Topic   Next Topic Topic: links for 2008-07-22 from PeopleOverProcess.com

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use