Skip to content

Instantly share code, notes, and snippets.

View speedcom's full-sized avatar
🏠
Working from home

Mateusz Maciaszek speedcom

🏠
Working from home
View GitHub Profile
/**
* Part Zero : 10:15 Saturday Night
*
* (In which we will see how to let the type system help you handle failure)...
*
* First let's define a domain. (All the following requires scala 2.9.x and scalaz 6.0)
*/
import scalaz._
import Scalaz._
@speedcom
speedcom / Example.scala
Last active August 29, 2015 14:18 — forked from noelwelsh/Example.scala
error-handling-web-app
import scalaz.\/
import scalaz.syntax.either._
object Example2 {
// This example simulates error handling for a simple three tier web application
//
// The tiers are:
// - the HTTP service
// - a user authentication layer
// - a database layer
@speedcom
speedcom / Path-Dependent-Types
Last active August 29, 2015 14:24
Scala's Path-Dependent-Types
// 1 example
class A(name: String) {
case class B(nr: Int)
def add(first: B, second: B): Int = first.nr + second.nr
}
val a1 = new A(name = "first")
val a2 = new A(name = "second")
@speedcom
speedcom / Context Bound
Created July 4, 2015 12:35
Scala Context Bound Examples
trait Acceptable[T]
object Acceptable {
implicit object LongOK extends Acceptable[Long]
implicit object IntOK extends Acceptable[Int]
}
object A {
def f[T : Acceptable](t : T) = t
@speedcom
speedcom / Phantom-Types + F-bounded-types
Last active August 29, 2015 14:24
Scala Phantom-Types + F-bounded-types
// SCALA PHANTOM TYPES EXAMPLES
// 1 SIMPLE EXAMPLE
trait DoorStatus
trait DoorOpened extends DoorStatus
trait DoorClosed extends DoorStatus
case class Door[Status <: DoorStatus] private () {
def close[T >: Status <: DoorOpened](): Door[DoorClosed] = Door[DoorClosed]()
@speedcom
speedcom / View Bound
Last active September 30, 2015 13:38
implicit conversion from one type to another one
// implicit conversion from one type to another one
// 1 way
object ViewBound {
trait Show {
def show: String
}
object Show {
package Boot
import akka.actor.{Cancellable, Actor, ActorSystem, Props}
import akka.stream.{OverflowStrategy, ActorFlowMaterializer}
import akka.stream.actor.ActorSubscriberMessage.{OnComplete, OnNext}
import akka.stream.actor.{ActorSubscriber, OneByOneRequestStrategy, RequestStrategy}
import akka.stream.scaladsl._
import org.akkamon.core.exporters.StatsdExporter
import org.akkamon.core.instruments.{CounterTrait, LoggingTrait, TimingTrait}
object ReadSample extends App {
/**
* The readable trait defines how objects can be converted from a string
* representation to the objects instance. For most of the standard types
* we can simply use the toType function of String.
*/
trait Readable[T] {
def read(x: String): T
}
@speedcom
speedcom / monad.scala
Created October 17, 2015 19:45
Scala Monad For-Comprehension
/** A for comprehension ready Monad */
case class Focomo[A](value: A) {
self =>
/** satisfies monadic binding of a function f that returns B */
def map[B](f: A => B): Focomo[B] = {
println("map!");
Focomo(f(value))
}
/** satisfies monadic binding of a function f that returns Focomo[B] */
def flatMap[B](f: A => Focomo[B]): Focomo[B] = {
@speedcom
speedcom / ScheduledExecutor.scala
Created January 11, 2016 22:29 — forked from platy/ScheduledExecutor.scala
Scala wrapper around java's ScheduledExecutorService
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy
import java.util.concurrent._
import scala.concurrent.{ Promise, Future }
import scala.concurrent.duration.FiniteDuration
import scala.language.implicitConversions
import scala.util.Try
object ScheduledExecutor {
private val defaultHandler: RejectedExecutionHandler = new AbortPolicy