Skip to content

Instantly share code, notes, and snippets.

@kovacshuni
Created January 27, 2020 10:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kovacshuni/56f2e56f500361a72c36623edec38e47 to your computer and use it in GitHub Desktop.
Save kovacshuni/56f2e56f500361a72c36623edec38e47 to your computer and use it in GitHub Desktop.
typeclasses-example
package com.hiya.h4b.cpas.service
import java.time.Instant
import java.time.Instant.now
import akka.http.scaladsl.model.StatusCode
import akka.http.scaladsl.model.StatusCodes.{Created, NotFound, OK}
object Typeclasses extends App {
trait Statusable[T] {
def isSuccess(t: T): Boolean
}
object Statusable {
def apply[T](implicit instance: Statusable[T]): Statusable[T] = instance
}
implicit def statusableInstant: Statusable[Instant] = new Statusable[Instant] {
override def isSuccess(i: Instant): Boolean = i.isAfter(now())
}
implicit def statusableStatusCode: Statusable[StatusCode] = new Statusable[StatusCode] {
override def isSuccess(s: StatusCode): Boolean = s.isSuccess()
}
def getCommonStatus[T: Statusable](statusebles: Set[T]): Boolean =
statusebles.forall(Statusable[T].isSuccess(_))
println(getCommonStatus(Set[StatusCode](OK, Created)))
println(getCommonStatus(Set[StatusCode](NotFound)))
println(getCommonStatus(Set(now().plusSeconds(2), now().plusSeconds(5))))
println(getCommonStatus(Set(now().plusSeconds(2), now().minusSeconds(60))))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment