This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// typeclass framework (really a Monoid + SemiGroup) | |
trait Num[A] { | |
val zero: A | |
def add(x: A, y: A): A | |
} | |
// provide default typeclass implementations in the companion object so when used | |
// it brings some default typeclass implementations with it | |
object Num { | |
// object way |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.language.higherKinds | |
// Functor typeclass - it abstracts over higher kinded types (functions at the type level) | |
trait Functor[HigherKindedType[_]] { | |
def map[A, B](functorA: HigherKindedType[A])(mapper: A => B): HigherKindedType[B] | |
def unit[A](simpleA: => A): HigherKindedType[A] | |
} | |
// Monad typeclass - it also abstracts over higher kinded types (functions at the type level) | |
// A Monad is also a Functor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scala.language.higherKinds | |
// Functor typeclass - it abstracts over higher kinded types (functions at the type level) | |
trait Functor[HigherKindedType[_]] { | |
def map[A, B](functorA: HigherKindedType[A])(mapper: A => B): HigherKindedType[B] | |
def unit[A](simpleA: => A): HigherKindedType[A] | |
} | |
object Functor { | |
// A Functor typeclass implementation for the List Higher Kinded Type (function at the type level) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazonaws.services.sqs | |
import org.json4s.NoTypeHints | |
import org.json4s.ext.{JavaTypesSerializers, JodaTimeSerializers} | |
import org.json4s.jackson.Serialization | |
import scala.collection.JavaConverters._ | |
import scala.util.Try | |
case class SQSMessage[T](messageId: String, receiptHandle: String, md5OfBody: String, body: T, | |
attributes: Map[String, String], md5OfAttributes: String) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain | |
import com.amazonaws.services.sqs.AmazonSQSAsyncClient | |
import com.amazonaws.services.sqs.model.{ReceiveMessageResult, SendMessageResult} | |
import com.github.dwhjames.awswrap.sqs.AmazonSQSScalaClient | |
import scala.collection.JavaConverters._ | |
import scala.concurrent.{Await, Future} | |
import scala.language.postfixOps | |
import scala.concurrent.duration._ | |
implicit val ec = scala.concurrent.ExecutionContext.global |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Setup | |
val rejectionHandler = RejectionHandler.default | |
val logDuration = extractRequestContext.flatMap { ctx => | |
val start = System.currentTimeMillis() | |
// handling rejections here so that we get proper status codes | |
mapResponse { resp => | |
val d = System.currentTimeMillis() - start | |
logger.info(s"[${resp.status.intValue()}] ${ctx.request.method.name} ${ctx.request.uri} took: ${d}ms") | |
resp | |
} & handleRejections(rejectionHandler) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def sort[T](input: List[T])(implicit ordering: Ordering[T]): List[T] = { | |
def inner(input: List[T], output: List[T]): List[T] = | |
(input, output) match { | |
case (Nil, out) => out | |
case (head :: tail, Nil) => inner(tail, head :: Nil) | |
case (iHead :: iTail, oHead :: oTail) if ordering.lteq(iHead, oHead) => | |
inner(iTail, iHead :: oHead :: oTail) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cats.syntax.applicative._ | |
import cats.syntax.flatMap._ | |
import cats.syntax.functor._ | |
import cats.syntax.monoid._ | |
import cats.{Applicative, Id, Monad, Monoid, ~>} | |
import scala.concurrent._ | |
import scala.language.higherKinds | |
implicit val ec = scala.concurrent.ExecutionContext.Implicits.global |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def mergeSort[T](list: List[T])(implicit ordering: Ordering[T]): List[T] = { | |
def merge(l: List[T], r: List[T]): List[T] = | |
(l, r) match { | |
case (lHead:: lTail, rHead :: rTail) if ordering.lteq(lHead, rHead) => lHead :: merge(lTail, r) | |
case (lHead:: lTail, rHead :: rTail) => rHead :: merge(l, rTail) | |
case (x, Nil) => x | |
case (Nil, y) => y |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
"use strict"; | |
async function example(x) { | |
const y = await Promise.resolve(1); | |
try { | |
await Promise.reject(new Error("muahaha")); | |
} catch (e) { | |
console.log(e); | |
} | |
return x + y; |
OlderNewer