Skip to content

Instantly share code, notes, and snippets.

View Centaur's full-sized avatar

oldpig Centaur

  • singerdream.com
  • Shanghai
View GitHub Profile
val extraVersion: Option[ExtraVersion] = e match {
case ExtraRC(no) => Some(RC(no.toInt))
case ExtraBETA(no) => Some(BETA(no.toInt))
case ExtraSNAPSHOT(date) => Some(SNAPSHOT(date))
case ExtraM(no) => Some(M(no.toInt))
case Minus(no) => Some(GA(no.toInt))
case _ => None
}
val extraVersion: Option[ExtraVersion] = Option(e match {
import scala.language.higherKinds
trait MaybeEmpty[T[_]] {
def isEmpty(thing: T[_]): Boolean
}
implicit def optionMaybeEmpty[O[_] <: Option[_]] = new MaybeEmpty[O] {
def isEmpty(thing: O[_]) = thing.isEmpty
}
def findWithPriority[T](xs: Seq[T], high: T => Boolean, low: T => Boolean): Option[T] = ???
assert(findWithPriority(List(1, 3, 4, 2), (x: Int) => x > 3, (x: Int) => x > 2) == Some(4))
assert(findWithPriority(List(1, 4, 3, 2), (x: Int) => x > 2, (x: Int) => x > 3) == Some(4))
assert(findWithPriority(List(1, 3, 4, 2), (x: Int) => x > 3, (x: Int) => x > 4) == Some(4))
assert(findWithPriority(List(1, 3, 4, 5, 2), (x: Int) => x > 5, (x: Int) => x > 3) == Some(4))
assert(findWithPriority(List(1, 3, 4, 2), (x: Int) => x > 4, (x: Int) => x > 4) == None)
@Centaur
Centaur / mytypedouble.scala
Last active August 29, 2015 14:01
Double MyType
object doubleMyType {
trait MyList[E, Small <: MyList[E, Small, Big], Big <: MyList[E, Small, Big]] {
def head: E
def tail: MyList[E, Small, Big]
def createSmallList(head: E, tail: MyList[E, Small, Big]): Small
def createBigList(head: E, tail: MyList[E, Small, Big]): Big
@Centaur
Centaur / fromScalaConsole.scala
Created May 9, 2014 03:30
json4s customized serialization
import java.util.Date
import org.json4s._
import native.JsonMethods._
import org.json4s.native.Serialization
val body = """{
|"_id": "1223456a171",
|"siteid": "abc",
|"serverid": "3266",
def countDigit(i: Int): Int = {
def helper(n: Int, accu:Int): Int = {
if (n >= 0 && n <10) accu
else helper(n/10, accu + 1)
}
helper(i, 1)
}
assert(3 == countDigit(100))
@Centaur
Centaur / fromScalaConsole.scala
Created April 23, 2014 04:07
transform date
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.util.Locale
import scala.util.matching.Regex.Match
val extractor = """<Current Date \+ (\d+) days>""".r
def transform(src: String): String = {
def calc(matched: Match) = {
LocalDate.now().plusDays(matched.group(1).toInt).format(DateTimeFormatter.ofPattern("MMMM dd, YYYY", Locale.US))
def pair(s: String, key: Int, value: Int): (String,String) = {
val arr = s.split(",")
arr(key) -> arr(value)
}
def pair[V](s: String, key: Int, value: (Int, String => V)): (String,V) = {
val arr = s.split(",")
arr(key) -> value._2.apply(arr(value._1))
}
def pair[K](s: String, key: (Int, String => K), value: Int): (K, String) = {
val arr = s.split(",")
@Centaur
Centaur / fromScalaConsole.scala
Last active August 29, 2015 13:59
extract pair as expected type
trait CanFromString[T] {
def fromString(s: String): T
}
implicit object intCanFromString extends CanFromString[Int] {
override def fromString(s: String): Int = s.toInt
}
implicit object SymbolCanFromString extends CanFromString[Symbol] {
override def fromString(s: String): Symbol = Symbol(s)
}
// You won't be able to compile this with:
// scala -language:dynamics NoDynamics.scala
// These two ambiguous implicits disable the `dynamics` feature.
implicit def ambDynamics1: language.dynamics.type = ???
implicit def ambDynamics2: language.dynamics.type = ???
object Woo extends Dynamic {
def selectDynamic(name: String) = s"$name a mess of a language"