Skip to content

Instantly share code, notes, and snippets.

@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)
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)
}
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("["))
}
}
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
@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
@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 / assoc.bat
Created June 19, 2012 14:24
java applet file assoc
assoc .svg=SvgGraphicsFile
ftype SvgGraphicsFile=C:\Program Files\Internet Explorer\iexplore.exe "www.someplace.ru/index.html?file=" "%1"
@Rogach
Rogach / SlidingWindowMap.scala
Created September 3, 2012 03:51
SlidingWindowMap
class SlidingWindowMap(keys: Set[String], maxCount: Int, periodMs: Int) {
val times = collection.mutable.Map(keys.map(k => (k, Vector[Long]())).toList:_*)
def nextKey: Option[String] = {
val now = System.currentTimeMillis
this.synchronized {
val key = times.find(_._2.dropWhile(_ < now - periodMs).size < maxCount).map(_._1)
key.foreach { k => times(k) = times(k).dropWhile(_ < now - periodMs) :+ now }
key
}
}
@Rogach
Rogach / Simple.scala
Created September 18, 2012 08:07
File to test highlighting upon
package simple
package even.simpler
import scala.collection._
import scala.util.{Random => Rnd}
import scala.collection.mutable.Map
object Simple {
type TP = Seq[String]
@Rogach
Rogach / gist:3801267
Created September 28, 2012 17:55
Bigger scala tuples
git clone git://github.com/scala/scala.git scala-tuplicity
cd scala-tuplicity
git checkout v2.9.2
export ANT_OPTS="-Xmx8192m -Xss25M -Xms4096M -XX:MaxPermSize=512M"
VERS="-Dbuild.release=true -Dversion.number=2.9.2-tuplicity -Dmaven.version.number=2.9.2-tuplicity"
ant build
sed -i 's/\(val MaxTupleArity, .*\) 22/\1 222/' src/compiler/scala/tools/nsc/symtab/Definitions.scala
ant build
ant replacelocker
sed -i 's/\(MAX_ARITY .*\) 22/\1 222/' src/build/genprod.scala