scala on stackoverflow

Syndicate content
most recent 30 from stackoverflow.com 2012-02-19T09:00:32Z
Updated: 2 hours 32 min ago

How can I add jars from more than one unmanaged directory in an SBT .scala project configuration

18 February 2012 - 7:41pm

I'm trying to get SBT to build a project that could have more than one unmanaged directory. If I had a single directory, I could easily do it like this:

unmanagedBase := file( "custom-libs" ).getAbsoluteFile

But since I have two directories with unmanaged jars, I need to be able to add them all. I have found some information in here, but still doesn't seem useful for my full .scala file build.

Here is what I have been able to do so far, but still doesn't work, I believe the issue is that I'm not correctly overriding the unmanagedJars task, any help is appreciated. Below is the code I have so far:

import sbt._ import Keys._ object Build extends sbt.Build { val unmanagedJars = TaskKey[Keys.Classpath]("unmanaged-jars") unmanagedJars := { Configuration.listUnmanaged( file(".").getAbsoluteFile ) } lazy val jobsProject = Project( "some-company-jobs", file(".").getAbsoluteFile ) .settings( organization := "com.some-company", version := "1.0.0", scalaVersion := "2.9.1", scalacOptions := Seq("-deprecation", "-encoding", "utf8"), resolvers ++= Configuration.resolutionRepos, libraryDependencies ++= Configuration.dependencies, unmanagedBase := file( "custom-libs" ).getAbsoluteFile ) } object Configuration { def listUnmanaged( base : RichFile ) : Keys.Classpath = { val baseDirectories = (base / "custom_libs") +++ ( base / "executables" ) (baseDirectories ** "*.jar").classpath } object Repos { val SonatypeSnapshots = ("sonatype-snapshots" at "http://oss.sonatype.org/content/repositories/snapshots") val SonatypeReleases = "sonatype-releases" at "http://oss.sonatype.org/content/repositories/releases" val SpringSource = "spring-source" at "http://repository.springsource.com/ivy/bundles/release" val SpringExternal = "spring-external" at "http://repository.springsource.com/maven/bundles/external" } val resolutionRepos = Seq[Resolver]( ScalaToolsReleases, ScalaToolsSnapshots, Repos.SonatypeReleases, Repos.SonatypeSnapshots, Repos.SpringExternal, Repos.SpringSource ) val dependencies = Seq( "commons-lang" % "commons-lang" % "2.6", "commons-cli" % "commons-cli" % "1.2", "commons-pool" % "commons-pool" % "1.5.6", ) }

Scala : Can there be covariance in a constructor for defined type?

18 February 2012 - 4:53pm

I have the class Variable[X <: SeqVal[_]](initialState:Calc[X])

which I instantiate with new Variable[SeqVal[Float]](Max()) where Max is

case class Max(seq: Int = 0, value: Float = .0f) extends SeqVal[Float] with Calc[SeqVal[Float]], and there are other case classes other than Max.

This does not compile even though Max does implement a variant of the trait Calc[SeqVal[_]].

[error] ../Variable.scala:14: type mismatch; [error] found : com.quasiquant.calc.Max [error] required: com.quasiquant.calc.Calc[com.quasiquant.calc.Price] [error] Note: com.quasiquant.messages.SeqVal[Float] >: com.quasiquant.calc.Price (and com.quasiquant.calc.Max <: com.quasiquant.calc.Calc[com.quasiquant.messages.SeqVal[Float]]), but trait Calc is invariant in type X. [error] You may wish to define X as -X instead. (SLS 4.5) [error] extends Variable[Price](Max(), initChildren)

I need help in trying to work out how I can change the bounding of initialState:Calc[X] so the initialState can be set to anything that implements Calc[X] (not just Max). I would prefer it if i didn't have to add a second type parameter to all the instantiations of Variable

Why doesn't Scala have an IO Monad?

18 February 2012 - 3:05pm

I'm wondering why Scala does not have an IO Monad like Haskell.

