Skip to content

Instantly share code, notes, and snippets.

View Centaur's full-sized avatar

oldpig Centaur

  • singerdream.com
  • Shanghai
View GitHub Profile
package org.scalaconsole
import swing.TextComponent
import javax.swing.JTextPane
import util.regexp.PointedHedgeExp
import java.awt.{Graphics2D, Point}
class TextPane extends TextComponent {
override lazy val peer: JTextPane = new JTextPane() with SuperMixin
@Centaur
Centaur / initLift.scala
Created August 1, 2011 11:11
script to create a minimal lift project
#!/bin/sh
exec scala $0 $@
!#
import java.io._
def cwd = System.getProperty("user.dir")
def setCwd(s:String) {
if(!new File(s).exists())
def quicksort[T : Ordering](l: List[T]) : List[T] = l match {
case Nil => Nil
case x :: xs =>
val ord = Ordering[T]
import ord._
val smaller = quicksort(xs.filter(_<=x))
val bigger = quicksort(xs.filter(_>x))
smaller ++ List(x) ++ bigger
}
@Centaur
Centaur / updatesbt.scala
Created November 10, 2011 11:17
a scala script to update sbt-launch.jar to the latest version
#!/bin/sh
exec scala $0 $0 $@
!#
/**
* This script finds the newest sbt-launch.jar from its official repository,
* update the one in th same directory of this script if necessary.
* it manages the current sbt version in a companion file 'current_sbt_version'
* and will create it if not already exists.
*/
@Centaur
Centaur / updatesbt.scala
Created December 5, 2011 15:10
sbt updator for windows (rename it to .bat)
::#!
@echo off
call scala .\%0 .\%0 %*
goto :eof
::!#
/**
* This script finds the newest sbt-launch.jar from its official repository,
* update the one in th same directory of this script if necessary.
* it manages the current sbt version in a companion file 'current_sbt_version'
import collection.mutable.ListBuffer
case class Part(contentType:Option[String], encoding:Option[String], location:Option[String], data:ListBuffer[String])
var boundary: String = null
val Boundary = """.*boundary="(.*)"""".r
var state = 0
val IN_PART = 1
val IN_DATA = 2
@Centaur
Centaur / reorganizelist.scala
Created February 26, 2012 14:01
For Freewind
case class Message(username:String, content:String)
val list = List(Message("aaa", "111"), Message("aaa","222"),
Message("bbb","333"),Message("aaa", "444"))
import collection.mutable.ListBuffer
list.foldLeft(new ListBuffer[(String,ListBuffer[Message])]()){
case (buff, item) =>
if(buff.nonEmpty && buff.last._1 == item.username) buff.last._2 += item
@Centaur
Centaur / CssSelector.scala
Created March 18, 2012 14:18
scala xml css selector
import xml._
case class CssSelector(css:String) {
def find(node:Node):Seq[Node]
def unique(node:Node):Option[Node] = find(node).headOption
}
assert(CssSelector("div.some_class").unique(<div><div class="some_class">abc</div></div>).get.text == "abc")
assert(CssSelector("div#some_id span.some_class".unique(
<div id="some_id" class="any_class">
@Centaur
Centaur / dsl.scala
Created March 22, 2012 05:32
scala dsl demo
class ShhConsumeTest extends Logging with FunSuite{
val pos = new ShhVirtualPos("192.168.1.14", 5960, "206290010320001", "20000005")
var card = Card("9444443228739457375", "954000")
test("send") {
assert(pos.签到.result === OK)
val 消费交易 = card 在 pos 消费 Money(9.63)
assert(消费交易.result === OK)
}
}
@Centaur
Centaur / No null in.scala
Created April 11, 2012 14:06
For 阿丑
def doSomethingIfNoNull(name:String, password:String, other:String, doSomething: =>String): Option[String] = {
for(n <- Option(name);
p <- Option(password);
o <- Option(other))
yield(doSomething)
}
assert(doSomethingIfNoNull("abc", null, "cde", "Do something") == None)
assert(doSomethingIfNoNull(null, "abc", "cde", "Do something") == None)
assert(doSomethingIfNoNull("abc", "cde", null, "Do something") == None)