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
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) => |
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
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 { |
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
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)) |
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
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) |
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
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] = ??? | |
} |
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
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(_)) | |
} |
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
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) | |
} |
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
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 |
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
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 |
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
val filter = EmailFilter.senderIsNot(Address("[email protected]")) && | |
EmailFilter.bodyContains("Unsubscribe") | |
val newsletters = emails.filter(filter.run) |
NewerOlder