Skip to content

Instantly share code, notes, and snippets.

View Centaur's full-sized avatar

oldpig Centaur

  • singerdream.com
  • Shanghai
View GitHub Profile
@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 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 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",
@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
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)
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
}
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 {
val keywords = Set("class", "public", "private", "protected")
def wordCount(is: java.io.InputStream): Map[String, Int] = {
var result = Map[String, Int]()
val scanner = new java.util.Scanner(is)
while(scanner.hasNext) {
val word = scanner.next
if(keywords.contains(word)) {
if(result.contains(word)) {
@Centaur
Centaur / gist:cb0d95116b6b691993c0
Created August 14, 2014 03:17
refactor challenge
object PathList {
def unapplySeq(path: String): Option[Seq[String]] =
if(path.isEmpty) None
else Some(path.split("/"))
}
def testMatch(str: String): Boolean = str match {
case PathList("org", "scalaconsole", "fxui", "main", "ace-builds", sub) => true
case PathList("org", "scalaconsole", "fxui", "main", "ace-builds", sub, xs@_*) if sub != "src-min-noconflict" => true

I'm having trouble understanding the benefit of require.js. Can you help me out? I imagine other developers have a similar interest.

From Require.js - Why AMD:

The AMD format comes from wanting a module format that was better than today's "write a bunch of script tags with implicit dependencies that you have to manually order"

I don't quite understand why this methodology is so bad. The difficult part is that you have to manually order dependencies. But the benefit is that you don't have an additional layer of abstraction.