Skip to content

Instantly share code, notes, and snippets.

Avatar

Trond Bjerkestrand tbje

View GitHub Profile
@tbje
tbje / Shell.scala
Last active Sep 26, 2017
Running a shell command in scala native capturing output
View Shell.scala
import scalanative.native._, stdlib._, stdio._
@extern
object Pipe {
def popen(cmd: CString, mode: CString): Ptr[FILE] = extern
def pclose(file: Ptr[FILE]): Unit = extern
}
object Shell {
def runCommand(cmd: String, cwd: Option[String] = None): Option[String] =
@tbje
tbje / addition.sbt
Last active Feb 28, 2017
addition.sbt
View addition.sbt
import com.typesafe.training.sbtkoan.SbtKoan
SbtKoan.autoImport.configurations := Set(Configurations.Test, Configuration("multi-jvm", "Multi-jvm dir", true, Nil, true))
@tbje
tbje / keybase.md
Last active Jul 28, 2016
keybase.md
View keybase.md

Keybase proof

I hereby claim:

  • I am tbje on github.
  • I am tbje (https://keybase.io/tbje) on keybase.
  • I have a public key ASAKaaSO1Y7nE_TfyvDf1raxzYEXDxvW3vKzeg74psA_Lgo

To claim this, I am signing this object:

@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 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)
View application.conf
akka {
loggers = [akka.event.slf4j.Slf4jLogger]
loglevel = "DEBUG"
actor {
provider = akka.remote.RemoteActorRefProvider
}
remote {
enabled-transports = [akka.remote.netty.tcp]
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 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 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 ---^
}
@tbje
tbje / ensimeWithSources.scala
Last active Dec 27, 2015
Allows you to look at sources of dependencies in ensime. Feel free to transform this into a sbt plugin.
View ensimeWithSources.scala
// After having run eclipse with-sources=true from sbt (with sbteclipse) .classpath should contain sourcepaths:
val eclippseCp = scala.xml.XML.load(".classpath")
val entries = eclippseCp.child filter { _.label == "classpathentry" }
val sources = entries flatMap { _.attributes filter { _.key=="sourcepath" }} map { _.value.text }
val destDir = "~/ensime-sources" // feel free to modify this
def fileName(file: String) = new java.io.File(file).getName.replace("-sources.jar", "")
import java.io.File