So, in Scala the return type of method readLine is String whereas in Haskell the comparable function getLine has the return type IO String.

There is a similar question about this topic, but its answer it not satisfying:

Using IO is certainly not the dominant style in scala.

Can someone explain this a bit further? What was the design decision for not including IO Monads to Scala?

Why doesn't Scala has an IO Monad?

18 February 2012 - 3:05pm

I'm wondering why Scala does not have an IO Monad like Haskell.

So, in Scala the return type of method readLine is String whereas in Haskell the comparable function getLine has the return type IO String.

There is a similar question about this topic, but its answer it not satisfying:

Using IO is certainly not the dominant style in scala.

Can someone explain this a bit further? What was the design decision for not including IO Monads to Scala?

Scala type inference fails to note that these types are identical, whatever they are

18 February 2012 - 10:15am

I have a design pattern here where there is an object generator (MorselGenerator and its children), any instance of which always generates the same exact type of object (Morsels and its children), but the type checker will not let me perform any operations on two or more of these generated objects, believing they might be different.

How do I get this past the type checker?

trait Morsel { type M <: Morsel def calories : Float def + (v : M) : M } trait MorselGenerator { type Mg <: Morsel def generateMorsel : Mg } class HotDog(c : Float, l : Float, w : Float) extends Morsel { type M = HotDog val calories : Float = c val length : Float = l val width : Float = w def + (v : HotDog) : HotDog = new HotDog(v.calories + calories, v.length + length, v.width + width) } class HotDogGenerator extends MorselGenerator { type Mg = HotDog def generateMorsel : HotDog = new HotDog(500.0f, 3.14159f, 445.1f) } object Factory { def main ( args : Array[String] ) { val hdGen = new HotDogGenerator() println(eatTwo(hdGen)) } def eatTwo ( mGen : MorselGenerator ) { val v0 : mGen.Mg = mGen.generateMorsel val v1 : mGen.Mg = mGen.generateMorsel v0 + v1 /// ERROR HERE } }

Compiler generates the following compile error

Generator.scala:43: error: type mismatch; found : v1.type (with underlying type mGen.Mg) required: v0.M v0 + v1 /// ERROR HERE ^ one error found


Update

Here is C++ code that is more or less equivalent to what I'm trying to do. Note that the eatTwo function is fully polymorphic and makes no reference to specific derived types of Morsel or MorselGenerator.

#include <stdlib.h> #include <stdio.h> template <class M> class Morsel { public: Morsel(float c) : calories(c) {} float calories; virtual M operator + (const M& rhs) const = 0; }; template <class M> class MorselGenerator { public: virtual M * generateMorsel() const = 0; }; class HotDog : public Morsel<HotDog> { public: HotDog(float c, float l, float w) : Morsel<HotDog>(c), length(l), width(w) {} float length, width; HotDog operator + (const HotDog& rhs) const { return HotDog(calories+rhs.calories, length+rhs.length, width+rhs.width); } }; class HotDogGenerator : public MorselGenerator<HotDog> { HotDog * generateMorsel() const { return new HotDog(500.0f, 3.14159f, 445.1f); } }; /////////////////////////////////////////////// template <class MorselType> float eatTwo ( const MorselGenerator<MorselType>& mGen) { MorselType * m0 = mGen.generateMorsel(); MorselType * m1 = mGen.generateMorsel(); float sum = ((*m0) + (*m1)).calories; delete m0; delete m1; return sum; } int main() { MorselGenerator<HotDog> * morselStream = new HotDogGenerator(); printf("Calories Ingested: %.2f\n", eatTwo(*morselStream)); delete morselStream; }

Programming Pattern Comparison?

18 February 2012 - 8:31am

Is there a book or a website where is explained how to solve some example problem X in OOP style and then is given and explained an opposed functional programming style solution? Perhaps one more example which solves the problem with the best of both worlds?

This would help me translate my pattern knowledge to functional programming and learn this stuff better.

The best would be if java/scala is used for the examples.

How to use an autoincrement index in for comprehension in Scala

18 February 2012 - 8:02am

Is it possible to use an autoincrement counter in for comprehensions in Scala?

something like

for (element <- elements; val counter = counter+1) yield NewElement(element, counter)

Lift Mapper - trait with OneToMany mapping

18 February 2012 - 3:04am

Fairly new to lift and I am trying to create a model for my application. Since I want to keep things in the spirit of DRY I want to use trait mixins to specify some of the fields in my model. For instance, I have a Person trait which I mixin to my Employee class:

trait Person[T <: Mapper[T]] { self: T => object firstName extends MappedString[T](this, 50) object lastName extends MappedString[T](this, 50) object civicRegNumber extends MappedString[T](this, 12) } class Employee extends LongKeyedMapper[Employee] with IdPK with Person[Employee] { def getSingleton = Employee object user extends MappedLongForeignKey(this, User) } object Employee extends Employee with LongKeyedMetaMapper[Employee]

This seems to work fine but now I want to add a one to many relation from my Person trait to a PersonContactInfo trait which would have a few fields such as email etc and which would have a corresponding EmployeeContactInfo. I am getting a bunch of compilation errors and I'm a little stumped as to how to proceed. This is what I have right now (does not compile):

trait ContactInfo[T <: Mapper[T]] { self: T => object email extends MappedEmail[T](this, 80) } trait PersonContactInfo[T <: Mapper[T]] extends ContactInfo[T] with OneToMany[Long, T] { self: T => object person extends LongMappedMapper(this, getSingleton) } class EmployeeContactInfo extends LongKeyedMapper[EmployeeContactInfo] with IdPK with PersonContactInfo[EmployeeContactInfo] { def getSingleton = EmployeeContactInfo } object EmployeeContactInfo extends PersonContactInfo[EmployeeContactInfo] with LongKeyedMetaMapper[EmployeeContactInfo]

Any hints as to how to make this work is appreciated! Or if someone has already tried this and failed please tell me that also! :)

Scala create a sparse vector

18 February 2012 - 1:35am

This is the functionality i am trying to achieve in scala
create a list of some numbers .. say (1, 2 ,3 , 4, 5) // this represents 1 document and its features
There will be n such lists with different features.

I want to put this n lists into a matrix. So that later down the line, if I want to do operations on this matrix like matrix transpose, matrix inverse i can do it easily.

Currently I do have the lists ready, but i am not sure how to use the sparseVector and Encoder function of scala as the number of rows for this matrix would be huge (approx 1 million) and columns would be 200000. So performance is also an issue

What does the keyword 'implicit' mean when it's placed in front of lambda expression parameter?

18 February 2012 - 1:05am

I have seen this kind of code many times before, most recently at scala-user mailing list:

context(GUI) { implicit ec => // some code }

context is defined as:

def context[T](ec: ExecutionContext)(block: ExecutionContext => T): Unit = { ec execute { block(ec) } }

What purpose does the keeyword implicit achieve when placed in front of a lambda expression parameter?

Effective Scala [closed]

17 February 2012 - 6:46pm

I have just started to learn Scala. I was looking for something in line with Effective Java for Scala.

What are your thoughts, opinions, tips on writing Effective Scala Code?

reading from binary file Scala

17 February 2012 - 11:59am

how to read a binary file in chunks in scala.

This was what I was trying to do

val fileInput = new FileInputStream("tokens") val dis = new DataInputStream(fileInput) var value = dis.readInt() var i=0; println(value)

the value which is printed is a huge number. Whereas it should return 1 as the first output

Lift application performance degrade

17 February 2012 - 11:36am

I am using lift framework with embedded jetty. My application is running pretty fast if I am it in development. As soon as I make a assembly of it using SBT, the performance degrades upto 20-30 times. The request which was taking 400ms, starts taking 10sec. Does lift has something to do with assembly?

Please give me some pointers to solve this problem.

Thanks, Puneet

IntelliJ, Akka and Configuration files

17 February 2012 - 9:57am

When using akka, I place akka.conf in src/main/resources. When I run through sbt, the akka.conf is correctly recognized. But not when I run through IntelliJ (even after a gen-idea). What is the appropriate way to accomplish this?

Numerical library for Scala

17 February 2012 - 9:18am

I'm looking for a library to do numerical computing in Scala (or Java, although something that can use scala functions would be way nicer!) with at least the following capabilities:

  • L-BFGS
  • Minimizers (Powell, QuasiNewton, ...)
  • Numerical differentiation of multivariable functions
  • Numerical integration (not strictly necessary but highly preferred)

I'm also only looking for something that's actively maintained (last update during 2011 at the earliest), preferably but not necessarily free. Also, numerical stability is required, aka all operations should be implemented in a way that gives consistent results where precision is preserved as much as possible.

I'm already aware of IMSL, but would prefer something else.

Thanks in advance

Scala SBT: scala.tools.nsc isn't running

17 February 2012 - 6:25am

I have a problem with scala.tools.sbt

scala file

Here I used parser functionality to make abstract syntax tree of code 2 + 3

import scala.tools.nsc._ object Main extends App { var i = new Interpreter println(i.parse("2 + 3")) }

SBT configuration

name := "scalaSample" version := "1.0-SNAPSHOT" scalaVersion := "2.9.1" libraryDependencies += "org.scalatest" %% "scalatest" % "1.7.1" % "test" libraryDependencies += "org.scala-lang" % "scala-compiler" % "2.9.1"

Error

Failed to initialize compiler: object scala not found. ** Note that as of 2.8 scala does not assume use of the java classpath. ** For the old behavior pass -usejavacp to scala, or if using a Settings ** object programatically, settings.usejavacp.value = true.

[error] (run-main) java.lang.NullPointerException java.lang.NullPointerException at scala.tools.nsc.CompilationUnits$CompilationUnit. (CompilationUnits.scala:16) at scala.tools.nsc.interpreter.ExprTyper$codeParser$.applyRule(ExprTyper.scala:22) at scala.tools.nsc.interpreter.ExprTyper$codeParser$.stmts(ExprTyper.scala:36) at scala.tools.nsc.interpreter.ExprTyper$$anonfun$parse$2.apply(ExprTyper.scala:47) at scala.tools.nsc.interpreter.ExprTyper$$anonfun$parse$2.apply(ExprTyper.scala:46) at scala.tools.nsc.reporters.Reporter.withIncompleteHandler(Reporter.scala:46) at scala.tools.nsc.interpreter.ExprTyper$class.parse(ExprTyper.scala:46) at scala.tools.nsc.interpreter.IMain$exprTyper$.parse(IMain.scala:1012) at scala.tools.nsc.interpreter.IMain.parse(IMain.scala:1013) at eu.semantiq.scalaToJS.Main$delayedInit$body.apply(Main.scala:7) at scala.Function0$class.apply$mcV$sp(Function0.scala:34) at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12) at scala.App$$anonfun$main$1.apply(App.scala:60) at scala.App$$anonfun$main$1.apply(App.scala:60) at scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59) at scala.collection.immutable.List.foreach(List.scala:45) at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:30) at scala.App$class.main(App.scala:60) at eu.semantiq.scalaToJS.Main$.main(Main.scala:5) at eu.semantiq.scalaToJS.Main.main(Main.scala) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:616) java.lang.RuntimeException: Nonzero exit code: 1 at scala.sys.package$.error(package.scala:27)

