DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Scala Development on Linux


vim + repl

This is the most light-weight solution which only scala and vim needed.

First write your Scala script "NewList.scala":

sealed trait List[+A]
case object Nil extends List[Nothing]
case class Cons[+A](head: A, tail: List[A]) extends List[A]

object List {
  def sum(ints: List[Int]): Int = ints match {
    case Nil => 0
    case Cons(x,xs) => x + sum(xs)
  }
  def apply[A](as: A*): List[A] =
    if (as.isEmpty) Nil
    else Cons(as.head, apply(as.tail: _*))
  // added by me
  def test() = {
    println(sum(List(3,20,1,2)))
  }
}

Note there can not be package definition in the script, or :load will fail.

Then start a REPL:

scala
scala> :load NewList.scala
scala> List.test

After modification in NewList.scala, run :load ... and List.test again.

Use tmux to make this process smooth

Create an executable shell script ~/.tmux/myscripts/refresh-scala-repl.sh:

#!/bin/bash
tmux send-keys -t .1 ':load NewList.scala'
tmux send-keys -t .1 Enter
tmux send-keys -t .1 'List.test'
tmux send-keys -t .1 Enter

Here ".1" means "send keys to the 1st pane of the current session, current window". The full format is #{session_name}:#{window_index}.#{pane_index}. So if your editor pane is above (or on the left side of) Scala REPL pane, you should use ".2" instead of ".1" in above script.

In vim, run :autocmd BufWritePost *.scala silent !/home/leo/.tmux/myscripts/refresh-scala-repl.sh. Now every time you save the Scala script, :load and List.test will be run automatically in Scala REPL.

Here "silent" will prevent vim from redrawing the screen.

Sometimes you have to use scalac & scala to run your codes like this:

sealed trait MyOption[+A] {
  def mymap[B](f: A => B): MyOption[B] = this match {
    case MyNone => MyNone
    case MySome(a) => MySome(f(a))
  }
}

case class MySome[+A](get: A) extends MyOption[A]
case object MyNone extends MyOption[Nothing]

object MyOption {
}
object Main extends App{
  assert(Some(3).mymap(_ * 2) == Some(6))
}

The corresponding autocmd is rerun-option.sh:

#!/bin/bash
tmux send-keys -t .2 'scalac MyOption.scala && scala Main'
tmux send-keys -t .2 Enter

Ref:

http://stackoverflow.com/questions/19313807/tmux-send-keys-syntax

Autocommands in "Learn Vimscript the Hard Way"

http://vi.stackexchange.com/questions/3060/suppress-output-from-a-vim-autocomand

Use ScalaIDE

ScalaIDE is based on Eclipse, which contains Scala compiler and runtime as a plugin. So you needn't install Scala any more.

It can check syntax error and code completion on the fly. But you have to create a Scala project, add your script in it.

In above example, after modifying codes in editor, use Ctrl-A then Ctrl-Shift-X (shortcut of toolbar button "Run Selection in Scala Interpreter", can be modified in Window -> Preferences -> General -> Keys: Send Selection to Scala Interpreter) to reload script (like :load in REPL). Then focus Scala Interpreter with mouse, and use Ctrl-Up then Ctrl-Enter to run List.test.

sbt + vim

  1. Download sbt package (sbt-0.13.5.deb for Mint 17 64bit) or use PPA and apt update from sbt website;

  2. Install it, then start sbt repl with sbt in shell; This will install sbt (sbt shell in folder /usr/share/sbt-launcher-packaging, all jars needed in folder ~/.ivy2/cache/org.scala-xxx ).

Now you can run scala repl with sbt console in shell, or run console within sbt repl.

Script REPL

Create a file "Hi.scala":

object Hi extends App {
    println("Hi, there")
}

Then run this script within sbt:

$ sbt
...
> run
...
[info] Running Hi
Hi, there

You can use tilde prefix to detect source change automatically, just use "~run" instead of "run" in above example.



Published

Aug 7, 2014

Last Updated

Aug 7, 2014

Category

Tech

Tags

  • hook 1
  • linux 158
  • save 2
  • scala 20
  • vim 92

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor