Skip to content

Instantly share code, notes, and snippets.

View longliveenduro's full-sized avatar

Chris Wewerka longliveenduro

View GitHub Profile
import scala.util.Try
/**
* Alternative to unsound construct: "sealed abstract case class"
* see https://gist.github.com/tpolecat/a5cb0dc9adeacc93f846835ed21c92d2
* The solution below is the summary of the comments of the above gist.
*
* Works as is in Scala 2.12 and higher
* In Scala 2.11.12 you have to add -Xsource:2.12
*/
/**
* A Red / Green Tree.
*/
case class RedGreenTree {
}
@longliveenduro
longliveenduro / ExtractFields.scala
Created September 13, 2017 08:25
Kafka Connect Custom Transformation ExtractFields
package net.gutefrage.connector.transforms
import java.util
import org.apache.kafka.common.config.ConfigDef
import org.apache.kafka.connect.connector.ConnectRecord
import org.apache.kafka.connect.data.{Schema, SchemaBuilder, Struct}
import org.apache.kafka.connect.transforms.Transformation
import org.apache.kafka.connect.transforms.util.Requirements.{requireMap, requireStruct}
import org.apache.kafka.connect.transforms.util.SimpleConfig
@longliveenduro
longliveenduro / RichJsLookupResult.scala
Created May 4, 2017 15:36
Converting Play Json's JsLookupResult to Scalactic's Good Or Bad
import org.scalactic._
import play.api.libs.json.{JsError, JsLookupResult, JsSuccess, Reads}
object RichJsLookupResult {
implicit class RichPlayJsonValidateToGoodOrBad(lookupResult: JsLookupResult) {
def parseAs[T](implicit rds: Reads[T]): T Or Every[String] = {
lookupResult.validate[T] match {
case s: JsSuccess[T] => Good(s.get)
object CollectDemo {
val listOfTrys: List[Try[String]] = List(Success("apple"), Failure(new Exception("some failure")), Success("peach"), Success("strawberry"))
val listOfExistingFruits: List[String] = listOfTrys.collect {
case Success(name) => name
}
// prints out: List(apple, peach, strawberry)
println(listOfExistingFruits)
}
implicit class RichView[V <: android.view.View](view: V) {
def onClick[U](f: android.view.View => U): V = {
view.setOnClickListener(new android.view.View.OnClickListener {
def onClick(p: android.view.View): Unit = f(p)
})
view
}
}
case class User(id: Int, name: String, avatarUrl: Option[String])
val kevin = User(1, "Kevin", None)
val jeremy = User(1, "Jeremy", Some("http://someurl.de"))
val users = Seq(kevin, jeremy)
users.forEach { // iterate over the user sequence
user => user match { // match every entry in the value 'user'
case User(name, Some(url)) =>
public class User {
private int id;
private String name;
public int getId() {
return id;
}
public String getName() {
return name;
case class User(id: Int, name: String)
button.onClick(openUri("http://wikipedia.org"))