Skip to content

Instantly share code, notes, and snippets.

Avatar

Trond Bjerkestrand tbje

View GitHub Profile
View tupled.scala
case class User(name: String, email: String)
object User {} // <--- Notice this part here
// https://groups.google.com/forum/#!topic/scala-user/jyWBMz5Qslw
class Users(tag: Tag) extends Table[User](tag, "User") {
def name = column[String]("name", O.NotNull, O.PrimaryKey)
def email = column[String]("first", O.NotNull)
def * = (name, email) <> (User.tupled, User.unapply)
// Compile complains here ---^
}
View Classes.scala
object Trond {
val age = 25 // value - immutable variable - final in java
def sayHi = println(s"My name is Trond and I'm $age years old!") // definition - method
}
// Trond.age = 27 does not compile as age is immutable (val)
Trond.sayHi
object Eli { // singleton object - only one of these in the JVM
var age = 22 // variable (mutable)
View pre-commit
if test $(sbt test | grep -c -E "Reformatted|Failed") != 0
#if test $(sbt compile | grep -c -E "Reformatted|Failed") != 0
#if test $(sbt ";scalariformFormat;test:scalariformFormat" | grep -c -E "Reformatted|Failed") != 0
then
echo "Sbt detected problems"
exit 1
fi
View application.conf
akka {
loggers = [akka.event.slf4j.Slf4jLogger]
loglevel = "DEBUG"
actor {
provider = akka.remote.RemoteActorRefProvider
}
remote {
enabled-transports = [akka.remote.netty.tcp]
View FullTextSearch.scala
case class Match(cols: Column[_]*) {
def against(what: Column[_], modifier: SearchModifier.Value*) =
SimpleExpression[Boolean]{ case (nodes, qb) =>
import qb._
qb.sqlBuilder += "MATCH("
qb.sqlBuilder.sep(nodes, ", ")(expr(_))
qb.sqlBuilder += ") AGAINST("
qb.expr(what.toNode)
qb.sqlBuilder += " "
qb.sqlBuilder.sep(modifier, " ")(_.toString)
@tbje
tbje / animals.scala
Created Jun 25, 2015
Feeding animals
View animals.scala
package misc
// Animal
abstract class Animal {
def name: String
def eat(food: Food): Animal =
this
View better.scala
def render = {<a onclick={Alert(Call(myFunction, 4, 5))}>Click me</a>}
View comet.scala
val listFromDb : Box[List[String]]= ... should collect data from db, but throws exception ...
def render = {
listFromDb match {
case None => <p>No items.</p>
case Some(items) =>
bind(
"myComet", defaultXml,
"content" -> transforListToXhtml(list))
View uniqueHash.scala
/* http://tbje.blogspot.com/2010/04/random-unique-hash-in-scala.html */
import scala.util.Random
def uniqueRandomKey(chars: String, length: Int, uniqueFunc: String=>Boolean) : String = {
val newKey = (1 to length).map(x => chars(Random.nextInt(chars.length))).mkString
if (uniqueFunc(newKey)) newKey else uniqueRandomKey(chars, length, uniqueFunc)
}
val chars = ('a' to 'z') ++ ('A' to 'Z') ++ ('1' to '9')
val key = uniqueRandomKey(chars.toString, 22, (x:String)=>!keyExists?(x))
View CaptchaSnippet.scala
import net.liftweb.http._
import net.liftweb.http.SHtml._
import net.liftweb.util._
import net.liftweb.util.Helpers._
import net.liftweb.common._
import java.net.{HttpURLConnection, MalformedURLException, URL, URLEncoder}
import java.io.{BufferedReader, InputStreamReader, IOException, OutputStreamWriter}
import scala.xml._