Skip to content

Instantly share code, notes, and snippets.

Ruurtjan ruurtjan

View GitHub Profile
@ruurtjan
ruurtjan / functional-dsl-blog-10.scala
Last active May 27, 2020
functional-dsl-blog-10.scala
View functional-dsl-blog-10.scala
sealed trait EmailFilter { self =>
def &&(that: EmailFilter): EmailFilter = EmailFilter.And(self, that)
def ||(that: EmailFilter): EmailFilter = (self.negate && that.negate).negate
def negate : EmailFilter = EmailFilter.Not(self)
import EmailFilter._
def run(email: Email): Boolean = self match {
case Always => true
case Not(filter: EmailFilter) => !filter.run(email)
case And(left: EmailFilter, right: EmailFilter) =>
@ruurtjan
ruurtjan / functional-dsl-blog-9.scala
Created May 22, 2020
functional-dsl-blog-9.scala
View functional-dsl-blog-9.scala
case class EmailFilter(run: Email => Boolean) { self =>
def &&(that: EmailFilter): EmailFilter =
EmailFilter(email => self.run(email) && that.run(email))
def ||(that: EmailFilter): EmailFilter =
EmailFilter(email => self.run(email) || that.run(email))
def negate : EmailFilter =
EmailFilter(email => !self.run(email))
}
object EmailFilter {
@ruurtjan
ruurtjan / functional-dsl-blog-8.scala
Created May 22, 2020
Final encoded email filter
View functional-dsl-blog-8.scala
object final_encoded_email_filter {
final case class Address(emailAddress: String)
final case class Email(sender: Address, to: List[Address], subject: String, body: String)
case class EmailFilter(run: Email => Boolean) { self =>
def &&(that: EmailFilter): EmailFilter = EmailFilter(email => self.run(email) && that.run(email))
def ||(that: EmailFilter): EmailFilter = EmailFilter(email => self.run(email) || that.run(email))
def negate : EmailFilter = EmailFilter(email => !self.run(email))
@ruurtjan
ruurtjan / functional-dsl-blog-7.scala
Created May 22, 2020
Initial encoded email filter example
View functional-dsl-blog-7.scala
object initial_encoded_email_filter {
final case class Address(emailAddress: String)
final case class Email(sender: Address, to: List[Address], subject: String, body: String)
sealed trait EmailFilter { self =>
def &&(that: EmailFilter): EmailFilter = EmailFilter.And(self, that)
def ||(that: EmailFilter): EmailFilter = (self.negate && that.negate).negate
def negate : EmailFilter = EmailFilter.Not(self)
@ruurtjan
ruurtjan / functional-dsl-blog-6.scala
Created May 22, 2020
functional-dsl-blog-6.scala
View functional-dsl-blog-6.scala
object MoreOrthogonal {
def flatMap[A, B](l: List[A], f: A => List[B]): List[B] =
MoreOrthogonal.flatten(MoreOrthogonal.map(l, f))
def flatten[A](l: List[List[A]]): List[A] = ???
def map[A, B](l: List[A], f: A => B): List[B] = ???
}
@ruurtjan
ruurtjan / functional-dsl-blog-5.scala
Created May 22, 2020
functional-dsl-blog-5.scala
View functional-dsl-blog-5.scala
object Original {
def flatMap[A, B](l: List[A], f: A => List[B]): List[B] = ???
def flatten[A](l: List[List[A]]): List[A] =
Original.flatMap(l, (as: List[A]) => as)
def map[A, B](l: List[A], f: A => B): List[B] =
Original.flatMap(l, List(_))
}
@ruurtjan
ruurtjan / functional-dsl-blog-4.scala
Created May 1, 2020
functional-dsl-blog-4.scala
View functional-dsl-blog-4.scala
sealed trait EmailFilter { self =>
def &&(that: EmailFilter): EmailFilter = EmailFilter.And(self, that)
def ||(that: EmailFilter): EmailFilter = (self.negate && that.negate).negate
def negate : EmailFilter = EmailFilter.Not(self)
}
@ruurtjan
ruurtjan / functional-dsl-blog-3.scala
Created Apr 30, 2020
functional-dsl-blog-3.scala
View functional-dsl-blog-3.scala
val always: EmailFilter = Always
val never: EmailFilter = always.negate
def senderIs(sender: Address): EmailFilter = SenderEquals(sender)
def senderIsNot(sender: Address): EmailFilter = SenderEquals(sender).negate
def recipientIs(recipient: Address): EmailFilter = RecipientEquals(recipient)
def recipientIsNot(recipient: Address): EmailFilter = RecipientEquals(recipient).negate
def senderIn(senders: Set[Address]): EmailFilter = senders.foldLeft(never)(_ || senderIs(_))
def recipientIn(recipients: Set[Address]): EmailFilter = recipients.foldLeft(never)(_ || recipientIs(_))
def bodyContains(phrase: String): EmailFilter = BodyContains(phrase)
def bodyDoesNotContain(phrase: String): EmailFilter = BodyContains(phrase).negate
@ruurtjan
ruurtjan / functional-dsl-blog-2.scala
Created Apr 30, 2020
functional-dsl-blog-2.scala
View functional-dsl-blog-2.scala
sealed trait EmailFilter
final case object Always extends EmailFilter
final case class Not(filter: EmailFilter) extends EmailFilter
final case class And(left: EmailFilter, right: EmailFilter) extends EmailFilter
final case class SenderEquals(target: Address) extends EmailFilter
final case class RecipientEquals(target: Address) extends EmailFilter
final case class BodyContains(phrase: String) extends EmailFilter
@ruurtjan
ruurtjan / functional-dsl-blog-1.scala
Last active May 22, 2020
functional-dsl-blog-1.scala
View functional-dsl-blog-1.scala
val filter = EmailFilter.senderIsNot(Address("me@gmail.com")) &&
EmailFilter.bodyContains("Unsubscribe")
val newsletters = emails.filter(filter.run)
You can’t perform that action at this time.