Skip to content

Instantly share code, notes, and snippets.

View Centaur's full-sized avatar

oldpig Centaur

  • singerdream.com
  • Shanghai
View GitHub Profile
@Centaur
Centaur / MergedSubject.scala
Created April 29, 2016 11:16
truncate on subscribe
package org.snippets
import rx.lang.scala._
import rx.lang.scala.subjects.ReplaySubject
import scala.concurrent.Promise
import scala.concurrent.ExecutionContext.Implicits.global
object MergedSubject {
@Centaur
Centaur / RegexMatch.scala
Last active February 1, 2016 05:29
find by regex
package org.snippets
object RegexMatch {
val LOCATION_JU_MAIN_CAT_PREFIX = "a"
val LOCATION_JU_WAP_CAT_PREFIX = "b"
val LOCATION_JU_TAOBAO_CAT_PREFIX = "c"
val LOCATION_JU_TMALL_CAT_PREFIX = "d"
val juCatRE = s"$LOCATION_JU_MAIN_CAT_PREFIX([0-9]+)".r
@Centaur
Centaur / flatten.scala
Created December 8, 2015 05:47
Shapeless tuple flatten
import shapeless._
import poly._
import shapeless.ops.tuple.FlatMapper
import syntax.std.tuple._
object Flatten {
trait LowPriorityFlatten extends Poly1 {
implicit def default[T] = at[T](Tuple1(_))
}
object flatten extends LowPriorityFlatten {
@Centaur
Centaur / redis.markdown
Created December 7, 2015 16:02 — forked from bdotdub/redis.markdown
Running redis using upstart on Ubuntu

Running redis using upstart on Ubuntu

I've been trying to understand how to setup systems from the ground up on Ubuntu. I just installed redis onto the box and here's how I did it and some things to look out for.

To install:

@Centaur
Centaur / yadsun.scala
Created November 16, 2015 05:42
Idiomatic Scala
object Yadsun {
trait SubColumnStatus
case object Disabled extends SubColumnStatus
case object Enabled extends SubColumnStatus
type SubColumns = Map[String, SubColumnStatus]
case class SysColumn(columnId: Int, columnText: String, subColumns: SubColumns) {
def addSubColumn(text: String) = commonOp(text)(_ => subColumns, txt => subColumns.updated(txt, Enabled))
@Centaur
Centaur / MonadAndSyntax.scala
Last active November 12, 2015 08:41
Monad & Syntax
object HKind {
trait Monad[M[_]] {
def flatMap[T, U](m: M[T], f: T => M[U]): M[U]
def map[T, U](m: M[T], f: T => U): M[U]
}
implicit class MonadSyntax[X, M[_] : Monad](sx: M[X]) {
def flatMap[U](f: X => M[U]): M[U] = implicitly[Monad[M]].flatMap(sx, f)
@Centaur
Centaur / PlayJsonVersion.scala
Created November 11, 2015 05:29
play-json to Argonaut
import java.io.NotSerializableException
import play.api.libs.json._
/**
* Created by xiefei on 15/11/11.
*/
trait Jsonable
case class Config(name: String)
@Centaur
Centaur / HListSequence.scala
Last active November 18, 2015 11:59
HList Sequence
import scalaz.{idInstance => _, _}
import std.scalaFuture._
import syntax.applicative._
import shapeless._
import UnaryTCConstraint.*->*
import poly._
import ops.hlist._
object HListSequence {
@Centaur
Centaur / Problem.md
Last active May 19, 2018 05:08
Scala vs Clojure

Treat or Trick

Everyone knows what Halloween is and how children love it. Children in costumes travel from house to house asking for treats with a phrase "Trick or treat". After that, they divide the treats equally among all. This year, they collected tons of candies, and need your help to share everything equally. You know that children receive different number of candies depending on their costume: vampire gets 3 candies from one house, zombie – 4 candies, and witch – 5 candies. That is, three children in three different costumes get 3+4+5=12 candies from one house.

Input sample:

The first argument is a path to a file. Each line includes a test case with number of vampires, zombies, witches, and houses that they visited.

For example:

@Centaur
Centaur / lens.scala
Created October 21, 2015 15:05
Nested Lens
import monocle.macros.Lenses
import language.higherKinds
@Lenses("_") case class Street(name: String)
@Lenses("_") case class Address(street: Street)
@Lenses("_") case class Company(addresses: Seq[Address])
@Lenses("_") case class Employee(company: Company)
object ILens {
val employee = Employee(Company(Seq(