<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description></description><title>la.scala._</title><generator>Tumblr (3.0; @scala-la)</generator><link>http://la.scala.la/</link><item><title>capWord Competition</title><description>&lt;p&gt;This competition was run on #scala.  The task is to write a short &lt;code&gt;def capWord&lt;/code&gt; which checks whether a given string is a Capital word.  Some entries follow.&lt;/p&gt;

&lt;p&gt;@paulp I:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def capWord(s: String) = (s.tail map (_.isLowerCase))
.reduceLeft(_ &amp;&amp; _) &amp;&amp; s.head.isUpperCase
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— and he notes: of course that fails on “P”.  but maybe that’s not a word either.  more of a letter.&lt;/p&gt;

&lt;p&gt;@paulp II:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def capWord(s: String) = !s.isEmpty &amp;&amp; 
(s.head isUpperCase) &amp;&amp; (s.tail forall (_.isLowerCase))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— and he notes: I won’t deal with null out of spite.&lt;/p&gt;

&lt;p&gt;@RShulz:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def capWord(word: String): Boolean = 
{ word.count(_.isUpperCase) == 1 &amp;&amp; word(0).isUpperCase }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— and he notes: (Count exists in my mind.)&lt;/p&gt;

&lt;p&gt;@dcsobral I, the winning entry (no head or tail, and I like it):&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def capWord(w: String) = w.toList match {
  case h :: t if ('A' to 'Z' contains h) &amp;&amp; 
    (t forall ('a' to 'z' contains _)) =&gt; true
  case _ =&gt; false
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;@dcsobral II, the fold entry:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def capWord(w: String) = w.tail.foldLeft(w.head isUpperCase)
( (flag, l) =&gt; flag &amp;&amp; l.isLowerCase )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;@dcsobral III, fanciful entry:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def capWord(w: String) = (
    (
    w
    map (_ isUpperCase)
    map (if (_) 1 else 0)
    zipWithIndex
    )
  map Function.tupled((a,b) =&gt; a * (b+1))
  reduceLeft (_+_)
  ) == 1
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;@SethTisue, mindboggling entry:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def capWord(s: String) = 
(Math.log(s.map(c =&gt; 
    if(c.isUpperCase) 1 else 0).mkString.toDouble) 
/ Math.log(10)).toString.endsWith(".0")
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;@paulp III, the mondblowingboggling entry:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def capWord(s: String) = s.head.isUpperCase &amp;&amp; 
(s.tail map (c =&gt; 
    io.Source.fromURL(new java.net.URL("http://www.simplyscala.com/interp?code='%s'.isLowerCase"
.format(c.toChar.toString))).mkString) 
forall (_ contains "true"))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— and he notes: I could use threads to speed that up a little.&lt;/p&gt;</description><link>http://la.scala.la/post/185653446</link><guid>http://la.scala.la/post/185653446</guid><pubDate>Fri, 11 Sep 2009 19:22:30 -0400</pubDate></item><item><title>Salmon Run and other NLP Toolkits</title><description>&lt;p&gt;There’re several important NLP toolkits in Java and soon Scala, as well as blogs about them.  Here’s some.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://sujitpal.blogspot.com/"&gt;Salmon Run&lt;/a&gt; Excellent soup-to-nuts usage guides to many JVM technologies.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://lingpipe.com/"&gt;LingPipe&lt;/a&gt; We’re using it for N-gram models.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://opennlp.sourceforge.net/"&gt;OpenNLP&lt;/a&gt; My UPenn PhD classmate Tom Morton’s framework.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://github.com/DRMacIver"&gt;DRMacIver&lt;/a&gt; It’s not a project, it’s a person!  Awesome tools.&lt;/p&gt;</description><link>http://la.scala.la/post/185640883</link><guid>http://la.scala.la/post/185640883</guid><pubDate>Fri, 11 Sep 2009 19:02:46 -0400</pubDate></item><item><title>Bzip2</title><description>&lt;p&gt;Needed to open &lt;code&gt;bz2&lt;/code&gt; files in the same way as &lt;code&gt;zip&lt;/code&gt; ones.  Old &lt;code&gt;bzip2&lt;/code&gt; streams for Java have a bug where you have to manually swallow two first characters from the underlying file stream, which are &lt;code&gt;BZ&lt;/code&gt;, before you coudl give it to the &lt;code&gt;bzip2&lt;/code&gt; stream.  The modern wrapper from Apache Commons relieves you from this tedious check.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://commons.apache.org/compress/"&gt;Apache Commons Compress&lt;/a&gt;&lt;/p&gt;</description><link>http://la.scala.la/post/185637234</link><guid>http://la.scala.la/post/185637234</guid><pubDate>Fri, 11 Sep 2009 18:57:12 -0400</pubDate></item><item><title>Value Serialization</title><description>&lt;p&gt;So I needed to serialize a bunch of Scala Maps, and it’s supersimple: just tack a&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@serialize
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;on top of the declaration, be it a class, or just a value!  And then the usual Java serialization applies!&lt;/p&gt;</description><link>http://la.scala.la/post/185633653</link><guid>http://la.scala.la/post/185633653</guid><pubDate>Fri, 11 Sep 2009 18:52:05 -0400</pubDate></item><item><title>Command-line options</title><description>&lt;p&gt;DRMacIver has a super-lovely li’l class, &lt;a href="http://github.com/DRMacIver/optional/tree/master"&gt;optional.Application&lt;/a&gt;, from which you can derive your main object, with parameters for &lt;code&gt;main&lt;/code&gt; being extracted from the cmdline.&lt;/p&gt;</description><link>http://la.scala.la/post/185631299</link><guid>http://la.scala.la/post/185631299</guid><pubDate>Fri, 11 Sep 2009 18:48:23 -0400</pubDate></item><item><title>Graph Storage</title><description>&lt;p&gt;When representing data as a graph, it’s important to get efficient storage and traversing platform for it.  Some good JVM ones I found:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://jgrapht.sourceforge.net/"&gt;JGraphT&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://neo4j.org/"&gt;Neo4j&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Any RDF storage can be used for triplets — edges with a weight/relationship, e.g.&lt;/p&gt;

&lt;p&gt;&lt;a href="http://www.franz.com/agraph/allegrograph/"&gt;AllegroGraph RDF Store&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Other RDF stores, such as the one underlying &lt;a href="http://dbis.informatik.uni-freiburg.de/index.php?project=SP2B/data.php"&gt;DBLP++&lt;/a&gt;.&lt;/p&gt;</description><link>http://la.scala.la/post/185630257</link><guid>http://la.scala.la/post/185630257</guid><pubDate>Fri, 11 Sep 2009 18:46:49 -0400</pubDate></item><item><title>Autovivification, anyone?</title><description>&lt;p&gt;I’m storing a bunch of counts for twits in a map of maps, like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import scala.collection.mutable.{Map=&gt;UMap}

class Repliers {
  type RepCount = UMap[UserID,(TwitCount,TwitCount)]
  var reps: UMap[UserID, RepCount] = UMap.empty

  // TODO here goes our favorite non-autovivification,
  // a question is, whether this can be made into auto-
  def addTwit(twit: Twit): Unit = {
    twit.reply match {
      case None =&gt;
      case Some(reply) =&gt;
        val uid = twit.uid
        val ruid = reply.replyUser
        val tinc = if (reply.replyTwit.isEmpty) 0 else 1
        val u = reps.get(uid)
           u match {
             case Some(repcount) =&gt; repcount.get(ruid) match {
               case Some((numUser,numTwit)) =&gt; {
                 val x = reps(uid)(ruid)
                 reps(uid)(ruid) = (x._1 + 1, x._2 + tinc)
               }
               case _ =&gt; reps(uid)(ruid) = (1, tinc)
             }
             case _ =&gt; reps(uid) = UMap(ruid-&gt;(1, tinc))
           }
    }
  }
// ...
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;How do we auto-vivify the maps, by a kind of more laconic upsert (insert or update)?&lt;/p&gt;</description><link>http://la.scala.la/post/143672947</link><guid>http://la.scala.la/post/143672947</guid><pubDate>Fri, 17 Jul 2009 15:41:37 -0400</pubDate></item><item><title>Using Berkeley DB DPL for Implementations of Abstract Scala Classes</title><description>&lt;p&gt;I have a class which is a spec for storage and may be implemented via PostgreSQL, Berkeley DB, db4o, jdbm, etc.  Annotating it is not possible as it’s already published and backend-agnostic, so, for BDB, we create a mirror class with Java types and fill it from the original Scala class.  This allows to use nullable values instead of Options to simplify indexing, and allows to serialize types Berkeley DB knows about, such as &lt;code&gt;Date&lt;/code&gt;, instead of Joda’s &lt;code&gt;DateTime&lt;/code&gt;.  I have constructors which take the Scala original and fill in the annotated BDB Entity fields, applying conversions such as &lt;code&gt;Long&lt;/code&gt;=&gt;&lt;code&gt;java.lang.Long&lt;/code&gt;, and &lt;code&gt;toScalaStuff&lt;/code&gt; functions to return Scala ones back, with reverse conversions.  Here’s how one class is mirrored in BDB:&lt;/p&gt;

&lt;p&gt;The spec:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;case class ReplyTwit (
  tid: TwitID,
  replyTwit: Option[TwitID],
  replyUser: UserID
  )
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;BDB body:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;@Entity
class ReplyTwitBDB {
  @PrimaryKey
  var tid: java.lang.Long = null
  @SecondaryKey{val relate=MANY_TO_ONE}
  var replyTwit: java.lang.Long = null
  @SecondaryKey{val relate=MANY_TO_ONE}
  var replyUser: java.lang.Integer = null

  def this(
    _tid: TwitID,
    _replyTwit: Option[TwitID],
    _replyUser: UserID
    ) = { this()
      tid = _tid
      replyTwit = _replyTwit match {
        case Some(x) =&gt; x
        case _ =&gt; null
      }
      replyUser = _replyUser
    }
  def this(t: ReplyTwit) = this(t.tid,t.replyTwit,t.replyUser)
  def toReplyTwit: ReplyTwit = {
    val replyTwitLongOpt = replyTwit match {
      case null =&gt; None
      case x =&gt; Some(x.longValue)
    }
    ReplyTwit(tid.longValue,replyTwitLongOpt,replyUser.intValue)
  }

  override def toString:String = {
    var s = "Reply tid:%d ru:%d" format (tid,replyUser)
    if (replyTwit != null) s += " rt:"+replyTwit
    s
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;It may be a bit verbose, but it works, and is used in db-bdb.scala in tfitter.  I might also implement the original BDB Bind API with my own serialization, but then I’d have to manage secondary indices manually as databases.  The DPL also allows me to evolve the class layouts with versioning and narrowing/widening between them.&lt;/p&gt;</description><link>http://la.scala.la/post/143668890</link><guid>http://la.scala.la/post/143668890</guid><pubDate>Fri, 17 Jul 2009 15:33:08 -0400</pubDate></item><item><title>Back from the Concrete into the Abstract</title><description>&lt;p&gt;Once I implemented a PostgreSQL interface for my &lt;code&gt;TwitterDB&lt;/code&gt; abstract class, I did another backend with Berkeley DB, Java Edition.  When doing it, I found that one method is almost identical, so I decided to push it back into the abstract class.  But how’d I do construct backend-specific objects?  One way is to pass them back in as function parameters.  So here’s the result — in the abstract class:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;case class SubParams (
  makeTwit:  TwitID =&gt; TwitDB, 
  makeUser:  UserID =&gt; UserDB,
  txnBegin:     () =&gt; Unit,
  txnCommit:    () =&gt; Unit,
  txnRollback:  () =&gt; Unit
)


// curry make/txn params
def insertUserTwitCurry(subParams: SubParams)(ut: UserTwit)
    : Unit = {
    import System.err

    val SubParams(makeTwit,makeUser,txnBegin,txnCommit,
        txnRollback) = subParams

    val UserTwit(user,twit) = ut
    val uid = user.uid
    val tid = twit.tid
    try {

      val t = makeTwit(tid) // TwitPG(tid)

      txnBegin
      t put twit // will cause exception if present and rollback
      val u = makeUser(uid) // UserPG(uid)
      u.updateUserForTwit(ut)
      txnCommit
    } catch {
      case e =&gt; {
        err.println(e)
        err.println("ROLLBACK uid="+uid+" tid="+tid)
        txnRollback
      }
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I needed to pass a several functions to a method as parameters, and created a class, &lt;code&gt;subParams&lt;/code&gt;, to hold them.  I pass case class constructors, such as &lt;code&gt;TwitPG&lt;/code&gt;, directly as functions there, and it works.&lt;/p&gt;

&lt;p&gt;Interestingly, one can supply the same &lt;code&gt;def blah: Unit&lt;/code&gt; function via call by name into a &lt;code&gt;def&lt;/code&gt;, but have to group together in a case class with &lt;code&gt;()=&gt;Unit&lt;/code&gt;, you can’t have an &lt;code&gt;x: =&gt; Unit&lt;/code&gt; parameters to a case class.&lt;/p&gt;

&lt;p&gt;Here’s how I implement my abstract function in the PostgreSQL and Berkeley DB backends:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// PostgreSQL has no commit begin, passing Unit=&gt;Unit do-nothing 
val subParams = SubParams(TwitPG,UserPG,()=&gt;(),conn.commit _,
    conn.rollback _)
def insertUserTwitB = insertUserTwitCurry(subParams)(_)

// Berkleley DB
val subParams = SubParams(TwitBDB,UserBDB,txnBegin _,
    txnCommit _,txnRollback _)
def insertUserTwitB = insertUserTwitCurry(subParams)(_)
&lt;/code&gt;&lt;/pre&gt;</description><link>http://la.scala.la/post/143657955</link><guid>http://la.scala.la/post/143657955</guid><pubDate>Fri, 17 Jul 2009 15:10:51 -0400</pubDate></item><item><title>sneaking closing actions into an iterator</title><description>&lt;p&gt;I created an iterator to wrap a Berkeley DB cursor.  One thing with iterating through it is closing the cursor when we reach the end of the
iteration.  So I simply stuck the closing into the &lt;code&gt;hasNext&lt;/code&gt; call-through
to the underlying Java iterator!&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  class TwIteratorBDB extends TwIterator {
    val cursor = twitPrimaryIndex.entities
    val javaIter = cursor.iterator
    // TODO a way to stick closing actions into our iterator,
    // are there any official or better ways?
    def hasNext: Boolean = { val has = javaIter.hasNext
      if (!has) cursor.close
      has
    }
    def next: Twit = javaIter.next.toTwit
  }
&lt;/code&gt;&lt;/pre&gt;</description><link>http://la.scala.la/post/143154484</link><guid>http://la.scala.la/post/143154484</guid><pubDate>Thu, 16 Jul 2009 21:32:05 -0400</pubDate></item><item><title>continually, ultimately, takeWhileFinally</title><description>&lt;p&gt;I needed to generate a Stream out of a Berkeley DB cursor.  One thing you need to do when you finish reading from a cursor is to close it.  So we needed to have a Stream with a closing action.  @paulp to the rescue!&lt;/p&gt;

&lt;p&gt;First, we ‘d came up with this cute Stream generator, without cursor closing yet:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  def cursorStream[T](cursor: EntityCursor[T]): Stream[T] =
        continually(cursor.next _) map (_.apply()) 
        takeWhile (_ != null)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Rumor has it you can say just &lt;code&gt;map(_.apply) or even&lt;/code&gt;map(_())`&lt;/p&gt;

&lt;p&gt;&lt;code&gt;continually&lt;/code&gt; is in 2.8 but not in 2.7, so I made it up as
  // paulp has it in scala.util.control.Exception in 2.8,
  // replacing Stream.Cons with Stream.cons works in 2.7:
  def continually[A](elem: =&gt; A): Stream[A] = 
    Stream.cons(elem, continually(elem))&lt;/p&gt;

&lt;p&gt;OK, but now we need to close the cursor when finished with the stream.  @paulp suggested ultimately(…) which he had written together with s.u.c.E in 2.8 as well.  I copied it into 2.7 as&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  // def ultimately[T](body: =&gt; Unit): Catch[T] = 
  //        noCatch andFinally body
  def ultimately[T](fin: =&gt; Unit)(body: =&gt; T): T = 
    try { body } finally { fin }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;So one would have though this might work:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  // WRONG: closes right after head, tail-lazy Stream!
  def cursorStream[T](cursor: EntityCursor[T]): Stream[T] =
    ultimately(cursor.close) { 
      continually(cursor.next _) map (_.apply()) 
        takeWhile (_ != null) force.toStream }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— but it doesn’t, doing &lt;code&gt;fin&lt;/code&gt; right after the first head read.&lt;/p&gt;

&lt;p&gt;@paulp promised 22 other ways to do it otehrwise, e.g.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  // WRONG: error on append (() =&gt; cursor.close), 
  // same wrong Unit type, not T, 
   // for Stream(cursor.close _):
  (continually(cursor.next _) takeWhile (_ != null)) 
    append (() =&gt; cursor.close) map (_.apply())
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;or&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;(continually(cursor.next _) takeWhile (_ != null)) ++ 
  Stream(cursor.close _) map (_())
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— both of which don’t typecheck as they try to pin a Unit on top on a T Stream.  I’d gladly explore the remaining 20 ideas, as this got me really obsessed with brevity and the strange new world of s.u.c.E-like combinators!  &lt;code&gt;noCatch andThen&lt;/code&gt;, man!&lt;/p&gt;

&lt;p&gt;@dibblego briefly mentioned&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val r = st takeWhile p; fin; r
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— but retracted it in the face of side effects (inherent in dealing with cursor).&lt;/p&gt;

&lt;p&gt;And I ended up with the combinator I really needed, &lt;code&gt;takeWhileFinally&lt;/code&gt;,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  def takeWhileFinally[T](st: Stream[T], p: T =&gt; Boolean, 
    fin: =&gt; Unit): Stream[T] =
    st match {
      case Stream.cons(head,tail) if p(head) =&gt; 
        Stream.cons(head, takeWhileFinally(tail,p,fin))
      case _ =&gt; fin; Stream.empty
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— which keeps pushing &lt;code&gt;fin&lt;/code&gt; to the closing predicate check.  In terms of it, the working &lt;code&gt;cursorStream&lt;/code&gt; looks like&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  def cursorStream[T](cursor: EntityCursor[T]): Stream[T] =
    takeWhileFinally(continually(cursor.next _) map (_.apply()), 
      (_:T) != null, cursor.close)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I also learned that Jorge Ortiz doesn’t like Stream anymore to the point of advocating its banishment, although his Euler project solutions in Scala from 12/2007 employ it with wild abandon, causing Scala 2.8 stack overflow on &lt;code&gt;fib&lt;/code&gt; right away when trying those.&lt;/p&gt;</description><link>http://la.scala.la/post/143154374</link><guid>http://la.scala.la/post/143154374</guid><pubDate>Thu, 16 Jul 2009 21:31:48 -0400</pubDate></item><item><title>Further Maven Findings: CLI plugin, errors</title><description>&lt;p&gt;There’s a &lt;a href="http://wiki.github.com/mrdon/maven-cli-plugin"&gt;maven-cli-plugin&lt;/a&gt; by &lt;a href="http://github.com/mrdon"&gt;Don Brown&lt;/a&gt;, the author of the &lt;a href="http://github.com/mrdon/maven-yamlpom-plugin/tree/master"&gt;maven-yamlpom-plugin&lt;/a&gt; as well.  The CLI one allows to start a maven shell, &lt;code&gt;maven2&gt;&lt;/code&gt;, and enter commands like &lt;code&gt;compile&lt;/code&gt;.  The following findigs apply to Scala:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;when running as &lt;code&gt;mvn cli:execute&lt;/code&gt;, compile means Java compile and does nothing.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;need to run in phase mode, &lt;code&gt;mvn cli:execute-phase&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;then got errors for &lt;code&gt;maven-resource-plugin&lt;/code&gt; having wrong description, apparently &lt;code&gt;twdata&lt;/code&gt;’s, where yaml and cli plugins come from.  This error persists in maven 2.1.0 but is not there for 2.0.10, so I ended up doing the CLI plugin under 2.0.10.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Still, whenever I push arrow up the first time, half the time I get “pipe closed” error and the shell quits.  So much for reusing maven metadata.&lt;/p&gt;

&lt;p&gt;Some other maven fun: when I installed, by tab completing oversight, a tar.gz instead of a jar into it, with -Dpackaging=jar, it did nothing to warn me, and &lt;code&gt;mvn compile&lt;/code&gt; couldn’t find the classes which were supposed to be in that jar; also was showing &lt;code&gt;downloading &lt;that jar&gt;&lt;/code&gt; every time.&lt;/p&gt;

&lt;p&gt;On Mac OSX, &lt;code&gt;jar-with-dependencies&lt;/code&gt; works fine attached to package phase; on CentOS, same pom was doing weird thing under both maven 2.0.10 and 2.1.0, cooking the jar seemingly OK and then erroring with “cannot open zip file.”&lt;/p&gt;

&lt;p&gt;Now since we got a team here at Dartmouth, I’d like to make my m2 repo shared.  So I moved my &lt;code&gt;~/.m2&lt;/code&gt; to &lt;code&gt;/opt/m2&lt;/code&gt;.  Then I created a new &lt;code&gt;~/.m2&lt;/code&gt; and placed the following &lt;code&gt;settings.xml&lt;/code&gt; there:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;settings&gt;
    &lt;localRepository&gt;/opt/m2/repository&lt;/localRepository&gt;
&lt;/settings&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Interestingly, the original &lt;code&gt;settings.xml&lt;/code&gt;, with their &lt;code&gt;&lt;pluginGroups&gt;&lt;/code&gt; defined, are now in &lt;code&gt;/opt/m2/settings.xml&lt;/code&gt; — and seem to work.&lt;/p&gt;

&lt;p&gt;I also found, in &lt;code&gt;$M2_HOME&lt;/code&gt;, there’s a &lt;code&gt;bin/m2.conf&lt;/code&gt; with a line,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;set maven.home default ${user.home}/m2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;but replacing it with an e.g.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;set maven.home default /opt/m2
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— doesn’t lead to using that instead of &lt;code&gt;~/.m2&lt;/code&gt; by itself.&lt;/p&gt;</description><link>http://la.scala.la/post/129096635</link><guid>http://la.scala.la/post/129096635</guid><pubDate>Tue, 23 Jun 2009 22:45:44 -0400</pubDate></item><item><title>Learning About Ranges -- the Hard Way</title><description>&lt;p&gt;I have a 3-part actor pipeline, ReadLines feeding Parsers feeding Inserters.  The Parsers hold both other ends as parameters.  Originally they were connected to each other as&lt;/p&gt;

&lt;p&gt;&lt;a href="http://paste.pocoo.org/show/124614/"&gt;paste&lt;/a&gt;&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;ReadLines started
Inserter 0 started, object com.tfitter.Status$Inserter@384ab40a
Inserter 1 started, object com.tfitter.Status$Inserter@636f2067
Parser 0 started, object com.tfitter.Status$JSONExtractor@7d98d9cf, talking to Inserter 0 [com.tfitter.Status$Inserter@71cd427a]
Parser 1 started, object com.tfitter.Status$JSONExtractor@60328c40, talking to Inserter 1 [com.tfitter.Status$Inserter@1695ef17]
.parser 0 [com.tfitter.Status$JSONExtractor@7d98d9cf] sends its inserter 0 [com.tfitter.Status$Inserter@71cd427a] twit 2173617511
parser 1 [com.tfitter.Status$JSONExtractor@60328c40] sends its inserter 1 [com.tfitter.Status$Inserter@1695ef17] twit 2173617601
parser 0 [com.tfitter.Status$JSONExtractor@7d98d9cf] sends its inserter 0 [com.tfitter.Status$Inserter@71cd427a] twit 2173617606
parser 0 [com.tfitter.Status$JSONExtractor@7d98d9cf] sends its inserter 0 [com.tfitter.Status$Inserter@71cd427a] twit 2173617603
parser 1 [com.tfitter.Status$JSONExtractor@60328c40] sends its inserter 1 [com.tfitter.Status$Inserter@1695ef17] twit 2173617604
parser 0 [com.tfitter.Status$JSONExtractor@7d98d9cf] sends its inserter 0 [com.tfitter.Status$Inserter@71cd427a] twit 2173617605
parser 1 [com.tfitter.Status$JSONExtractor@60328c40] sends its inserter 1 [com.tfitter.Status$Inserter@1695ef17] twit 2173617613
parser 0 [com.tfitter.Status$JSONExtractor@7d98d9cf] sends its inserter 0 [com.tfitter.Status$Inserter@71cd427a] twit 2173617609
parser 1 [com.tfitter.Status$JSONExtractor@60328c40] sends its inserter 1 [com.tfitter.Status$Inserter@1695ef17] twit 2173617607
end of input, informing parser 1
Parser 1 exiting.
parser 0 [com.tfitter.Status$JSONExtractor@7d98d9cf] sends its inserter 0 [com.tfitter.Status$Inserter@71cd427a] twit 2173617614
end of input, informing parser 0
ReadLines exiting
Parser 0 exiting.
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The inserters which started were one, and those to whom parsers send messages — different.  It turns out that &lt;code&gt;(0 until numThreads)&lt;/code&gt; is not a &lt;code&gt;List&lt;/code&gt; but rather a &lt;code&gt;Range&lt;/code&gt;, and map applied to it did something quite different — so when those &lt;code&gt;inserters&lt;/code&gt;, not a list, were iterated over, we got new inserters assigned to parsers instead of the originals.  Or, as &lt;code&gt;@paulp&lt;/code&gt; said, I’ve learned about the ranges the hard way.&lt;/p&gt;

&lt;p&gt;I found it simultaneously by appending the type annotation to &lt;code&gt;inserters&lt;/code&gt; as shown below — that led to adding &lt;code&gt;.toList&lt;/code&gt; onto the &lt;code&gt;Range&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Here’s the right way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;val readLines = new ReadLines(args(0),numThreads,showingProgress)

// I didn't realize I'm not using a List but get a Range...
// added .toList below
val inserters: List[Inserter] = (0 until numThreads).toList map (
    new Inserter(_))

val parsers: List[JSONExtractor] = inserters map (
    ins =&gt; new JSONExtractor(ins.id,readLines,ins))
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally, I started the processes in the end of the main:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;readLines.start
inserters map (_.start)
parsers map (_.start)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Since I had &lt;code&gt;main&lt;/code&gt; defined as&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def main(args: Array[String]) = { ... }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;I was getting an error that &lt;code&gt;main&lt;/code&gt; cannot be found! Apparently this is because the last &lt;code&gt;map&lt;/code&gt; returns not a &lt;code&gt;Unit&lt;/code&gt;.  In OCaml there’s a different iterator in addition to &lt;code&gt;List.map&lt;/code&gt; returning a unit type — &lt;code&gt;List.iter&lt;/code&gt;, and similarly we have &lt;code&gt;foreach&lt;/code&gt;.  Additionally, we can exploit a typical beginners’ error, omitting &lt;code&gt;=&lt;/code&gt; between the &lt;code&gt;def&lt;/code&gt; and the body, to ensure &lt;code&gt;main&lt;/code&gt; discarding any result and thus always returning &lt;code&gt;Unit&lt;/code&gt;.  Altogether, this looks like:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def main(args: Array[String])  { 
    // no = for main: Array[String] =&gt; Unit!

    readLines.start
    inserters foreach (_.start)
    parsers foreach (_.start)
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;On the other hand, for the first time since Ada’s rendezvous, an elegant parallel app works well for me, taking up to 600% CPU on an 8-core box.  Compared with single-threaded MacBook Pro at 38 minutes, that animal finishes in 7 minutes.  This even makes it unnecessary to rewrite the whole thing in OCaml.&lt;/p&gt;</description><link>http://la.scala.la/post/129092030</link><guid>http://la.scala.la/post/129092030</guid><pubDate>Tue, 23 Jun 2009 22:37:18 -0400</pubDate></item><item><title>case objects, parens, extends abstract</title><description>&lt;p&gt;For passing messages between actors in my system, I declared things from memory as&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;abstract case class ParserMessage()
case class Parse(s: String) extends ParserMessage
case class EndOfInput // extends ParserMessage // caused class not found exception!
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;One funny thing occurred is that &lt;code&gt;EndOfInput&lt;/code&gt; was not found in the receiving actor until I cut off its &lt;code&gt;extends&lt;/code&gt;.  This was probably caused by &lt;code&gt;abstract case class&lt;/code&gt; — needed to cut off &lt;code&gt;case&lt;/code&gt; per &lt;code&gt;@jorgeortiz85&lt;/code&gt;.  Scala 2.8 said that case classes without parameters are deprecated — I had to do &lt;code&gt;EndOfInput()&lt;/code&gt; or use a &lt;code&gt;case object&lt;/code&gt;, which I did.  Before that declaring with parens and matching without them also caused “class not found” error.&lt;/p&gt;

&lt;p&gt;So now the right way:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;abstract class ParserMessage() // hmm, still need those ()?
case class Parse(s: String) extends ParserMessage
case object EndOfInput extends ParserMessage
&lt;/code&gt;&lt;/pre&gt;</description><link>http://la.scala.la/post/129057636</link><guid>http://la.scala.la/post/129057636</guid><pubDate>Tue, 23 Jun 2009 21:30:57 -0400</pubDate></item><item><title>default parameters breaking old code</title><description>&lt;p&gt;Fun facts from &lt;code&gt;@DRMacIver&lt;/code&gt; and &lt;code&gt;@paulp&lt;/code&gt;, formatted and elided for blog.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;DRMacIver: Fun fact: the reason Stream changed from a trait to a class was because doing so made its memory usage change from unbounded to constant in certain circumstances,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;paulp: You can’t overload &lt;code&gt;foo&lt;/code&gt; and &lt;code&gt;foo()&lt;/code&gt;, they are the same in definition. You can define overloads in the repl, but like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def foo = 5 ; def foo() = 10&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;— of course that one won’t work.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def foo() = 5 ; def foo(x: String) = 5 + x.length.  
&lt;/code&gt;&lt;/pre&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;paulp: You’d think you could unify these as&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def foo(x: String = "") = 5 + x.length&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;but you will break things if you do.  (I will leave what breaks as puzzle.)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;DRMacIver: The big thing I want to unify and know that we can’t is mkString
paulp: indeed &lt;code&gt;mkString&lt;/code&gt; is a perfect example.
DRMacIver: As clearly it &lt;em&gt;should&lt;/em&gt; be unified as&lt;/p&gt;

&lt;p&gt;&lt;code&gt;def mkString(start : String = "", def connect : String = "", def end : String = "") : String = ...&lt;/code&gt;&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;paulp: and would break the most code, maybe.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;DRMacIver: but the problem is that if you do that then mkString(“,”) does rather the wrong thing&lt;/li&gt;
&lt;li&gt;paulp: DRMacIver: not only that.  &lt;code&gt;mkString&lt;/code&gt; now won’t work.  You’d have to say &lt;code&gt;mkString()&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;DRMacIver: I consider that a boring and acceptable breakage&lt;/li&gt;
&lt;li&gt;paulp: it’s awfully widespread.&lt;/li&gt;
&lt;li&gt;DRMacIver: Particularly given that rules for ()s are going to be tightened up anyway at some point
paulp: then we’d best do it now.&lt;/li&gt;
&lt;li&gt;DRMacIver: It’s awfully widespread and awfully easy to fix.  I think it is something slated for 2.8. But it’s probably one of those things that are easy to forget about…
paulp: what is so wrong with &lt;code&gt;mkString(connect, start, end)&lt;/code&gt; then?&lt;/li&gt;
&lt;li&gt;DRMacIver: Existing code compiles but does the wrong thing.  Which is a nasty thing to do&lt;/li&gt;
&lt;li&gt;paulp: DRMacIver: mark it with a @transition annotation, enable a -Xtransition flag, and complain about everything using it unless they re-mark it with @itscool (or stop compiling with -Xtransition.) I dunno, should be some annoyance-based approach out there.&lt;/li&gt;
&lt;li&gt;paulp: alexyk: it’s a “feature” I believe that you must supply a parameter list if there are any defaults.  I don’t put in feature in quotes because I think it’s a bad idea, it’s probably a good idea.&lt;/li&gt;
&lt;/ul&gt;</description><link>http://la.scala.la/post/128863981</link><guid>http://la.scala.la/post/128863981</guid><pubDate>Tue, 23 Jun 2009 14:31:07 -0400</pubDate></item><item><title>anyToMap versions</title><description>&lt;p&gt;I wanted to get an &lt;code&gt;anyToMap&lt;/code&gt; in a shape most reminiscent of OCaml/Haskell,  similar to a partial function consisting of a series of &lt;code&gt;case&lt;/code&gt; matches in &lt;code&gt;react&lt;/code&gt;.  Here’s some looks.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// original and
// @djspiewak
def anyToMap(a: Any) = a match {
  case m: Map[_, _] =&gt; m
  case _ =&gt; throw new RuntimeException("Baad")
}

// alternatively
def anyToMap(a: Any) = a.asInstanceOf[Map[_, _]]

// verbose
def anyToMap = { a:Any =&gt;a match {
  case m: Map[_, _] =&gt; m
  case _ =&gt; throw new RuntimeException("Baad")
}}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;And the winner is…&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;// @jesnor
val anyToMap : Any =&gt; Map[_, _] = 
    { case m: Map[_,_] =&gt; m; case _ =&gt; error("Baad") }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Also, as &lt;code&gt;@paulp&lt;/code&gt; reminds us, we know that&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;error("Baad") == throw new RuntimeException("Baad")
&lt;/code&gt;&lt;/pre&gt;</description><link>http://la.scala.la/post/128858954</link><guid>http://la.scala.la/post/128858954</guid><pubDate>Tue, 23 Jun 2009 14:20:25 -0400</pubDate></item><item><title>The Price of Trunk</title><description>&lt;p&gt;I need to parse JSON streams, and was going to do it with scala-json.  Now that is an &lt;code&gt;Ant+Ivy&lt;/code&gt; production, untangled below, and it depends on &lt;code&gt;@robey&lt;/code&gt;’s `configgy.  I was wondering why — it has no config…  of course it bit me in the arse.&lt;/p&gt;

&lt;p&gt;There’s a method call, &lt;code&gt;s.regexSub&lt;/code&gt;, in &lt;code&gt;def quote(s: String)&lt;/code&gt;.  Obviously there’s no &lt;code&gt;regexSub&lt;/code&gt; in &lt;code&gt;String&lt;/code&gt; nor in &lt;code&gt;scala.runtime.RichString&lt;/code&gt;, and thus I was searching for a while.  The thing has an import on top:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;import net.lag._
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;— and sure enough, &lt;code&gt;configgy&lt;/code&gt; has a file, &lt;code&gt;extensions.scala&lt;/code&gt;, with said &lt;code&gt;regexSub&lt;/code&gt; and &lt;code&gt;unquoteC&lt;/code&gt; defined on a &lt;code&gt;ConfiggyString&lt;/code&gt;.  There’s also a top-level &lt;code&gt;object extensions&lt;/code&gt; with an&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;implicit def stringToConfiggyString(s: String): ConfiggyString =
    new ConfiggyString(s)
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;OK, got that.  However, &lt;code&gt;configgy&lt;/code&gt; is also &lt;code&gt;Ant+Ivy&lt;/code&gt; and depends on Scala 2.7.3.  Am going into &lt;code&gt;ivysettings.xml&lt;/code&gt; and try to replace 2.7.3 with 2.8.0-SNAPSHOT.  However, as opposed to Maven, Ivy cannot find 2.8.0-SNAPSHOT — in repo-snapshots, on scala-tools.org, there’s a directory &lt;code&gt;2.8.0-SNAPSHOT/&lt;/code&gt;, but no &lt;code&gt;scala-...-2.8.0-SNAPSHOT.jar&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Fine, I edit &lt;code&gt;ivysettings.xml&lt;/code&gt; and add a &lt;code&gt;maven-local&lt;/code&gt; repo, and since I do have a Scala 2.8 in &lt;code&gt;~/.m2/&lt;/code&gt;, it starts compiling — only to error out on collections and stuff.  So much for &lt;code&gt;configgy&lt;/code&gt; for now — could only email &lt;code&gt;@robey&lt;/code&gt; for 2.8 update…&lt;/p&gt;

&lt;p&gt;For now I copied &lt;code&gt;extensions.scala&lt;/code&gt; to &lt;code&gt;scala-json&lt;/code&gt;.  For testing, it’s using &lt;code&gt;specs&lt;/code&gt;, and it turns out &lt;code&gt;specs&lt;/code&gt; doesn’t compile under 2.8 either.  This calls for an &lt;a href="http://groups.google.com/group/specs-users/browse_thread/thread/48a912fd4b62f42f?hl=en"&gt;agonizing choice between BDD and 2.8&lt;/a&gt;, same as for others.  Then I simply uncomment &lt;code&gt;&lt;!--goal&gt;testCompile&lt;/goal--&gt;&lt;/code&gt;.  Now it compiles, issuing some unchecked and deprecated warnings.  Those can be revealed in maven with the following configuration to &lt;code&gt;maven-scala-plugin&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;plugin&gt;
    &lt;groupId&gt;org.scala-tools&lt;/groupId&gt;
    &lt;artifactId&gt;maven-scala-plugin&lt;/artifactId&gt;
    &lt;!--version&gt;2.11-SNAPSHOT&lt;/version--&gt;
    &lt;executions&gt;
        &lt;execution&gt;
            &lt;goals&gt;
                &lt;goal&gt;add-source&lt;/goal&gt;
                &lt;goal&gt;compile&lt;/goal&gt;
                &lt;!-- wait until specs work with 2.8.0 --&gt;
                &lt;!-- goal&gt;testCompile&lt;/goal --&gt;
            &lt;/goals&gt;
        &lt;/execution&gt;
    &lt;/executions&gt;
    &lt;configuration&gt; 
        &lt;!--jvmArgs&gt; 
          &lt;jvmArg&gt;-Xmx128m&lt;/jvmArg&gt; 
          &lt;jvmArg&gt;-Xss2m&lt;/jvmArg&gt; 
        &lt;/jvmArgs--&gt; 
        &lt;args&gt; 
        &lt;!--arg&gt;-target:jvm-${java.src.version}&lt;/arg--&gt; 
        &lt;arg&gt;-deprecation&lt;/arg&gt; 
        &lt;arg&gt;-unchecked&lt;/arg&gt; 
        &lt;!--arg&gt;-encoding&lt;/arg&gt;&lt;arg&gt;${encoding}&lt;/arg--&gt; 
        &lt;arg&gt;-optimise&lt;/arg&gt; 
        &lt;!--arg&gt;-verbose&lt;/arg--&gt; 
        &lt;/args&gt; 
    &lt;/configuration&gt;
&lt;/plugin&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;OK, now I got my &lt;code&gt;scala-json&lt;/code&gt;!  Using &lt;code&gt;@paulp&lt;/code&gt;’s cool &lt;code&gt;:jar&lt;/code&gt; command in REPL, we can test it right away, without restarting REPL:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;scala&gt; :jar /Users/alexyk/.m2/repository/com/twitter/scala-json/1.0/scala-json-1.0.jar
Added /Users/alexyk/.m2/repository/com/twitter/scala-json/1.0/scala-json-1.0.jar to your classpath.

scala&gt; import com.twitter.commons.Json
import com.twitter.commons.Json

scala&gt; Json.build(Json.build(List(1, 2))).toString
res0: String = [1,2]
&lt;/code&gt;&lt;/pre&gt;</description><link>http://la.scala.la/post/124957831</link><guid>http://la.scala.la/post/124957831</guid><pubDate>Tue, 16 Jun 2009 22:59:40 -0400</pubDate></item><item><title>mvn install:install-file # woes</title><description>&lt;p&gt;Trying to install an ivy-generated jar for &lt;code&gt;configgy-1.3&lt;/code&gt; into the local &lt;code&gt;.m2&lt;/code&gt; repo.  &lt;code&gt;Ant+Ivy&lt;/code&gt; end up with a &lt;code&gt;dist/&lt;/code&gt; directory, e.g. &lt;code&gt;dist/configgy-1.3/configgy-1.3.{jar,pom}&lt;/code&gt; and more.  When I try to do, manually,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt; mvn install:install-file -Dfile=configgy-1.3.jar -DgroupId=net.lag
   -DartifactId=configgy -Dversion=1.3 -Dpackaging=jar
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;maven complains there’s no &lt;code&gt;artifactId&lt;/code&gt; or etc. or &lt;code&gt;packaging&lt;/code&gt; — it was originally &lt;code&gt;-Dpackaging=jar&lt;/code&gt; missing, like it doesn’t know what, huh?  You messin’ with me, mah-ven?&lt;/p&gt;

&lt;p&gt;Thanks to &lt;code&gt;@mharrah&lt;/code&gt;’s good graces, here’s a way to use Ant+Ivy to install the fruit of its labors into maven’s own repo:&lt;/p&gt;

&lt;p&gt;Change “local” on line 85 of &lt;code&gt;ant/package.xml&lt;/code&gt; to &lt;code&gt;"maven-local"&lt;/code&gt;
Add this after &lt;code&gt;&lt;/chain&gt;&lt;/code&gt; on line 22 of &lt;code&gt;ivy/ivysettings.xml&lt;/code&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;filesystem name="maven-local" m2compatible="true"&gt;
  &lt;artifact pattern="${user.home}/.m2/repository/[organisation]/
  [module]/[revision]/[artifact]-[revision](-[classifier]).[ext]"/&gt;
&lt;/filesystem&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Run &lt;code&gt;ant package&lt;/code&gt;&lt;/p&gt;</description><link>http://la.scala.la/post/124763131</link><guid>http://la.scala.la/post/124763131</guid><pubDate>Tue, 16 Jun 2009 16:14:47 -0400</pubDate></item><item><title>Let the Week Begin with Another Java Build-Tool ACI!</title><description>&lt;p&gt;Those who read this blog probably know what ACI means.  (See previous posts if you don’t.)  One thing about Java-based world is that it produces ACI in a frequency similar to MSFT: there’s a new self-inflicted Java build tool ACI every day!  Here’s how.&lt;/p&gt;

&lt;p&gt;I didn’t rest on my newly obtained Maven-fu laurels more than a day when I wanted to get &lt;a href="http://github.com/stevej/scala-json/tree/master"&gt;&lt;code&gt;scala-json&lt;/code&gt; off the github&lt;/a&gt; and into my mavenized project.  Well, &lt;code&gt;scala-json&lt;/code&gt; is not mavenized, but rather is &lt;code&gt;antivyized&lt;/code&gt; — it uses the &lt;code&gt;Ant+Ivy&lt;/code&gt; combo to build, as other things from Twitter HQ do nowadays.&lt;/p&gt;

&lt;p&gt;Since it relied on older versions of everything, I had to immediately go in and prevent downloading of obsolete Scala source jars — even Maven didn’t want those sources, but this did.&lt;/p&gt;

&lt;p&gt;The build breaks at docs for the lack of a Scala method (presumably due to the Scala version update).  It still produces some &lt;code&gt;commons-1.0&lt;/code&gt; packages — both a &lt;code&gt;zip&lt;/code&gt; and a &lt;code&gt;jar&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now I end up with a &lt;code&gt;dist&lt;/code&gt; subdir with a &lt;code&gt;commons-1.0.zip&lt;/code&gt; and &lt;code&gt;commons-1.0/&lt;/code&gt;, the latter containing the &lt;code&gt;commons-1.0.jar&lt;/code&gt;.  Strangely, the zip contains all the prerequisites but no json, and the jar is the json.  But it’s referred in the &lt;code&gt;commons-1.0.pom&lt;/code&gt; sitting alongside by &lt;code&gt;artifactId org.twitter&lt;/code&gt; and &lt;code&gt;name commons&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How do you install an &lt;code&gt;Ant+Ivy&lt;/code&gt;-built jar into your maven repo?&lt;/p&gt;</description><link>http://la.scala.la/post/124170876</link><guid>http://la.scala.la/post/124170876</guid><pubDate>Mon, 15 Jun 2009 17:27:58 -0400</pubDate></item><item><title>Encrypting and Replacing Passwords with Maven</title><description>&lt;p&gt;I’ve had an interesting ACI experience (lana.reverse-cranial inversion, similar to MSFT’s inversion of / into \) trying to hide my Twitter password in a Maven app from it being deployed to GitHub.  At first I absent-mindedly hard-coded the password into code and uploaded it, which called for a need to learn &lt;a href="http://github.com/guides/completely-remove-a-file-from-all-revisions"&gt;how to obliterate a file completely from a git repo&lt;/a&gt;.  Then temporarily, I replaced the password line with&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;twitterPassword = readLine
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which of course is still not good enough, even though you can call such a program with &lt;code&gt;echo pwd | program&lt;/code&gt; and keep in the commandline for repetition with GNU readline.  One real persistent way to do it is read from a file.  The below is the same thing with ACI-inspired Maven and Java resources.&lt;/p&gt;

&lt;p&gt;Basic Maven resource filtering is described in &lt;a href="http://maven.apache.org/guides/getting-started/index.html#How_do_I_filter_resource_files"&gt;Maven Getting Started Guide — Filtering Resources&lt;/a&gt;.  I created my &lt;code&gt;src/main/resources/application.resources&lt;/code&gt; file with just two lines,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;${twitter.user}
${twitter.password}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The actual passwords are in another file I created &lt;em&gt;not to be version-tracked&lt;/em&gt;, &lt;code&gt;src/main/filters/filter.properties&lt;/code&gt;, with just two lines for the actual things:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;twitter.user=actualUserName
twitter.password=actualPassword
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Then I stuck the following into the POM:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;build&gt;
    &lt;sourceDirectory&gt;src/main/scala&lt;/sourceDirectory&gt;

    &lt;filters&gt;
        &lt;filter&gt;src/main/filters/filter.properties&lt;/filter&gt;
    &lt;/filters&gt;

    &lt;resources&gt;
      &lt;resource&gt;
        &lt;directory&gt;src/main/resources&lt;/directory&gt;
        &lt;filtering&gt;true&lt;/filtering&gt;
      &lt;/resource&gt;
    ...
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Now we access this resource as follows, from Scala’s main:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;  def main(args: Array[String]) = {

    def getProperties = 
      this.getClass getResourceAsStream "/application.properties"
    var st = getProperties

    // in order to reset, do ss = ss.reset // !
    var ss = BufferedSource.fromInputStream(st, "UTF-8", 512, { 
      () =&gt;  Source.fromInputStream(getProperties) })

    val linesIter = ss.getLines
    val twitterUser=linesIter.next.trim
    // can use some simple steganography here
    val twitterPassword=linesIter.next.trim

    println("user=&gt;"+twitterUser+", password=&gt;"+twitterPassword)
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;In order to be able to reread from the stream, one wishes to make it resettable with &lt;code&gt;ss.reset&lt;/code&gt; — and it turned out to be necessary to use &lt;code&gt;BufferedSource&lt;/code&gt; here.  You &lt;em&gt;have&lt;/em&gt; to reassign &lt;code&gt;ss.reset&lt;/code&gt; to something on which you’ll re-read the source, e.g. declare it a &lt;code&gt;var&lt;/code&gt; and reassign to itself with &lt;code&gt;ss = ss.reset&lt;/code&gt; (hence that &lt;code&gt;var&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Since I own &lt;code&gt;scala.la&lt;/code&gt; domain, my packages are all operas, starting with &lt;code&gt;la.scala&lt;/code&gt;.  Thus I discovered I can’t refer to things as &lt;code&gt;scala.io.BufferedSource&lt;/code&gt; — I either have to prefix it with &lt;code&gt;_root_.&lt;/code&gt;, or import things, as I’ve done here.  Alas, one can’t rename top-level &lt;code&gt;scala&lt;/code&gt; with something like &lt;code&gt;import _root_.{scala=&gt;lisp}&lt;/code&gt;: it compolains, somewhat confusingly, that &lt;code&gt;_root_&lt;/code&gt; cannot be imported.&lt;/p&gt;

&lt;p&gt;It would be nice to encrypt all passwords similarly to &lt;a href="http://maven.apache.org/guides/mini/guide-encryption.html"&gt;password encryption in Mavem 2.1.0 &lt;code&gt;settings.xml&lt;/code&gt;&lt;/a&gt;.  However, it’s used mostly for deployment plugin.  Were you to refer to the passwords in filtered resources, they still will be plain text in the end.  One thing I suggested on the Maven Users list is&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the password to be encrypted in the same way as Maven 2.1.0 does it; then&lt;/li&gt;
&lt;li&gt;the password decrypted internally during compilation&lt;/li&gt;
&lt;li&gt;the source file with the password to be interpolated with the decrypted password during compilation&lt;/li&gt;
&lt;li&gt;upon successful compilation into a class file or failed compile, the expanded source to be discarded from target/&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;I also asked, on the Maven Users list, whether, and how, one can refer to elements from &lt;code&gt;settings.xml&lt;/code&gt;:&lt;/p&gt;

&lt;p&gt;Also, I tried to refer to the password as defined in settings.xml,&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;servers&gt;
    &lt;server&gt;
        &lt;id&gt;twitter&lt;/id&gt;
        &lt;username&gt;blah&lt;/username&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;…&lt;/p&gt;

&lt;p&gt;— but the dotted notation seems incapable of referring to the username from the block with the id of “twitter”.  Is there a way to do it with &lt;code&gt;${settings:...what?...}&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Nobody from Sonatype replied to this, which a bit clouds the idea of “Sonatype the Maven company” supporting their product properly.&lt;/p&gt;

&lt;p&gt;Some more references to &lt;code&gt;settings.xml&lt;/code&gt; are here:&lt;/p&gt;

&lt;p&gt;&lt;a href="http://maven.apache.org/ref/2.1.0/maven-settings/settings.html"&gt;Maven Settings&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="http://maven.apache.org/settings.html"&gt;General Settings&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One thing worth checking, suggested by &lt;code&gt;@nuttycom&lt;/code&gt;, is setting a &lt;em&gt;repo policy&lt;/em&gt; there to &lt;em&gt;not&lt;/em&gt; auto-download-and-update snapshots, preventing the sutuation where my local Scala build is partially supplanted by a broken Maven snapshot (where scala-compiler was failing for a while).&lt;/p&gt;

&lt;p&gt;Upd.  Here’s how one can filter the source code — just have to tell the compile phase to work off the filtered code, not the originals — by &lt;a href="http://www.gxdeveloperweb.com/Blogs/Bram-de-Kruijff/Maven-secrets-filtering-sources.htm"&gt;Bram de Kruijff&lt;/a&gt;:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;&lt;project...&gt;
  ...
  &lt;build&gt;
    ...
      &lt;!-- Overrule the default pom source directory to match
            our generated sources so the compiler will pick them up --&gt;
      &lt;sourceDirectory&gt;target/filtered-sources/java&lt;/sourceDirectory&gt;
  ...
  &lt;/build&gt;
...
&lt;/project&gt;
&lt;/code&gt;&lt;/pre&gt;</description><link>http://la.scala.la/post/124159629</link><guid>http://la.scala.la/post/124159629</guid><pubDate>Mon, 15 Jun 2009 17:03:41 -0400</pubDate></item></channel></rss>
