Skip to content

Instantly share code, notes, and snippets.

View tbje's full-sized avatar

Trond Bjerkestrand tbje

View GitHub Profile
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 ---^
}
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)
@tbje
tbje / pre-commit
Last active August 29, 2015 14:01
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
akka {
loggers = [akka.event.slf4j.Slf4jLogger]
loglevel = "DEBUG"
actor {
provider = akka.remote.RemoteActorRefProvider
}
remote {
enabled-transports = [akka.remote.netty.tcp]
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 June 25, 2015 16:09
Feeding animals
package misc
// Animal
abstract class Animal {
def name: String
def eat(food: Food): Animal =
this
def render = {<a onclick={Alert(Call(myFunction, 4, 5))}>Click me</a>}
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))
/* 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))
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._