Skip to content

Instantly share code, notes, and snippets.

View afsalthaj's full-sized avatar

Afsal Thaj afsalthaj

View GitHub Profile
implicit def genericValidation[A, R](
implicit gen: Generic.Aux[A, R], env: Validation[R]
): Validation[A] =
new Validation[A] {
override def validate(b: A) =
env.validate(gen.to(b)).map(gen.from)
}
// Please note that Generic.Aux[A, R] === Generic[A] {type Repr = R}
// as we cannot do env: Validation[gen.Repr]
import shapeless.{HList, ::, HNil }
import scalaz.syntax.applicative._
import scalaz.syntax.either._
import scalaz.syntax.validation._
implicit val hnilEncode: Validation[HNil] =
Validation.createInstance( _.successNel[ValidationError]) //Yea, that's it
implicit def hlistEncoder[H, T <: HList] (
implicit
implicit val validateName: Validation[Name] =
_.successNel[ValidationError]
implicit val validateExchangeId: Validation[ExchangeId]=
_.successNel[ValidationError]
implicit val validateName: Validation[Name] =
_.successNel[ValidationError]
implicit val validateExchangeId: Validation[ExchangeId]=
_.successNel[ValidationError]
import SuperExchange._
import scalaz.std.string._
import scalaz.syntax.validation._
import SuperExchange._
case class SuperExchange(id: ExchangeId, name: Name)
object SuperExchange {
type ExchangeId = ExchangeId.Type
import scalaz.syntax.std.option._
import scalaz.{@@, Show, Tag}
import scalaz.syntax.show._
trait Tagger[A] {
sealed trait Marker
final type Type = A @@ Marker
def apply(a: A): Type = Tag[A, Marker](a)
def unapply(tagged: Type): Option[A] = unwrapped(tagged).some
def unwrapped(tagged: Type): A = Tag.unwrap(tagged)
import scalaz.ValidationNel
trait Validation[A] {
def validate(a: A): ValidationNel[ValidationError, A]
}
object Validation {
implicit def validation[A](implicit a: Validation[A]): Validation[A] = a
// Inspired from shapeless docs
// Thanks to @adam_evans for this.. and cats documentation as well
// This can be a very simple open source... its far better than just about everything out there!
def fromCommandLineArguments(args: List[String]) = {
args.sliding(2, 2).flatMap(t => (
(t.headOption.flatMap(str => str.startsWith("--").option(str.replace("--", ""))) |@|
t.lift(1).flatMap(str => (!str.startsWith("--")).option(str))){(_, _)}).toList).toMap
import Types._
import scala.compat.java8.FutureConverters.toScala
// eh.send returns a completable future in Java. we convert it to Scala future
// and throttle sending the data to eventhub in azure nicely and easily!
private def sendDataInFutureControlled: Seq[EventData] => EventHubClient => Future[Iterator[Void]] =
seq => eh => {
// We throttle it and send in sequence to make sure that we are not overloading eventhub
Future.sequence(seq.grouped(3).map(s => toScala[Void](eh.send(s.asJava))))
}
package com.telstra.dx.iot.bin.generator
import org.scalacheck.Gen
import scala.annotation.tailrec
import scalaz.{-\/, \/, \/-}
import scalaz.syntax.either._
import scalaz.syntax.std.boolean._
// An algebra for a stateful data generation. More or less scalaz.State but not exactly, but focussed on