Skip to content

Instantly share code, notes, and snippets.

View chenharryhua's full-sized avatar

Harry Chen chenharryhua

  • Tabcorp
  • Melbourne Australia
View GitHub Profile
package futurable
import scala.concurrent.Future
import concurrent.ExecutionContext.Implicits.global
trait Futurable[In] {
type Out
def apply(in: In): Future[Out]
}
package futurable
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.language.{ higherKinds, implicitConversions }
import scala.util.Try
import scalaz._
import Scalaz._
package futurable
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import scala.language.{ higherKinds, implicitConversions }
import scala.util.Try
import scalaz._
import Scalaz._
package recursion.scheme
import scalaz.Functor
//http://blog.sumtypeofway.com/recursion-schemes-part-iii-folds-in-context/
object Part1 {
sealed trait Expr[A]
case class Ident[A](name: String) extends Expr[A]
case class Index[A](target: A, idx: A) extends Expr[A]
case class Call[A](func: A, args: List[A]) extends Expr[A]
case class Unary[A](op: String, target: A) extends Expr[A]
case class Binary[A](lhs: A, op: String, rhs: A) extends Expr[A]
import scalaz._
import Scalaz._
//scalaz 7.3.0-M4
object MyFreeT extends App {
trait MyFunctor[A]
case class Get() extends MyFunctor[Int]
case class Put(x: Int) extends MyFunctor[String]
type MyOps[A] = Free[MyFunctor, A]
type MyOpT[A] = FreeT[MyFunctor, \/[Throwable, ?], A]
package dsl
import scalaz._
import Scalaz._
object MyOptionFree extends App {
sealed trait ElasticF[T] extends Product with Serializable
final case class Bool[T](seq: Seq[T]) extends ElasticF[T]
final case class Term[T](name: String, value: Option[String]) extends ElasticF[T]
implicit val elasticFunctor = new Functor[ElasticF] {
def map[A, B](fa: ElasticF[A])(f: A => B): ElasticF[B] = fa match {
@chenharryhua
chenharryhua / code inspection.md
Created November 4, 2016 03:48
code inspection

Yesterday, I reviewed my colleague's code which implemented a few tie-break rules to our search api. the code roughly looks like follow(to save space, I removed a few rules). for each tie-break method, it calls direct to the next rule if the condition is not satisfied.

  private def tieBreakerSortWithinESRelevanceGroups(decoratedSearchRequest:DecoratedSearchRequest, leftListing:ListingInfo, rightListing:ListingInfo):Boolean = {
    def tieBreakOnRoiScore(decoratedSearchRequest:DecoratedSearchRequest, leftListing:ListingInfo, rightListing:ListingInfo):Boolean = {
      (decoratedSearchRequest.intent, leftListing.roiScore, rightListing.roiScore) match {
        case (Some(Intent.TYPE), leftRoi, rightRoi) if (leftRoi == rightRoi) => tieBreakOnSource(decoratedSearchRequest, leftListing, rightListing)
        case (Some(Intent.TYPE), leftRoi, rightRoi) => leftRoi < rightRoi
        case _ => tieBreakOnSource(decoratedSearchRequest, leftListing, rightListing)
package yoneda
import language.higherKinds
import scalaz._
import Scalaz._
/**
* A => ? to F is nature transformation, encoded as ~>[(A => ?), F]
* f and g are the witness of the fact that:
* the nature transformation of A => ? to F is isomorphic to F[A]
*/
/**
* typesafe config is our default configuration library. the configuration can be output via the render methods.
* However, sometimes we don't want display the sensitive information to user, such as password.
* this gist is about how to remove sensitive information when output a configuration.
**/
/**
* one solution is that we first render the config and then filter out sensitive information via regex:
*/
config.root().render.replaceAll("(?m)^(\\s+\"password\".*)$", "")
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
object ImplicitInjection {
final class Foo(implicit ec: ExecutionContext){
def foo = Future(1).map(_ + 1)
}
trait Bar {
implicit val ec : ExecutionContext
def bar = Future(1).map(_ + 1)