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 9260199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:49:59+00:00 2026-06-18T12:49:59+00:00

I am surfing through Scala for the Impatient, but unfortunately it assumes previous Java

  • 0

I am surfing through Scala for the Impatient, but unfortunately it assumes previous Java experience and an understanding of how [T] is used here:

object Iterables {
   def filter[T](unfiltered: Iterable[T], predicate: T => Boolean): Iterable[T] = {...}
   def find[T](iterable: Iterable[T], predicate: T => Boolean): T = {...}
}

The syntax for every instance of [T] and T is quite confusing here. What significance does it have when it’s listed directly after the function name, such as filter[T]. I understand in the parameters list, we’re looking for an Iterable of type T. But if T is a type, what does predicate: T => Boolean mean?

  • 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-18T12:50:01+00:00Added an answer on June 18, 2026 at 12:50 pm

    Type Parametric Methods

    A method in scala can accept one or more type parameters, just like a class, object or trait does.

    In this case it means that the same method can be called for a type T which can vary from call to call, but that have to be consistent within the definition.

    Let’s use for example

    def filter[T](unfiltered: Iterable[T], predicate: T => Boolean): Iterable[T]
    

    The method expects you to pass:
    – an Iterable of Ts (unfiltered)
    – a function that transforms a T to a Boolean (predicate)
    and the result will be another Iterable of Ts.

    The method will then iterate on the unfiltered object and for each element apply the predicate, to decide if it must be filtered or not.

    The resulting Iterable only contains the elements satisfying the predicate (i.e. those t for whom predicate(t) returns true)

    You can call filter for any type T, with the constraint that it must be consistent fo all parameters and the result type.

    examples

    //filtered is List(2,3)
    val filtered: Iterable[Int] = filter(List(1, 2, 3), (i: Int) => i > 1) 
    
    //filtered is List("b", "abc")
    val filtered: Iterable[String] = filter(List("a", "b", "abc"), (s: String) => s contains "b") 
    
    //filtered is List(Some(1), Some(2))
    val filtered: Iterable[Option[Int]] = filter(List(Some(1), Some(2), None), (op: Option[Int]) => op.isDefined)
    
    
    //you can't call this because T is not the same everywhere it's used
    val filtered: Iterable[Int] = filter(List(1, 2, 3), (op: Option[Int]) => op.isDefined)
    
    //this will get you
    <console>:12: error: type mismatch;
     found   : Option[Int] => Boolean
     required: Int => Boolean
               val filtered: Iterable[Int] = filter(List(1, 2, 3), (op: Option[Int]) => op.isDefined) 
    
                                                                                 ^
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm surfing through the wonderful blog maintained by Scott Stevenson, and I'm trying to
while surfing through the web and through stackoverflow.com i found many posts in forums,
I have been surfing for this anwser, but seems like its not supportef in
In an HTML 5 site a user, who's surfing through a mobile device, clicks
After surfing through tons of documentation on the web it seems that the iPhone
While surfing for a solution to call Matlab from Java, I came to know
I'm creating a simple auto surfing app to learn the WebBrowser object. I've got
I'm quite a newbie with Boost, and my only experience of surfing though a
Surfing on the source code of Java, I found the following declaration: public abstract
After reading the books, surfing the nets regarding the type of references in Java,

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.