Image Image Image Image Image
Scroll to Top

To Top

Uncategorized

20

Jun
2011

No Comments

In code
Uncategorized

By admin

ScalaCollider – saving presets with json

On 20, Jun 2011 | No Comments | In code, Uncategorized | By admin

Sometimes it is useful to save presets when working with ScalaCollider. In Supercollider this is usually done with

writeArchive

and

readArchive

, which can be called on any object that does not contain open functions. After review several possibilities to do the same in scala, I found that the easiest is to use lift-json for JSON parsing and generation. JSON is a human readable format, which is quite convenient.

The easiest way to save a preset would be using case classes to hold the data, since lift-json can automatically serialize and deserialize them:

import java.io.{FileWriter}
import net.liftweb.json._
import net.liftweb.json.Serialization.{write,read}
import scala.io.Source._

object TestJsonSerialization {

  def main(args: Array[String]) {
    implicit val formats = DefaultFormats

    case class Person(name: String, address: Option[String], num:List[Int])
    case class Container(persons:List[Person])
    val people = Container(List(Person("joe", None, List(1,2,3,4)),
        Person("jack", Some("NY grand central"), List(5,6,7,8))))

    //write values to disk
    val fw = new FileWriter("test.txt")
    fw.write(write(people))
    fw.close()

    //get values back
    val jsonString = fromFile("test.txt").mkString
    println(pretty(render(parse(jsonString))))
    println(read[Container](jsonString))
  }

}

To compile the example with sbt use this project file:

import sbt._

class JSONProj(info: ProjectInfo) extends DefaultProject(info)
{
  val json = "net.liftweb" %% "lift-json" % "2.3"
}

Tags | , , ,

16

Jun
2011

No Comments

In code
Uncategorized

By admin

Scala pattern matching and variables

On 16, Jun 2011 | No Comments | In code, Uncategorized | By admin

Today I was debugging some scala code for more than one hour. Turns out that if you use a variable in the pattern you are matching you need to put it in back ticks:

val thing = 3
4 match {
    case thing => println("bingo")
}

This code will match because thing will just became a new variable that is a container for whatever values are being matched.

val thing = 3
4 match {
    case `thing` => println("bingo")
    case _ => println("no match")
}

This code on the other hand will not match, because the variable thing is used, whose content is 3.

Tags | ,

14

Jun
2011

No Comments

In Uncategorized

By admin

Bwana – Graphical man pages in osx

On 14, Jun 2011 | No Comments | In Uncategorized | By admin

I’ve been using Bwana to show me man pages in safari. Reading man pages in terminal is just ridiculous in 2011…

I use a little script to open the manpage from terminal:

open man:$1

I have saved it to

/usr/local/bin/mman

so I only have to do

mman ln

to get the man page for ln.

14

Jun
2011

No Comments

In Uncategorized

By admin

DiffMerge

On 14, Jun 2011 | No Comments | In Uncategorized | By admin

Recently I had to do a bit of git conflict management when merging several branches of supercollider’s repository. I’ve found that FileMerge, the built in tool of OSX just didn’t cut it. I discovered the DiffMerge graphical merge tool which is really incredible and it’s free. To use it with git just download it from the website and install and then do:

git config --global diff.tool diffmerge
git config --global difftool.diffmerge.cmd "diffmerge "$LOCAL" "$REMOTE""

git config --global merge.tool diffmerge
git config --global mergetool.diffmerge.cmd "diffmerge --merge --result="$MERGED"
  "$LOCAL" "$BASE" "$REMOTE""
git config --global mergetool.diffmerge.trustexitcode false

Then when you merge and get conflicts you can do:

git mergetool -t diffmerge

It will ask you to hit enter for each conflict, then a window with 3 panes comes up. You use the center pane to edit code, but you can select code from the left (the branch where are you are merging to) or from the right (the branch you’re merging from). The center pane contains the newest commit which is common to both branches. While editing code it continuously display the differences to the right and left pane, which is great. When you are done, just hit save and close the application, which will drop back in the bash with git running.

and you can also do a visual diff with

git difftool -t diffmerge master~1 myfile.sc