Skip to content

Instantly share code, notes, and snippets.

View afsalthaj's full-sized avatar
💭
Every day is learning. Every day is the pursuit to perfectness.

Afsal Thaj afsalthaj

💭
Every day is learning. Every day is the pursuit to perfectness.
View GitHub Profile
@afsalthaj
afsalthaj / clojure_macros.md
Last active June 10, 2017 05:35
Clojure Macros

Clojure Macros

((((((((((((((())))))))))))))))))))))))


This is nice and simple!


package tech
trait Mondoid[T] {
def zero: T
def append(a: T, b: T): T
}
object Mondoid {
def apply[A](implicit instance: Mondoid[A]): Mondoid[A] = instance
}
sealed trait Fruits
object Fruits {
case class Banana() extends Fruits
case class Apple() extends Fruits
}
// This could be just FruitsBehaviou[-A]
trait FruitsBehavior[-A <: Fruits] {
def price(a: A): Int
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
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))))
}
// 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 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
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 SuperExchange._
import scalaz.std.string._
import scalaz.syntax.validation._
import SuperExchange._
case class SuperExchange(id: ExchangeId, name: Name)
object SuperExchange {
type ExchangeId = ExchangeId.Type
implicit val validateName: Validation[Name] =
_.successNel[ValidationError]
implicit val validateExchangeId: Validation[ExchangeId]=
_.successNel[ValidationError]