0

I am learning Scala and see the follow code in a tutorial

case class Email (
    subject: String,
    text: String,
    sender: String,
    recipient: String)

type EmailFilter = Email => Boolean
def newMailsForUser(mails: Seq[Email], f: EmailFilter) = mails.filter(f)

val notSentByAnyOf: Set[String] => EmailFilter = 
    senders => email => !senders.contains(email.sender)

I understand that notSentByAnyOf is a function type that has the signature type Set[String] => EmailFilter.

However, I am not sure what senders => email => !senders.contains(email.sender) means.

Can I please get some explanation?

Thank you very much for your help!

1 Answer 1

1

The answer is in what the type EmailFilter is.

An EmailFilter is defined as:

type EmailFilter = Email => Boolean

Which means it's a function from Email to Boolean. Now let's look at notSentByAnyOf:

senders => ...

is a function that given senders returns something. We know from the signature that something needs to be an EmailFilter. Since we know EmailFilter is really a function Email => Boolean, then that is what should be on the right side of the arrow1:

senders => (email => !senders.contains(email.sender))
           |________________________________________|
                               |
                            EmailFilter

So, notSentByAnyOf, is a function that given a set of String, returns a function Email => Boolean.

[1] The extra parenthesis in the example are just for emphasis. As in the original code, they can be omitted.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.