Skip to content

Instantly share code, notes, and snippets.

View kailuowang's full-sized avatar

Kai(luo) Wang kailuowang

View GitHub Profile
@kailuowang
kailuowang / gist:1169564
Created August 24, 2011 23:20
javascript Example of click event handle
var Note = Backbone.Model.extend({
initialize: function() {},
delete: function() {
//do something
this.log('deleted');
},
log: function(msg) {
alert(msg);
@kailuowang
kailuowang / gist:c73a70c9194e662163e8
Last active August 29, 2015 14:26
Functional Asyn Example
def createOrUpdateCard( profileId: ProfileId, contextName: ContextName): FutureEither[CardId] = {
lazy val nowF = getAndValidateGoogleNowCredential(profileId)
lazy val contentF = getContentFromStationService(profileId, contextName)
lazy val contextF = ensureContextExistsOnGoogle(contextName)
def createCard = {
for {
now <- nowF
content <- contentF
package com.iheart.play.akka
import org.specs2.mutable.Specification
import shapeless._
import ops.hlist._
import shapeless.ops.record.{Values, Keys}
import syntax.singleton._
object Test {
case class RawReq(ip: String, header: Map[String, String] = Map())
val endpoint0 = handle(
fromJson[PartialRequestMessage].body and from(req ⇒ 'name ->> req.headers("my_name") :: HNil),
process[RequestMessage] using myActor `then` expect[ResponseMessage].respondJson(Ok(_))
)
val endPoint1 = handleParams(
process[RequestMessage] using myActor `then` expect[ResponseMessage].respond(Ok) `with` authentication
)
object overloaded extends Overload {
implicit def futureT[T] =
Case((t: Future[T]) => t.map(Right(_))
implicit def futureOptT[T, U](implicit ev: U <:< Option[T]) =
Case((t: Future[U]) => t.map(Right(_.get)).getOrElse(Left(NotFoundException))
}
@kailuowang
kailuowang / abvsTypeDep.scala
Created December 21, 2015 22:34
Abstract Type vs Type parameter
trait AnimalMouth
class Beak extends AnimalMouth
//================================Generic type works as in the following =======================
trait Animal[Mouth <: AnimalMouth] {
def mouth : Mouth
def feedAnother(m: Mouth): Unit = ()
}
@kailuowang
kailuowang / processes.scala
Created January 1, 2016 02:18
Processor Data Structure
object Test {
type Cause = String
type FutureXor[A] = XorT[Future, Cause, A]
case class ProcessContext[+A](context: List[String], data: A) //context never need to change
trait Processor[-A, B] extends Serializable {
def process: ProcessContext[A] ⇒ FutureXor[B]
@kailuowang
kailuowang / AvroDeserializationSchema.scala
Last active November 4, 2019 13:55
Avro deserializer for Flink's Data Stream API Kafka Source
import org.apache.avro.io.DatumReader
import org.apache.avro.io.DecoderFactory
import org.apache.avro.specific.SpecificDatumReader
import org.apache.flink.api.common.typeinfo.TypeInformation
import org.apache.flink.api.java.typeutils.TypeExtractor
class AvroDeserializationSchema[T <: SpecificRecordBase: ClassTag](private val baselineMessage: () ⇒ T) extends DeserializationSchema[T] {
override def deserialize(message: Array[Byte]): T = {
import cats.{Applicative, Unapply}
import cats.instances.all._
import shapeless._
object Test {
trait Foo[L <: HList]
object Foo extends MkFoo
@kailuowang
kailuowang / HFunctors.md
Last active May 12, 2017 01:15
Two tales of high order functors.

There are two possible definitions of a somewhat higher order Functors: HFunctorA:

trait HFunctorA[H[_[_],_]] {
  def map[F[_], G[_], A](h: H[F, A])(f: F ~> G): H[G, A]
}

and HFunctorB:

trait HFunctorB[H[_[_]]] {