1

I'm looking at http://hseeberger.wordpress.com/2010/11/25/introduction-to-category-theory-in-scala/ and there's a bit of code that I can't understand how it works:

object Functor {

  def fmap[A, B, F[_]](as: F[A])(f: A => B)(implicit functor: Functor[F]): F[B] =
    functor.fmap(as)(f)

  implicit object ListFunctor extends Functor[List] {
    def fmap[A, B](f: A => B): List[A] => List[B] =
      as => as map f
  }
}

Specifically, how is ListFunctor.fmap accessing as when the definition of as is in the scope of Functor.fmap and (as far as I can tell) inaccessible to ListFunctor.fmap?

(related with a twist) There's a previous iteration to the above code that's defined:

trait Functor[F[_]] extends GenericFunctor[Function, Function, F] {
  final def fmap[A, B](as: F[A])(f: A => B): F[B] =
    fmap(f)(as)
}

object ListFunctor extends Functor[List] {
  def fmap[A, B](f: A => B): List[A] => List[B] = as => as map f
}

But again, as seems to be magically accessible to ListFunctor. I guess if I understand one of these, I'll understand the other.

1 Answer 1

3

It is not the same as, just the same name is (probably not quite) accidentally used in two places. The as => as map f is a function definition, and the as before the arrow is a declaration of a parameter for this very function.

It would be completely equivalent if written as x => x map f.

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

Comments

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.