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)

5
  • The { s=>...} is a lambda expression in Scala. Commented Jan 11, 2014 at 7:07
  • Granted it is confusing at first, then simple. I don't understand the question about the generics check. Commented Jan 11, 2014 at 7:22
  • @som-snytt I edited that part..it seems like I'm not very knowledge with generics.. Commented Jan 11, 2014 at 7: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. Commented Jan 12, 2014 at 8: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 :) Commented Jan 12, 2014 at 9:26

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Emmm..then how does the program find this s? The only thing being passed in map is a function..
@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 :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.