In scala REPL everything works

Welcome to Scala version 2.9.0.1 (OpenJDK 64-Bit Server VM, Java 1.6.0_23). Type in expressions to have them evaluated. Type :help for more information.

scala> import scala.tools.nsc._

import scala.tools.nsc._

scala> var i = new Interpreter

warning: there were 4 deprecation warnings; re-run with -deprecation for details warning: there were 1 deprecation warnings; re-run with -deprecation for details

i: scala.tools.nsc.Interpreter = scala.tools.nsc.Interpreter@786bfd73

scala> println(i.parse("2 + 3"))

Some(List(2.$plus(3)))

I feel really sorry for my bad English

Scala, extending the iterator

17 February 2012 - 6:21am

Im looking to extended the iterator to create a new method takeWhileInclusive, which will return operate like takeWhile but include the last element.

My issue is what is best practice to extend the iterator to return a new iterator which I would like to be lazy evaluated. Coming from a C# background I normal use IEnumerable and use the yield keyword, but such an option doesn't appear to exist in Scala.

for example I could have

List(0,1,2,3,4,5,6,7).iterator.map(complex time consuming algortithm).takeWhileInclusive(_ < 6)

so in this case the takeWhileInclusive would only have resolve the predicate on the values until I get the a result greater than 6, and it will include this first result

