Skip to content

Instantly share code, notes, and snippets.

@afsalthaj
Last active December 27, 2019 22:11
Show Gist options
  • Save afsalthaj/de0593e7aafb886ebfebff4fd624eae3 to your computer and use it in GitHub Desktop.
Save afsalthaj/de0593e7aafb886ebfebff4fd624eae3 to your computer and use it in GitHub Desktop.
import scalaz.Free
sealed trait Notification[A]
case class SendEmail(s: String) extends Notification[Unit]
case class SendMessage(s: String) extends Notification[Unit]
case class NothingJustLog(s: String) extends Notification[Unit]
sealed trait Table[Unit]
object Table {
type TableAction[A] = Free[Table, A]
def read(s: String): TableAction[String] =
Free.liftF(ReadOp(s))
def write(s: String): TableAction[String] =
Free.liftF(WriteOp(s))
def readAndSendNotification(s: String): Free[Table, Notification[Unit]] =
for {
s <- read(s)
notification = if (s == "admin") SendEmail("admin") else SendMessage("admin")
} yield notification
def writeAndSendNotification(s: String): Free[Table, Notification[Unit]] =
for {
s <- write(s)
notification = if (s == "bla") SendEmail("bla") else SendEmail("admin")
} yield notification
case class WriteOp(s: String) extends Table[String]
case class ReadOp(s: String) extends Table[String]
}
@jedws
Copy link

jedws commented Jul 29, 2018

minor point, in sealed trait Notification[Unit] and sealed trait Table[Unit] the Unit is a type parameter whose name doesn't matter, and the fact it is called Unit is quite misleading :-)

also, you'd normally have WriteOp[String] be WriteOp[Unit] as a write doesn't normally yield anything except maybe the possibility of error. It would make your overloading of the parameters and the val in the for comprehension a little less possible.

@afsalthaj
Copy link
Author

Fixed the typo of Unit :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment