logo       

Re: assorted beginner questions: msg#00441

lang.scala

Subject: Re: assorted beginner questions

I'll take a shot at some of the low hanging fruit...

Yang wrote:

are there multi-line string literals in scala?
Yes:

"""
This is a multi
line
string
"""

Unfortunately, there's no substitution of values like Ruby (hint, hint):
"""" -- note the 4th quote
This is a multi
line
String
created at #{new java.util.Date}
""""


are there any form of nested comments in scala?

No.  Comments in Scala are like comments in Java.
does scala have keyword arguments a la lisp/python?

No.  However, for multi-arguments, I like the construct:

def foo(arg1: int, arg2: String, optArgs: List[FooArgTypes])

abstract class FooArgTypes
case class Other(o: int) extends FooArgTypes
case class Another(x: NodeSeq) extends FooArgTypes
...

foo(arg1, arg2, Other(1) :: Another(<XML/>) :: YetAnother("Moo") :: WeRarelyUseThisOne(4) :: Nil)


The last "var arg" can be processed easily with pattern matching.
is there any special syntax for hashtable/map/set gets/updates?
It's not special syntax, but there's flexible syntax:
scala> import scala.collection.mutable.HashMap
import scala.collection.mutable.HashMap
scala> val a = new HashMap[String, int]
a: scala.collection.mutable.HashMap[java.lang.String,scala.Int] = Map()

scala> a += "Hello" -> 33
line3: scala.Unit = ()

scala> a += "Other" -> 47
line4: scala.Unit = ()

can i define functions that aren't part of an 'object' or 'class'?
'static methods'?
No.  However:
object Utils {
  def foo = "Hello"
}

import Utils._

class Example {
  def bar = foo
}

(new Example).foo // "Hello"


is there a more succinct way to reduce a list, e.g. something that
simply names an operator, like (mylist reduceLeft &&)?
In general, no, but there are special cases such as:
scala> l
line8: scala.List[scala.Int] = List(1,2,3,4)

scala> l.map{+ 2}
line9: scala.List[scala.Int] = List(3,4,5,6)




in pattern matching, can i bind to the composite structure as well as
its elements? in haskell you can do this as:
case blah of
 ls@(x:xs) -> ... -- i can refer to the cons (ls) or its car (x) and cdr (xs)
 [] -> ...
scala> l match {case Nil => List("Sigh"); case l@(x :: xs) => l}
line11: scala.List[scala.Any] = List(1,2,3,4)


what are the some of the principal reasons for scala's (generally)
lesser performance compared with java, at least according to the
language shootout? not trolling; i'd just like to understand the
strengths and weaknesses of languages that i use.

I can't answer definitively, but I find stuff like the shootout to be not real world.  The performance differences between Scala and Java are marginal at most.  What's 20% between friends?

The performance of my Textile parser radically exceeded my expectations.  Given that I wrote the code in my spare time over Christmas holiday and it can (once the JVM has JITed the code) run circles all around the Python and Ruby implementations, I don't care that if I had written it in ANTLR and hand-tuned the code, I would have been able to squeeze more performance out of it.

When there's a hardcore speed issue I have to deal with, I generally write "C" style Java code (I did this in Integer and got Excel comparable performance out of a Java app running on the same hardware back in the JDK 1.1/1.2 days, but the HotSpot team puked all over the code arguing that it wasn't "Object Oriented".)  I pre-allocate things so there's no memory allocation inside loops.  I figure out how to break up my tasks such that I minimize the use of synchronization and try/catch, etc.  One can write C style Scala and I'm pretty sure that the compiler will yield the same byte-code.

For the rest of my work, I don't care about squeezing the last 20% of performance out of my project.  If I have to pay some overhead for pattern matching and lots of implicit (but compiler verified) casting and implicit creation of "Rich" objects and boxing, I don't really care.  While a 7x (Python) or 100x (Ruby) performance penalty makes a difference to me, a few percent  doesn't.

That's my calculus for what it's worth.
thanks for any answers!

Sure thing.

Thanks,

David
yang
<Prev in Thread] Current Thread [Next in Thread>
Google Custom Search

News | FAQ | advertise