Created
January 27, 2020 10:54
-
-
Save kovacshuni/56f2e56f500361a72c36623edec38e47 to your computer and use it in GitHub Desktop.
typeclasses-example
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
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