3

I'd like to have some basic knowledge of how deeply my function call is nested. Consider the following:

scala> def decorate(f: => Unit) : Unit = { println("I am decorated") ; f }
decorate: (f: => Unit)Unit

scala>  decorate { println("foo") }
I am decorated
foo

scala> decorate { decorate { println("foo") } }
I am decorated
I am decorated
foo

For the last call, I'd like to be able to get the following:

I am decorated 2x
I am decorated 1x
foo

The idea is that the decorate function knows how deeply its nested. Ideas?

Update: As Nikita had thought, my example doesn't represent what I'm really after. The goal is not to produce the strings so much as to be able to pass some state through a series of calls to the same nested function. I think Régis Jean-Gilles is pointing me in the right direction.

Larsenal
  • 49,878
  • 43
  • 152
  • 220
  • The problem looks very much like a perfect candidate for recursion or `unfold`-`fold`. But your current example is imperative and rewriting it for functional algorithm would radically change its structure and logic, so I'm afraid to take it way off from what you may actually be looking for. It would help a lot if you presented a code closer to your real problem or rewrote the current one to be functional. – Nikita Volkov Nov 07 '12 at 20:18
  • Will do that; good idea. – Larsenal Nov 07 '12 at 20:49
  • Also seems like a really good candidate for a monadic collection, like Scala's Option type. In this case, your monad would turn a function `A => B` into a function `A => (B, Int)` where the integer is the current depth in the call stack. You could set this up to implement map, flatmap, and filter, and then you'd be able to use it in a for comprehension. – Jordan Lewis Nov 07 '12 at 21:25
  • You could even specify two implementations of your monadic type - one that counts invocations and one that doesn't - so you could track only the number of invocations on the particular function that you're interested in. – Jordan Lewis Nov 07 '12 at 21:28
  • Plz see the updates to [my answer](http://stackoverflow.com/a/13277826/485115) – Nikita Volkov Nov 07 '12 at 21:52

3 Answers3

4

You can use the dynamic scope pattern. More prosaically this means using a thread local variable (scala's DynamicVariable is done just for that) to store the current nesting level. See my answer to this other question for a partical example of this pattern: How to define a function that takes a function literal (with an implicit parameter) as an argument?

This is suitable only if you want to know the nesting level for a very specific method though. If you want a generic mecanism that works for any method then this won't work (as you'd need a distinct variable for each method). In this case the only alternative I can think of is to inspect the stack, but not only is it not very reliable, it is also extremely slow.

UPDATE: actually, there is a way to apply the dynamic scope pattern in a generic way (for any possible method). The important part is to be able to implicitly get a unique id for each method. from there, it is just a matter of using this id as a key to associate a DynamicVariable to the method:

import scala.util.DynamicVariable
object FunctionNestingHelper {  
  private type FunctionId = Class[_]
  private def getFunctionId( f: Function1[_,_] ): FunctionId = {
    f.getClass // That's it! Beware, implementation dependant.
  }
  private val currentNestings = new DynamicVariable( Map.empty[FunctionId, Int] )
  def withFunctionNesting[T]( body: Int => T ): T = {
    val id = getFunctionId( body )
    val oldNestings = currentNestings.value 
    val oldNesting = oldNestings.getOrElse( id, 0 )
    val newNesting = oldNesting + 1
    currentNestings.withValue( oldNestings + ( id -> newNesting) ) {
      body( newNesting )
    }    
  }
}

Usage:

import FunctionNestingHelper._
def decorate(f: => Unit)  = withFunctionNesting { nesting: Int =>
  println("I am decorated " + nesting + "x") ; f 
}

To get a unique id for the method, I actually get an id for a the closure passed to withFunctionNesting (which you must call in the method where you need to retrieve the current nesting). And that's where I err on the implementation dependant side: the id is just the class of the function instance. This does work as expected as of now (because every unary function literal is implemented as exactly one class implementing Function1 so the class acts as a unique id), but the reality is that it might well break (although unlikely) in a future version of scala. So use it at your own risk.

Finally, I suggest that you first evaluate seriously if Nikita Volkov's suggestion of going more functional would not be a better solution overall.

Community
  • 1
  • 1
Régis Jean-Gilles
  • 32,541
  • 5
  • 83
  • 97
0

You could return a number from the function and count how many levels you are in on the way back up the stack. But there is no easy way to count on the way down like you have given example output for.

Ivan Meredith
  • 2,222
  • 14
  • 13
0

Since your question is tagged with "functional programming" following are functional solutions. Sure the program logic changes completely, but then your example code was imperative.

The basic principle of functional programming is that there is no state. What you're used to have as a shared state in imperative programming with all the headache involved (multithreading issues and etc.) - it is all achieved by passing immutable data as arguments in functional programming.

So, assuming the "state" data you wanted to pass was the current cycle number, here's how you'd implement a function using recursion:

def decorated ( a : String, cycle : Int ) : String
  = if( cycle <= 0 ) a
    else "I am decorated " + cycle + "x\n" + decorated(a, cycle - 1)

println(decorated("foo", 3))

Alternatively you could make your worker function non-recursive and "fold" it:

def decorated ( a : String, times : Int )
  = "I am decorated " + times + "x\n" + a

println( (1 to 3).foldLeft("foo")(decorated) )

Both codes above will produce the following output:

I am decorated 3x
I am decorated 2x
I am decorated 1x
foo
Nikita Volkov
  • 42,792
  • 11
  • 94
  • 169