1

(It seems like I ask too many basic questions, but Scala is very difficult, especially when it comes to the language feature or syntactic sugar, and I come from a Java background..)

I am watching this video: Scalaz State Monad on YouTube. The lecturer wrote these code:

trait State[S, +A] {
  def run(initial: S):(S, A)
  def map[B](f: A=>B): State[S, B] =
    State { s =>
      val (s1, a) = run(s)
      (s1, f(a))
}

He said the State on the forth line is a "lambda" expression (I may have misheard). I'm a bit confused about this. He did have an object State, which looks like this:

Object State {
  def apply[S, A](f: S=>(S, A)): State[S, A] =
    new State[S, A] {
      def run(i: S) = f(i)
    }
}

The lecturer said he was invoking the apply method of Object State by invoking like that, and yes he did pass in a function that returns a correct tuple, but what is that { s=>? Where the hell did he get the s and is this about the implicit mechanism in Scala?

Also the function that is passed in map function changes A to B already, how can it still pass the generics check (from [S, A] to [S, B] assuming A and B are two different types, or are they the same type?If true, why use B not A in map function?)?

The video link is here and is set to be at 18:17 http://youtu.be/Jg3Uv_YWJqI?t=18m17s

Thank you (Monad is a bit daunting to anyone that's not so familiar with functional programming)

windweller
  • 2,365
  • 5
  • 33
  • 56
  • The `{ s=>...}` is a lambda expression in Scala. – pedrofurla Jan 11 '14 at 07:07
  • Granted it is confusing at first, then simple. I don't understand the question about the generics check. – som-snytt Jan 11 '14 at 07:22
  • @som-snytt I edited that part..it seems like I'm not very knowledge with generics.. – windweller Jan 11 '14 at 07:30
  • I respectfully suggest that you are finding Scala very difficult partly because you are looking at things that you are not ready to look at. Because Scala is so much more powerful than Java, you will find some pretty esoteric stuff out there, but that doesn't mean you need to know category theory to do in Scala the kinds of things you know how to do in Java. You would probably do best to set scalaz aside for at least six months, maybe a year. By then these little details will give you no trouble, and you will be ready to investigate scalaz and other packages far beyond Java's reach. – AmigoNico Jan 12 '14 at 08:45
  • @AmigoNico You might be right. The entire Monad and other functional programming patterns seem bizarre. I'll try to understand them again later on. Thanks :) – windweller Jan 12 '14 at 09:26

1 Answers1

1

You are on right way here.

He passes the function as parameter, but what is the function in general? Function takes argument and return a value. Returned value here is tuple (s1, f(a)), but also we need to reffer to function argument. s in your example is function argument.

It is same as

def map[B](f: A=>B): State[S, B] = {
    def buildState(s: S) = {
      val (s1, a) = run(s)
      (s1, f(a))         
    }  
    State(buildState)
}

But, since we dont need this function anywhere else we just inline it in apply call.

1esha
  • 1,722
  • 11
  • 17
  • Emmm..then how does the program find this `s`? The only thing being passed in `map` is a function.. – windweller Jan 11 '14 at 07:28
  • @WindDweller, check usage of `apply` method of `State` object. You call `apply` and passes function. `apply` creates instance of `State` trait with `run` method calling passed function. In short it returns new `State` object which call you function when you call `run` on it. Hope you got it :) – 1esha Jan 11 '14 at 07:45