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

  • Home
  • SEARCH
  • 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 6201773
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T04:36:57+00:00 2026-05-24T04:36:57+00:00

I am learning Scala (coming from a background of mostly Java). I am trying

  • 0

I am learning Scala (coming from a background of mostly Java). I am trying to wrap my head around the following code:

object Main {
  def main(args : Array[String]) {
    for (file <- filesEnding(".txt"))
      println(file.getName)
  }

  private val filesHere = (new java.io.File(".")).listFiles

  def filesMatching(matcher: String => Boolean) =
    for (file <- filesHere; if matcher(file.getName))
        yield file

  def filesEnding(query: String) = filesMatching(_.endsWith(query))
  /* Other matcher functions */
}

In particular I am confused where Scala gets the value for _ in each of the matcher functions. I can see that filesEnding is called with an argument of .txt. That argument is assigned to query. filesEnding then calls filesMatching with an argument consistent with a String => Boolean function. Finally I can see that file.getName is what eventually replaces the _ placeholder.

What I don’t get is how Scala knows to put file.getName in place of _. I am having trouble tracing this code in my head and the eclipse debugger isn’t much help in this situation. Can somebody walk me through what is happening in this code?

  • 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-05-24T04:37:00+00:00Added an answer on May 24, 2026 at 4:37 am

    The _ is just a shorthand for making an anonymous function:

    _.endsWith(query)
    

    is the same as the anonymous function

    fileName => fileName.endsWith(query)
    

    This function is then fed as the argument matcher to filesMatching. Inside that function you can see the call

    matcher(file.getName)
    

    This calls the anonymous function with file.getName as the _ argument (which I called fileName in the explicit example).

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

Sidebar

Related Questions

Coming from Java, I am learning Scala. I am interested in game and virtual
I'm learning Scala and trying to use javax.cache in Scala code and can't find
Many of the available resources for learning Scala assume some background in Java. This
I am starting to I am learning scala, and trying to build some simple
I'm still at the beginning in learning scala in addition to java and i
So I'm learning Scala at the moment, and I'm trying to create an abstract
I'm just learning scala, and I wrote the hello,world program like this: object HelloWorld
I am learning Scala and I have a Java project to migrate to Scala.
I'm learning Scala and I'm trying to store a function in a var to
I'm currently learning scala. Why this code doesn't work: class GenClass[T](var d : T)

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.