Skip to content

Instantly share code, notes, and snippets.

@tomer-ben-david
tomer-ben-david / gist:9760715
Created March 25, 2014 12:16
slf4j scala array printing
LOG.debug("[mymofulr][{}], response [{}]", Array(1, 2).asInstanceOf[Array[Object]])
// Note if you don't do the asInstanceOf[Array[Object]] it would simply print the Array.toString as the first item.
@tomer-ben-david
tomer-ben-david / gist:9911660
Created April 1, 2014 10:41
"Enum" Scala way
sealed trait Currency { def name: String }
case object EUR extends Currency { val name = "EUR" } //etc.
case class UnknownCurrency(name: String) extends Currency
So now I have the advantage of...
trade.ccy match {
case EUR =>
case UnknownCurrency(code) =>
}
@tomer-ben-david
tomer-ben-david / gist:1f2611db1d0851a65d43
Created May 27, 2014 10:36
write bytes to file in scala
val byteArray: Array[Byte] = Array(1,2)
val bos = new BufferedOutputStream(new FileOutputStream(filename))
Stream.continually(bos.write(byteArray))
bos.close() // You may end up with 0 bytes file if not calling close.
import org.scalatest._
import akka.testkit._
import org.junit.runner.RunWith
import org.scalatest.junit.JUnitRunner
import akka.actor.ActorSystem
@RunWith(classOf[JUnitRunner])
class MyActor$Test extends FlatSpec with TestKitBase with ShouldMatchers {
implicit lazy val system = ActorSystem()
@tomer-ben-david
tomer-ben-david / gist:57a29acb4b49e87d11fc
Created July 14, 2014 08:43
Scalatest akka actor testkit
// Your scalatest shoudl extend
extends TestKit(ActorSystem("test"))
// Then create actor as following
val mockedActorRef: ActorRef = TestActorRef(new ModelsRepositoryFetcherActor(MyCaseClassParam("a")))
@tomer-ben-david
tomer-ben-david / scala regular expression
Created October 2, 2014 05:36
scala regular expression
scala> "[,.\\W]".r findAllIn "hi ther. how," foreach {println _}
.
,
@tomer-ben-david
tomer-ben-david / scala.slick.clob.column.scala
Created December 28, 2014 06:23
Define a CLOB column with typesafe slick
def someBigText: Column[String] = column[String]("MY_FIELD_NAME", O.DBType("CLOB"))
@tomer-ben-david
tomer-ben-david / scalacheck.long.frequencies.scala
Last active August 29, 2015 14:12
scalacheck generate sample of longs with different frequencies
val arbLong: Gen[Long] = {
Gen.frequency((20, Arbitrary.arbDouble), (1, Long.MaxValue)).sample.get.arbitrary
}
@tomer-ben-david
tomer-ben-david / scalacheck.generate.complex.class.scala
Created January 4, 2015 10:17
scalacheck generate a complex class
case class MyCaseClass(e: SomeEnum, l: Long, d: SomeDouble)
def ratiosGen: Gen[MyCaseClass] = for {
e <- Gen.oneOf(Seq(State.ALWAYS, State.NONE, State.NEVER, null)) // one of the items.
l <- Arbitrary.arbitrary[Long]
d <- Arbitrary.arbitrary[Double]
} yield MyCaseClass(e, l, d)
@tomer-ben-david
tomer-ben-david / scalacheck.generate.list.of.objects.scala
Created January 4, 2015 10:50
generate list of objects with scalacheck
val listOfObjectsGen: Gen[List[MyCaseClass]] =
Gen.containerOf[List, MyCaseClass](myCaseClassGen)