Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

scala loan pattern, optional function param

Tags:

scala

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))
like image 340
eptx Avatar asked Sep 07 '11 21:09

eptx


1 Answers

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.

like image 151
Didier Dupont Avatar answered Nov 19 '22 06:11

Didier Dupont