Skip to content

Instantly share code, notes, and snippets.

View asflierl's full-sized avatar

Andreas Flierl asflierl

View GitHub Profile
asflierl / Persistence.scala
Created July 24, 2018 09:05
just another day in Scala...
private type H[A <: BSONValue, B] = BSONHandler[A, B]
private type DH[A] = BSONDocumentHandler[A]
object Implicits extends BSONConverter with DaoToDocDerivation {
private implicit def `(A, B) <-> BSON`[A <: BSONValue, B, C <: BSONValue, D](implicit hab: H[A, B], hcd: H[C, D]): DH[(B, D)] = handler[Tuple2[B, D]]
private implicit def `Coll[A] <-> BSON`[A[_], B](implicit h: H[_ <: BSONValue, B], cbf: CanBuildFrom[A[_], B, A[B]], ev: A[B] <:< Traversable[B]): H[BSONArray, A[B]] =
BSONHandler(bsonArrayToCollectionReader[A, B].read, collectionToBSONArrayCollectionWriter[B, A[B]].write)
private implicit def `Map[A, B] <-> BSON`[A, B](implicit htab: DH[(A, B)]): H[BSONArray, Map[A, B]] =
`Coll[A] <-> BSON`[Vector, (A, B)].as(_.toMap, _.toVector)
private implicit def `Set[A] <-> BSON`[A](implicit htab: H[_ <: BSONValue, A]): H[BSONArray, Set[A]] =
asflierl / REPL
Created July 17, 2018 12:15
Welcome to Scala 2.12.6 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_162).
Type in expressions for evaluation. Or try :help.
scala> case class Meep(msb: Long, lsb: Long)
defined class Meep
scala> case class Moop(value: Meep) extends AnyVal
defined class Moop
scala> case class Whee(moop: Moop, whoop: String)
import java.util.Locale;
public enum EBoolA {
public static final int fromBool(Boolean b) {
for (EBoolA mb : values()) {
StringBuilder sb = new StringBuilder();
[error] Unexpected internal error near index 1
[error] \
[error] ^
[error] java.util.regex.Pattern.error(
[error] java.util.regex.Pattern.compile(
[error] java.util.regex.Pattern.<init>(
[error] java.util.regex.Pattern.compile(
[error] java.lang.String.split(
[error] java.lang.String.split(
Welcome to Scala 2.11.8 (Java HotSpot(TM) 64-Bit Server VM, Java 1.7.0_80).
Type in expressions for evaluation. Or try :help.
scala> object Meep { val meep = "meep" }
defined object Meep
scala> class Moop { def x = null.asInstanceOf[Meep.type].meep }
defined class Moop
scala> new Moop().x
scala> val argl = collection.mutable.HashSet[Int]()
argl: scala.collection.mutable.HashSet[Int] = Set()
scala> val bargl = collection.mutable.HashSet[Int]()
bargl: scala.collection.mutable.HashSet[Int] = Set()
scala> (1 to 100).foreach(argl += _)
scala> argl.foreach { n => if (n % 10 == 0) argl += n * 100; bargl += n }
scala> null.asInstanceOf[].AudioSource.MIC
res0: Int = 1
class Meep[A, B](b: B) { def meep(a: A, f: B => B): Option[(B, B)] = Some(b, f(b)) }
class Moop extends Meep[Int, String]("meep") { def meep(a: Int)(f: String => String): Option[String] = super.meep(a, f).map(_._2) }
println(new Moop().meep(42, identity).map(_._2)) //witness ClassCastException
implicit class RichTraversable[A, C[B] <: TraversableLike[B, C[B]]](val wrapped: C[A]) extends AnyVal {
def splitBy(p: A => Boolean)(implicit cb: CanBuild[Either[C[A], C[A]], C[Either[C[A], C[A]]]]): C[Either[C[A], C[A]]] = {
val builder = cb()
@tailrec def spanNext(as: C[A]): Unit =
if (as.nonEmpty) {
val ph = p(as.head)
val (append, remaining) = as.span(a => p(a) == ph)
builder += (if (ph) Right(append) else Left(append) : Either[C[A], C[A]])
import org.scalacheck._, Prop.forAll, Gen._, Arbitrary._
object MeepSpec extends Properties("Meep") {
case class Meep(id: Int, name: String)
implicit lazy val arbMeep: Arbitrary[Meep] = Arbitrary(for (i <- arbitrary[Int]; n <- alphaStr) yield Meep(i, n))
// using ScalaCheck 1.12.5, this outlandish property would have passed:
property("meep1") = forAll { (f: Meep => Meep, m1: Meep, m2: Meep) => f(m1) == f(m2) }
// using ScalaCheck 1.13.0, we have to define how function input influences function output via an instance of Cogen: