Skip to content

Instantly share code, notes, and snippets.

@d6y
d6y / OSAAT1.md
Last active August 29, 2015 14:08
Pipe

Scalaz: one symbol at a time (OSAAT#1)

Symbol

|> or

Pronounced

  • Pipe
  • The Thrush combinator
@d6y
d6y / symbols
Last active August 29, 2015 14:08
scalaz symbols
scalaz (series/7.2.x)$ grep -R 'def [^A-Za-z_]' core effect concurrent iteratee
core/src/main/scala/scalaz/Arrow.scala: final def <<<[A, B, C](fbc: (B =>: C), fab: (A =>: B)): =>:[A, C] =
core/src/main/scala/scalaz/Arrow.scala: def >>>[A, B, C](fab: (A =>: B), fbc: (B =>: C)): (A =>: C) =
core/src/main/scala/scalaz/BijectionT.scala: def ***[C, D](g: Bijection[C, D])(implicit evF: F[B] =:= Id[B], evG: G[A] =:= Id[A]): Bijection[(A, C), (B, D)] =
core/src/main/scala/scalaz/BijectionT.scala: def ^^^[C, D](g: Bijection[C, D])(implicit evF: F[B] =:= Id[B], evG: G[A] =:= Id[A]): Bijection[A \/ C, B \/ D] =
core/src/main/scala/scalaz/BijectionT.scala: def <=<[C](that: BijectionT[F, G, C, A])(implicit FM: Bind[F], GM: Bind[G]): BijectionT[F, G, C, B] = compose(that)
core/src/main/scala/scalaz/BijectionT.scala: def >=>[C](that: BijectionT[F, G, B, C])(implicit M: Bind[F], GM: Bind[G]): BijectionT[F, G, A, C] = andThen(that)
core/src/main/scala/scalaz/Cofree.scala: final def =>>[B](f: Cofree[S, A] => B)(implicit
@d6y
d6y / popspec.scala
Last active August 29, 2015 14:08
populate
implicit val pc = PopulationControl(
dataSource=postgressDs,
...customzation here)
"Friends database" should {
populate("""
# User
@d6y
d6y / moverload.scala
Created November 1, 2014 17:23
Enumerations & Method Overload
scala> :paste
// Entering paste mode (ctrl-D to finish)
object Colours extends Enumeration {
val Red, Amber, Green = Value
}
object WeekDays extends Enumeration {
val Mon,Tue,Wed,Thu,Fri = Value
}
@d6y
d6y / epatmat.scala
Created November 1, 2014 17:24
Enumerations & Pattern Matching
scala> :paste
// Entering paste mode (ctrl-D to finish)
def traffic(colour: Colours.Value) = colour match {
case Colours.Green => "Go"
}
// Exiting paste mode, now interpreting.
traffic: (colour: Colours.Value)String
@d6y
d6y / simple.scala
Created November 1, 2014 17:24
Enumerations & Simple Sealed Traits and Objects
object WeekDay {
sealed trait EnumVal
case object Mon extends EnumVal
case object Tue extends EnumVal
case object Wed extends EnumVal
case object Thu extends EnumVal
case object Fri extends EnumVal
val daysOfWeek = Seq(Mon, Tue, Wed, Thu, Fri)
}
@d6y
d6y / planets.scala
Created November 1, 2014 17:25
Enuerations and case objects
object SolarSystemPlanets {
sealed abstract class Planet(
val orderFromSun : Int,
val name : String,
val mass : Kilogram,
val radius : Meter) extends Ordered[Planet] {
def compare(that: Planet) = this.orderFromSun - that.orderFromSun
@d6y
d6y / using.scala
Created November 1, 2014 17:26
Enuerations and case objects: usage
scala> import SolarSystemPlanets._
import SolarSystemPlanets._
scala> println(planets)
TreeSet(Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune)
scala> EARTH < MARS
res1: Boolean = true
scala> planets.filter(_.radius > 7.0e6)
@d6y
d6y / CustomResourceFilter.scala
Last active August 29, 2015 14:09
Re-writing in the container
package code.filter
import javax.servlet._
import http._
import java.util.regex._
// A wrapper around an HTTP Servlet Request which removes any /cached/:id from a URI
case class ResouceRequest(req: HttpServletRequest) extends HttpServletRequestWrapper(req) {
// The URI without the /cached/:id part:
@d6y
d6y / notify.scala
Created November 19, 2014 07:48
Notify example
import play.api.libs.json._
case class Notify(email: Option[String] = None)
implicit val reader = Json.reads[Notify]
Json.parse(""" { "email": "[email protected]" } """).asOpt[Notify]
// res0: Option[Notify] = Some(Notify(Some([email protected])))
Json.parse(""" { } """).asOpt[Notify]
// res1: Option[Notify] = Some(Notify(None))