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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:07:05+00:00 2026-05-25T13:07:05+00:00

I have a loan pattern that applies a function n times where ‘i’ is

  • 0

I have a loan pattern that applies a function n times where ‘i’ is the incrementing variable. “Occasionally”, I want the function passed in to have access to ‘i’….but I don’t want to require all functions passed in to require defining a param to accept ‘i’. Example below…

def withLoaner = (n:Int) => (op:(Int) => String) => {
  val result = for(i <- 1 to n) yield op(i)
  result.mkString("\n")
}

def bob = (x:Int) => "bob" // don't need access to i. is there a way use () => "bob" instead?
def nums = (x:Int) => x.toString // needs access to i, define i as an input param

println(withLoaner(3)(bob))

println(withLoaner(3)(nums))
  • 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-25T13:07:06+00:00Added an answer on May 25, 2026 at 1:07 pm
    def withLoaner(n: Int) = new {
      def apply(op: Int => String) : String = (1 to n).map(op).mkString("\n")
      def apply(op: () => String) : String = apply{i: Int => op()}
    }
    

    (not sure how it is related to the loan pattern)

    Edit Little explanation as requested in comment.

    Not sure what you know and don’t know of scala and what you don’t undestand in that code. so sorry if what I just belabor the obvious.

    First, a scala program consist of traits/classes (also singleton object) and methods. Everything that is done is done by methods (leaving constructor aside). Functions (as opposed to methods) are instances of (subtypes of) the various FunctionN traits (N the number of arguments). Each of them has as apply method that is the actual implemention.
    If you write

    val inc = {i: Int => i + 1}
    

    it is desugared to

    val inc = new Function1[Int, Int] {def apply(i: Int) = i + 1}
    

    (defines an anonymous class extending Function1, with given apply method and creating an instance)

    So writing a function has rather more weight than a simple method. Also you cannot have overloading (several methods with the same name, differing by the signature, just what I did above), nor use named arguments, or default value for arguments.

    On the other hand, functions are first classes values (they can be passed as arguments, returned as result) while methods are not. They are automatically converted to functions when needed, however there may be some edges cases when doing that. If a method is intended solely to be used as a function value, rather than called as a method, it might be better to write a function.

    A function f, with its apply method, is called with f(x) rather than f.apply(x) (which works too), because scala desugars function call notation on a value (value followed by parentheses and 0 or more args) to a call to method apply. f(x) is syntactic sugar for f.apply(x). This works whatever the type of f, it does not need to be one of the FunctionN.

    What is done in withLoaner is returning an object (of an anonymous type, but one could have defined a class separately and returned an instance of it). The object has two apply methods, one accepting an Int => String, the other one an () => String. When you do withLoaner(n)(f) it means withLoaner(n).apply(f). The appropriate apply method is selected, if f has the proper type for one of them, otherwise, compile error.

    Just in case you wonder withLoaner(n) does not mean withLoaner.apply(n) (or it would never stop, that could just as well mean withLoaner.apply.apply(n)), as withLoaner is a method, not a value.

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

Sidebar

Related Questions

Here's the c# code that I have: private double get806Fees (Loan loan) { Loan.Fee.Items
I have a Loan class that in its printPayment method, it prints the amortization
<td>@Html.EditorFor(Model => Model.Loan)</td> I have that in the beginning of the view, and then
We have a program that we install on the phones and loan the phones
I have a tab delimited text file as so : name \t loan period
Have a n-tire web application and search often times out after 30 secs. How
I have one class that declares an enumeration type as: public enum HOME_LOAN_TERMS {FIFTEEN_YEAR,
INFORMIX-SE with ISQL 7.3: I have separate tables for Loan, Purchase & Sales transactions.
I have one class called Person that basically looks like: public class Person {
I have a Win32 console program that I wrote and it works fine. The

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.