1

So I'm having trouble importing a package in scala. I downloaded the package, breeze, from github, because I wanted to sample from probability distributions.

I'm used to Python, where I can just download a package, include it in the path, and then import it in the code. So I'm very new to the idea of using a separate "build tool" to use 3rd party packages.

So I downloaded the "breeze" source code from github, installed sbt, and then within the source code for breeze, I ran sbt, and then I used the "assembly" command to get a .jar for breeze.

If I want to use the scala interpreter, I can import the package just fine with

   scala -cp breeze-master/target/scala-2.11/breeze-parent-assembly-0.8.jar

The problem is that I want to use this package in a separate piece of code that I'm writing in a file called Chromosome.scala. And when I try to import the package (as seen below), I get an error:

    error: not found: object breeze

Here's my code:

// Chromosome.scala

import breeze.stats.distributions._

class Chromosome(s:Int, bitstring:Array[Boolean]) {
  val size:Int = s;
  val dna:Array[Boolean] = bitstring;
  var fitness:Int = 0;

  def mutate(prob:Float):Unit = {
    // This method will randomly "mutate" the dna sequence by flipping a bit.
    // Any individual bit may be flipped with probability 'pm', usually small.

    val pm:Float = prob;

    // The variable bern is an instance of a Bernoulli random variable,
    // whose probability parameter is equal to 'pm'.
    var bern = new Bernoulli(pm);

    //Loop through the 'dna' array and flip each bit with probability pm.
    for (i <- 0 to (size - 1)) {
      var flip = bern.draw();
      if (flip) {
        dna(i) = !(dna(i));
      }
    }
  }
Steve Schmitt
  • 3,124
  • 12
  • 14
sinwav
  • 724
  • 1
  • 7
  • 20
  • 1
    Please include code for your script and build. It sounds like there might be a number of misunderstandings here, but it's hard to tell without specifics. – Aaron Novstrup May 27 '14 at 00:21
  • Thanks for the reply - just edited the post to be more specific and include some code. – sinwav May 27 '14 at 03:54

2 Answers2

1

“A script?” What is this and what is its connection to your SBT project? Scala scripts include their own launch command for the Scala interpreter / compiler ( / REPL…). If you want to access things beyond the standard library, you'll have to inclulde them there. Alternately, you can use the SBT Start Script plug-in to produce a launcher script that will include the project dependencies. It will only work locally, though you can write some text processing and other shell scripting to produce a portable launch bundle.

Randall Schulz
  • 26,420
  • 4
  • 61
  • 81
0

It looks like there's some understandable confusion about what sbt is supposed to do for you.

First off, you generally don't need to download a package from github and build it from source. In the rare cases that you do (such as when you require features that have not made it into a release of the library), sbt can handle the grunt work.

Instead, you tell sbt a little about the project you're building (including what its dependencies are), and sbt will download them, compile your code, and set up the runtime classpath for the scala interpreter (amongst myriad other build-related tasks).

Just follow the directions on the breeze wiki. Specifically, create a build.sbt file in your project's root folder and copy this into it:

libraryDependencies  ++= Seq(
            // other dependencies here
            "org.scalanlp" % "breeze_2.10" % "0.7",
            // native libraries are not included by default. add this if you want them (as of 0.7)
            // native libraries greatly improve performance, but increase jar sizes.
            "org.scalanlp" % "breeze-natives_2.10" % "0.7",
)

resolvers ++= Seq(
            // other resolvers here
            // if you want to use snapshot builds (currently 0.8-SNAPSHOT), use this.
            "Sonatype Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots/",
            "Sonatype Releases" at "https://oss.sonatype.org/content/repositories/releases/"
)

// Scala 2.9.2 is still supported for 0.2.1, but is dropped afterwards.
// Don't use an earlier version of 2.10, you will probably get weird compiler crashes.
scalaVersion := "2.10.3"

Put your source into the appropriate folder (by default, src/main/scala) and run sbt console. This command will download the dependencies, compile your code, and launch the Scala interpreter. At this point you should be able to interact with your class.

Aaron Novstrup
  • 20,967
  • 7
  • 70
  • 108