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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:37:44+00:00 2026-05-15T18:37:44+00:00

Just seen an interesting possibility to initialize code blocks in Scala for high order

  • 0

Just seen an interesting possibility to initialize code blocks in Scala for high order functions such as foreach or map:

(1 to 3) map {
  val t = 5
  i => i * 5
}


(1 to 3) foreach {  
  val line = Console.readLine  
  i => println(line)  
}  

Is this some documented feature or should I avoid such constructs? I could imagine, the “initialization” block comes into the constructor and the closure itself becomes an apply() method?

Thanks Pat for the original Question (http://extrabright.com/blog/2010/07/10/scala-question-regarding-readline)

  • 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-15T18:37:44+00:00Added an answer on May 15, 2026 at 6:37 pm

    While the features used are not uncommon, I’ll admit is is a fairly odd combination of features. The basic trick is that any block in Scala is an expression, with type the same as the last expression in the block. If that last expression is a function, this means that the block has functional type, and thus can be used as an argument to “map” or “foreach” . What happens in these cases is that when “map” or “foreach” is called, the block is evaluated. The block evaluates to a function ( i=> i*5 in the first case ), and that function is then mapped over the range.

    One possible use of this construct is for the block to define mutable variables, and the resulting function mutate the variables each time it is called. The variables will be initialized once, closed over by the function, and their values updated every time the function is called.

    For example, here’s a somewhat surprising way of calculating the first 6 factorial numbers

    (1 to 6) map {
          var total = 1
          i => {total *= i;total}
        } 
    

    (BTW, sorry for using factorial as an example. It was either that or fibonacci. Functional
    Progamming Guild rules. You gotta problem with that, take it up with the boys down at the hall.)

    A less imperative reason to have a block return a function is to define helper functions earlier in the block. For instance, if your second example were instead

    (1 to 3) foreach {  
      def line = Console.readLine  
      i => println(line)  
    }
    

    The result would be that three lines were read and echoed once each, while your example had the line read once and echoed three times.

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

Sidebar

Ask A Question

Stats

  • Questions 499k
  • Answers 499k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Maybe Jpcap can help. Notice that there's a Sourceforge project… May 16, 2026 at 12:29 pm
  • Editorial Team
    Editorial Team added an answer Got the answer for this just modified the above code May 16, 2026 at 12:29 pm
  • Editorial Team
    Editorial Team added an answer Thanks everyone, I used a combination of your responses. In… May 16, 2026 at 12:29 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I just looked at this posting: What is the best or most interesting use
I just found an interesting situation. Suppose you have some SwingWorker (I've made this
This is one of the more interesting things I've seen in iPhone development. The
I've just come across this code snippet in some Exchange 2010 code and I
I stumbled upon an interesting error that I've never seen before, and can't explain
Just seen this inside <boost/asio.hpp> #ifndef BOOST_ASIO_HPP #define BOOST_ASIO_HPP #if defined(_MSC_VER) && (_MSC_VER >=
Having just seen What does the following macro do? I gotta ask my own
I ve seen in twitter, i can get a user view page by just
I've seen a lot of code for doing listbox or listview thumbnails with hard
I have seen this show up several places in code, never with an explanation,

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.