Skip to content

Instantly share code, notes, and snippets.

View notyy's full-sized avatar

大魔头 notyy

View GitHub Profile
@notyy
notyy / gist:2050000
Created March 16, 2012 13:06
how to improve
scala> val source ="access_token=C4F0730***********485824&expires_in=7776000"
source: java.lang.String = access_token=C4F0730***********485824&expires_in=7776000
scala> val (accToken, expire) = (source.split('&')(0).split('=')(1), source.split('&')(1).split('=')(1))
accToken: String = C4F0730***********485824
expire: String = 7776000
@notyy
notyy / gist:2022552
Created March 12, 2012 15:13
logger with value
def myFunc() = {
val rs = calcSomeResult()
logger.info("result is:" + rs)
rs
}
为避免上面的麻烦写法:
object LogUtil {
def kestrel[A](x: A)(f: A => Unit): A = { f(x); x }
def logV[A](f: String => Unit)(s: String, x: A) = kestrel(x) { y => f(s + ": " + y) }
def perm[T](ls: List[T]): List[List[T]] = ls match {
case Nil => List[Nil]
case xs => for{
(x,i) <- ls.zipWithIndex
(leftX,rightX) = xs.splitAt(i)
ys <- perm(leftX ++ rightX.tail)
} yield (x:: ys)
}
@notyy
notyy / gist:1869326
Created February 20, 2012 13:55
2D字幕文件转3D
本程序逻辑如下:
1、列出当前目录下所有后缀名为srt的文件,过滤掉其中文件名以-3D结尾的文件
2、对每个符合条件1的文件,读取其内容,通过在字幕内容中加入坐标偏移量,使之能在左右屏的3D电视上正常播放
3、srt文件格式
1、文件由多个段落组成,段落之间以空行分隔
2、每段落中的内容由3部分组成
1、序号
2、时间量
3、字幕内容
4、转换逻辑是将原字幕内容分别在前面加上偏移量{\pos(96,255)\fscx50}和{\pos(288,255)\fscx50},变成两段内容,一个序号保持不变,位置也不变,而另一段必须加到原内容最后,序号递增
@notyy
notyy / gist:1619443
Created January 16, 2012 06:42
match case
def showType: Any => String = {
case s:String => "it's a String:" + s
case i:Int => "it's a Int:" + i
case l:List[Any] => "it's a List,length: " + l.length
case _ => "I don't know"
}
def showTypeM(t:Any):String = t match {
case s:String => "it's a String:" + s
case i:Int => "it's a Int:" + i
@notyy
notyy / gist:1565673
Created January 5, 2012 15:16
compolse
def m2(x:Int) = x * 2
def p1(x:Int) = x + 1
scala> (1 to 5).map(m2).map(p1)
res3: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 5, 7, 9, 11)
scala> (1 to 5).map(m2 _ andThen p1)
res9: scala.collection.immutable.IndexedSeq[Int] = Vector(3, 5, 7, 9, 11)
@notyy
notyy / gist:1548012
Created January 1, 2012 18:42
scala covariant
class Fruit {
def selfIntro() = "Fruit"
}
class Apple extends Fruit{
override def selfIntro() = "Apple"
}
class Package[T] (val contents:Seq[T]) {
def this() = this(List[T]())
@notyy
notyy / gist:1547987
Created January 1, 2012 18:28
Java covaiant
Object s = "abc";
List<Object> objects = new java.util.ArrayList<String>();
String[] a = new String[] { "abc" };
Object[] b = a;
b[0] = 1;
System.out.println(a[0]);
@notyy
notyy / gist:1547732
Created January 1, 2012 16:39
covariant
class Father {
def say() {
Console.println("I am father")
}
}
class Son extends Father {
override def say() {
Console.println("I am son")
}
@notyy
notyy / gist:1547150
Created January 1, 2012 12:03
runable
object Main extends App {
object SearchCodeEnum extends Enumeration {
type SearchCodeValue = Value
val org = Value
val file = Value
}
for(code <- SearchCodeEnum.values) {println(code)}
}