Image Image Image Image Image
Scroll to Top

To Top

2011 June

27

Jun
2011

One Comment

In Uncategorized

By admin

lift-json serialization pretty printing

On 27, Jun 2011 | One Comment | In Uncategorized | By admin

The built-in serialization of lift-json prints a string with no newlines. I prefer to have a nicely printed string so that I can edit the file if I want. For this end I created a new function

writePretty

:

object JsonPimps {

  def writePretty[A <: AnyRef](a: A)(implicit formats: Formats): String =
    (writePretty(a, new StringWriter)(formats)).toString

  def writePretty[A <: AnyRef, W <: Writer](a: A, out: W)(implicit formats: Formats): W =
    Printer.pretty(render(Extraction.decompose(a)(formats)), out)
}

23

Jun
2011

No Comments

In code

By admin

scala – iterate through elements of collection with their indexes

On 23, Jun 2011 | No Comments | In code | By admin

I finally discovered a nice syntax for iterating through a collection with the indices of each element provided:

List(9,9,9,9,9,9).zipWithIndex
res0: List[(Int, Int)] = List((9,0), (9,1), (9,2), (9,3), (9,4), (9,5))

21

Jun
2011

No Comments

In Uncategorized

By admin

Intellij – git, visual interactive rebasing, local history and SuperCollider

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

Wow, I’ve just discovered that the git integration in Intellij IDEA is quite advanced. It even does visual interactive rebasing, something which I couldn’t find in any of the shiny looking (and non-free) osx git visual apps.

It also automatically saves your files on every change and kees a private list of changes for the last couple of minutes/hours, which is basically a capable undo system for programmers.

Finally, today I’ve discovered that Intellij is actually a capable editor for SuperCollider classes. I use it to edit classes, but still rely on the SuperCollider app to run the actual code. I’ve made a basic xml file for supercollider syntax support.

I get for free the nice font rendering of plain text, all the git + local history functionality, tabbed editing, and folder layout with all my files. Also, it automatically strips whitespace, and I can position the cursor anywhere in the file and start typing

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 | ,

15

Jun
2011

No Comments

In code

By admin

scala compiled to LLVM and Scala.react

On 15, Jun 2011 | No Comments | In code | By admin

Today found a paper submitted at scala days 2011 on “Compiling Scala to LLVM” . That would probably give native speed to scala programs.  I will keep following the development of this.

Also interesting is the paper on “Scala.react: Embedded Reactive Programming in Scala” , also from scala days 2011.

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