so far I have:

object ImplicitIterator { implicit def extendIterator(i : Iterator[Any]) = new IteratorExtension(i) } class IteratorExtension[T <: Any](i : Iterator[T]) { def takeWhileInclusive(predicate:(T) => Boolean) = ? }

Scala Lift - Save file from post data (valums uploader)

17 February 2012 - 5:10am

I have the following object

object Upload_Dispatch extends RestHelper { serve { /* Presentation ---------------------------------------------*/ case "upload_presentation" :: Nil Post req => { println(req.body.map(_.length)) JsonResponse(JObject(JField("success", JBool(true)) :: Nil)) } } }

This comes from here:

https://github.com/timperrett/lift-file-uploader

The valums uploader script is here:

http://valums.com/ajax-upload/

I've got the whole thing working ok with the response returned etc, how do I actually save the files that are being sent? How do I access the actual byte array so I can use FileOutputStream?

Thanks in advance, any help much appreciated :)

What is the performance impact of using the type class pattern in Scala

17 February 2012 - 3:31am

I'm currently making extensive use of the type class pattern in to be performance-relevant portions of my code. I made out at least two potential sources of inefficiency.

  1. The implicit parameters get passed along message calls. I don't know whether this really happens. Maybe scalac can simply insert the implicit parameters where they are used and remove them from the method signature. This is probably not possible in cases where you insert the implicit parameters manually, since they might be resolved at runtime only. What optimizations do apply concerning passing implicit parameters?

  2. If the type class instance is provided by a def (contrary to a val), the object has to be recreated on every invocation of a "type classed method". This issue may be adressed by the JVM, which might optimize object creation away. This issue might also be adressed by scalac by reusing these objects. What optimizations do apply concerning the creation of implicit parameter objects?

