This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Date | |
import org.json4s._ | |
import native.JsonMethods._ | |
import org.json4s.native.Serialization | |
val body = """{ | |
|"_id": "1223456a171", | |
|"siteid": "abc", | |
|"serverid": "3266", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(",") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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" |