Skip to content

Instantly share code, notes, and snippets.

@Rogach
Rogach / analyze.scala
Created May 18, 2012 08:11
Activity tracker
#!/usr/bin/scala
!#
val activities = Seq(
("blogging", "chrome", "^Blogger".r),
("games", "chrome", "Game".r),
("guake", "python", "^Guake!$".r)
)
io.Source.fromFile(args(0)).getLines.toList // read the file
@Rogach
Rogach / gist:2576699
Created May 2, 2012 13:58
after init article
trait AfterInit extends DelayedInit {
def afterInit
def afterInitLevel = 1
private var initCount = 0
private def getInitNumber(clazz: Class[_]):Int =
if (clazz.getSuperclass == classOf[java.lang.Object]) 0 else getInitNumber(clazz.getSuperclass) + 1
final def delayedInit(x: => Unit) {
x
initCount += 1
if (getInitNumber(this.getClass) + afterInitLevel == initCount) afterInit
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
object ReadXMLFile {
def main(argv: Array[String]) {
try {
val factory = SAXParserFactory.newInstance
def readConfig(lines:List[String]):mutable.Map[String,Map[String,String]] = {
// assuming that *all* properties have their sections
if (lines.isEmpty) Map()
else {
val sectionRgx = """\s*\[(.*)\]\s*""".r
val sectionRgx(sectName) = lines.head
val pairs = lines.tail.takeWhile(!_.startsWith("[")).map(_.split("=")).map(a => (a(0),a(1))).toMap
Map(sectName -> pairs) ++ readConfig(lines.tail.dropWhile(!_.startsWith("["))
}
}
def LogConfig(configItem: String, configValue: Any) = {
val logString = configValue match {
case configValueMap: Map[_,_] => "Configured " + configItem + " to " + configValueMap.mkString("{",", ","}") + "."
case _ => "Configured " + configItem + " to " + configValue + "."
}
info(logString)
}
@Rogach
Rogach / example.scala
Created April 8, 2012 11:34
scallop examples 2
object Conf extends ScallopConf(List("-a","3","-b","5","tree")) {
val apples = opt[Int]("apples")
val bananas = opt[Int]("bananas")
verify
}
Conf.apples() // 3
Conf.bananas.get // Some(5)
@Rogach
Rogach / ScallopTest.scala
Created April 7, 2012 10:19 — forked from alexy/ScallopTest.scala
Holder fails, builder parses trailing args allright
import org.rogach.scallop._
import org.scalatest.FunSuite
import org.scalatest.matchers.ShouldMatchers
/**
* Created with IntelliJ IDEA.
* User: Alexy
* Date: 4/6/12
* Time: 10:34 AM
@Rogach
Rogach / gist:2308208
Created April 5, 2012 05:20
ScallopConf
object Main extends App {
object Conf extends ScallopConf(arguments) {
val apples:SOption[Int] = opt[Int]("apples")
val bananas = opt[Int]("bananas")
val file = trailArg[String]
verify
}
Conf.apples.get
Conf.bananas.option // not sure about option retreiving syntax
// maybe I should copy the code for SOption from stdlib Option?
@Rogach
Rogach / Help output.txt
Created April 4, 2012 20:16
Scallop examples
test 1.2.3 (c) 2012 Mr Placeholder
Usage: test [OPTION]...
test is an awesome program, which does something funny
Options:
-Dkey=value [key=value]...
some key-value pairs
-d, --donkey
use donkey mode
-m, --monkeys <arg>
@Rogach
Rogach / gist:1661713
Created January 23, 2012 08:18
Non-homogeneous tree
package socco
abstract class SoccoTree(val children:List[SoccoTree]) extends Seq[SoccoTree] with SoccoNode[SoccoTree] {
def apply(n:Int) = children(n)
def iterator = children.iterator
def length = children.length
// def add(t:SoccoTree)(implicit builder:SoccoTreeBuilder[B]) = builder.build(this.asInstanceOf[B], children :+ t)
}
trait SoccoNode[B <: SoccoTree] {
def soccoCompanion:SoccoTreeCompanion[B]