And of course there might be additional sources of inefficiency when applying the type class pattern. Please tell me about them.

Implicit conversion with implicit parameter

17 February 2012 - 2:49am

I'm implementing a Java interface with a lot of methods with Object parameters, which in my case are really Strings containing user names:

public interface TwoFactorAuthProvider { boolean requiresTwoFactorAuth(Object principal); ... //many more methods with the same kind of parameter }

I'm trying to use implicit conversion to convert these to User objects in my implementation:

class TwoFactorAuthProviderImpl(userRepository: UserRepository) extends TwoFactorAuthProvider { def requiresTwoFactorAuth(user: User): Boolean = { ... } }

When I define the conversion in the companion object of my class, it is picked up just fine and my class compiles:

object TwoFactorAuthProviderImpl { implicit def toUser(principal: Any): User = { null //TODO: do something useful } }

However, to be able to do the conversion, I need access to the user repository, which the TwoFactorAuthProviderImpl instance has, but the companion object does not. I thought I could possibly use an implicit parameter to pass it:

implicit def toUser(principal: Any)(implicit repo: UserRepository): User = { val email = principal.asInstanceOf[String] repo.findByEmail(email) }

But with the implicit parameter, the conversion is no longer picked up by the compiler (complaining that I'm not implementing the interface).

Is there a way to get the implicit conversion that I want, or is this outside the scope of what you can do with implicits?