Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8734181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:47:23+00:00 2026-06-13T09:47:23+00:00

I’m just trying to understand the below code : Here a new type alias

  • 0

I’m just trying to understand the below code :


Here a new type alias Set is declared which is a function that takes an Int
parameter and returns a boolean

type Set = Int => Boolean

Here a new method ‘contains’ is declared, which takes two parameters of type Set and Int
which returns a boolean. The boolean is set to the function declared in earlier
(‘type Set = Int => Boolean’)
But what logic is performed to determine if the Int ‘elem’ is a member of Set ‘s’

def contains(set: Set, elem: Int): Boolean = set(elem)

Here a method is defined which returns a set which returns a function ?

def singletonSet(elem: Int): Set = set => set == elem

Complete code with comments :

  /**
   * We represent a set by its characteristic function, i.e.
   * its `contains` predicate.
   */
  type Set = Int => Boolean

      /**
       * Indicates whether a set contains a given element.
       */
def contains(set: Set, elem: Int): Boolean = set(elem)

      /**
       * Returns the set of the one given element.
       */
      def singletonSet(elem: Int): Set = set => set == elem
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T09:47:25+00:00Added an answer on June 13, 2026 at 9:47 am

    Let’s read sort of backwards, in logical order.

    Say you have a finite set of integers: 0, 1, 2, 3, 5, 8 for instance

    One way to describe this set of integers is through a function (its characteristic or indicator function) that, for each integer, returns true if the integer is in the set, false if it is not.
    The signature for this function, as we described it, must always be Int => Boolean (“give me an integer, I will tell you if it’s in the set”), while its implementation will vary depending on the specific set.

    For the set in my example above you could write this function simply as:

    val mySet: Int => Boolean = x => Array(0,1,2,3,5,8) contains x
    

    or recognize that the ints in the set are the first ones of the Fibonacci sequence and define f in a slightly more sophisticated way (which I won’t do here…).
    Note that the “contains” I’ve used is defined for all scala collections.
    In any case, now you have a function that tells you what is in the set and what is not.
    Let’s try it in the REPL.

    scala> val mySet: Int => Boolean = x => Array(0,1,2,3,5,8) contains x
    mySet: Int => Boolean = <function1>
    
    scala> mySet(3)
    res0: Boolean = true
    
    scala> mySet(9)
    res1: Boolean = false
    

    Now, mySet has type Int => Boolean, which we can make more readable if we define it as a type alias.

    scala> type Set = Int => Boolean
    defined type alias Set
    

    Besides readability, defining Set as an alias of Int => Boolean is making it explicit that in a way a Set is its characteristic function. We can redefine mySet in a more concise (but otherwise equivalent) way with the Set type alias:

    scala> val mySet: Set = x => Array(0,1,2,3,5,8) contains x
    mySet: Int => Boolean = <function1>
    

    Now for the last piece of this long answer. Let’s define a characteristic function to describe this Singleton set: 3.
    Easy:

    val Singleton3 : Set = set => set == 3
    

    for a Singleton set containing only 4, it would be:

    val Singleton4 : Set = set => set == 4
    

    So, let’s generalize the creation of these functions and write a method that returns a Singleton function that, for any integer, describes the set containing only that integer:

    def singletonSet(elem: Int): Set = set => set == elem
    

    APPENDIX:

    I skipped this part, because it wasn’t really needed: def contains(set: Set, elem: Int): Boolean = set(elem)

    I think it’s sort of pointless and (without more context) it looks just like a contrived example to demonstrate how you can pass a function around as an argument, just like any other type in scala. It takes the Int => Bool function and the Int and just applies the function to the Int so you can do

    scala> contains(mySet, 3)
    res2: Boolean = true
    

    which is like calling mySet(3) directly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.