Skip to content

Instantly share code, notes, and snippets.

View kareblak's full-sized avatar

Kåre Blakstad kareblak

View GitHub Profile
@kareblak
kareblak / gist:4041559
Created November 8, 2012 20:59
StringConverter
case class TjalleBalle(dblOpt: Option[Double])
//Og her skal det skje magi
TjalleBalle("1.080") // men det gjør det altså ikke, og den whiner på feil type.
case class StringConverter[T](f: String => T)
implicit val convDouble = StringConverter[Double](_.toDouble)
implicit val convInt = StringConverter[Int](_.toInt)
implicit val convString = StringConverter[String](_)
implicit val convDouble = convOption[Double](_.toDouble)_
implicit val convInt = convOption[Int](_.toInt)_
implicit val convString = convOption[String](_.toString)_
def convOption[T](f: String => T)(s: String) = try { Some(f(s)) } catch { case _ => None }
val intent = TryCatch({
case e: IllegalArgumentException => logger.warn(e.getMessage, e); BadRequest ~> ResponseString(e.getMessage)
case e: UnsupportedOperationException => logger.warn(e.getMessage, e); NotImplemented ~> ResponseString(e.getMessage)
case e => logger.error(e.getMessage, e); InternalServerError
}).apply(intentImpl)
private lazy val intentImpl: Plan.Intent = {
package unfilteredx.kit
import unfiltered.request._
import unfiltered.response._
object Secure {
def secure[A,B](intent: unfiltered.Cycle.Intent[A,B]) = {
case req@ForwardedProto(proto) if (intent.isDefinedAt(req)) => intent(wrap(req, "https" == proto.toLowerCase))
case req if (intent.isDefinedAt(req)) => intent(req)
}
def load(id: Long)(profile: ProfileWrapper): Either[Exception, StoredSearch] = {
try {
val alert = Option(alertService.getAlert(id))
alert.map {
a: AlertBeanSpringJDBC =>
if (a.getLoginId != profile.id) {
Left(new SecurityException("This is not owned by the logged in user."))
}
Right(StoredSearch(
Some(a.getAlertId),
trait JsonParser extends BodyParserModule {
def parse[String, O <: Parsable[O]](a: String): O = a match {
case O(o) => o
case _ => ???
}
}
sealed trait Parsable[A] {
def parse(a: String): A
@kareblak
kareblak / gist:6007721
Created July 16, 2013 10:54
How to get rid of IntelliJ metafiles
rm -rf ~/Library/Preferences/com.jetbrains.intellij.plist
rm -rf ~/Library/Preferences/com.jetbrains.intellij.plist.lockfile
rm -rf ~/Library/Preferences/IntelliJIdea*
rm -rf ~/Library/Caches/IntelliJIdea*
rm -rf ~/Library/Application Support/IntelliJIdea*
rm -rf ~/Library/Caches/com.jetbrains.intellij
rm -rf ~/Library/Logs/IntelliJIdea
rm -rf ~/Library/Saved Application State/com.jetbrains.intellij.savedState
rm -rf ~/Library/Preferences/Idea*
rm -rf ~/Library/Preferences/com.jetbrains*
@kareblak
kareblak / gist:6974587
Last active December 25, 2015 12:09
Why?
scala> case class Pair[A,B](head: A, tail: B)

defined class Pair


scala> type HeadlessPair[B] = Pair[Any, B] // Setting it to any, so the compiler wont care when type view is accessed

defined type alias HeadlessPair
scala> def f[A[_], B](ab: A[B]) {}

warning: there were 1 feature warning(s); re-run with -feature for details

f: [A[_], B](ab: A[B])Unit

@kareblak
kareblak / gist:8456943
Last active January 3, 2016 11:29
Where's the structural type?
object FuncConversions {
//Nay
implicit def guavaFunction2function[A, R](fn: (A) => R) = {
new com.google.common.base.Function[A, R]{
def apply(input: A): R = fn(input)
}
}
//Yay
class Func[A, B](fn: (A) => B) extends com.google.common.base.Function[A, B] {
1.
(x,y)[5]
= (y, x)[4]
(y,x)[1] = (y,z)[4]
= (x, z)[4]
(x,z)[4]
2.