- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
Will parallelability be inherited?
Wed, 2011-04-13, 22:41
Hi,
Curious if there's anything special we'll need to do when we make a
custom collection subtype in 2.9 to get .par to work on those
subtypes. For example I have 22 Seq subtypes lined up for the next
release of ScalaTest. They are code-generated so it is hard to point
to them in SVN. Here's one of them:
class TableFor2[A, B](val heading: (String, String), rows: (A, B)*)
extends IndexedSeq[(A, B)] with IndexedSeqLike[(A, B), TableFor2[A,
B]] {
/**
* Selects a row of data by its index.
*/
def apply(idx: Int): (A, B) = rows(idx)
/**
* The number of rows of data in the table. (This does not include
the
heading
tuple)
*/
def length: Int = rows.length
/**
* Creates a new Builder
for TableFor2
s.
*/
override protected[this] def newBuilder: Builder[(A, B), TableFor2[A, B]] =
new ArrayBuffer mapResult { (buf: Seq[(A, B)]) =>
new TableFor2(heading, buf: _*)
}
/**
* Applies the passed property check function to each row of this
TableFor2
.
*
*
* If the property checks for all rows succeed (the property check
function returns normally when passed
* the data for each row), this apply
method returns
normally. If the property check function
* completes abruptly with an exception for any row, this
apply
method wraps that exception
* in a TableDrivenPropertyCheckFailedException
and
completes abruptly with that exception. Once
* the property check function throws an exception for a row, this
apply
method will complete
* abruptly immediately and subsequent rows will not be checked
against the function.
*
TableFor2
*/
def apply(fun: (A, B) => Unit) {
for (((a, b), idx) <- rows.zipWithIndex) {
try {
fun(a, b)
}
catch {
case e =>
val (aName, bName) = heading
throw new TableDrivenPropertyCheckFailedException(
FailureMessages("propertyException",
UnquotedString(e.getClass.getSimpleName)) + "\n" +
" " + FailureMessages("thrownExceptionsMessage", if
(e.getMessage == null) "None" else UnquotedString(e.getMessage)) +
"\n" +
(
e match {
case sd: StackDepth if
sd.failedCodeFileNameAndLineNumberString.isDefined =>
" " + FailureMessages("thrownExceptionsLocation",
UnquotedString(sd.failedCodeFileNameAndLineNumberString.get)) + "\n"
case _ => ""
}
) +
" " + FailureMessages("occurredAtRow", idx) + "\n" +
" " + aName + " = " + a + "," + "\n" +
" " + bName + " = " + b + "\n" +
" )",
Some(e),
getStackDepthForPropCheck("TableDrivenPropertyChecks.scala",
"forAll"),
FailureMessages("undecoratedPropertyCheckFailureMessage"),
List(a, b),
List(aName, bName),
idx
)
}
}
}
/**
* A string representation of this object, which includes the
heading strings as well as the rows of data.
*/
override def toString: String = stringPrefix + "(" +
heading.toString + ", " + rows.mkString(", ") + ")"
}
I think what should happen when someone invokes .par on this that the
result should run in parallel methods like map, etc., that you'd
expect to run in parallel. My apply method that does the property
check stopping on the first failure would continue to run
sequentially, which is what i want, because I don't invoke .par on
rows. I'm I correct in my surmise that I won't need to do anything
else to get .par to work on TableFor2?
Thanks.
Bill
----
Bill Venners
Artima, Inc.
http://www.artima.com