Saturday, 15 June 2013

scala: PartialFunction with state -


I can write in scala:

  val pf: partial function [string, unit ] = {Case S = & gt; Println (s)}  

Now I can pass the PF around the calling with appropriate values ​​

I can be able to define such a PF I am looking for a brief way to make it a state. Ask a counter how many times it has been called. One way is:

  var counter = 0 val pf: partial function [string, unit] = {case s => Counter + = 1; Println (s)}  

What I did not like here is that it is not short and the state is open.

  val pf: partial function [string, unit] = {var counter = 0; {Case S = & gt; {Println (counter + s) counter = = 1}}}  

No comments:

Post a Comment