Reputation: 311
As I know functor
is a function returning a module from other module
Such that its signature is * -> *
Then by definition, is apply
method in Companion object a functor ??
For example,
case class A(id: Double)
object A {
def apply(intId: Int) = A(intId.toDouble) // Functor: Double -> A
}
Am I right??
My Questions are
1) can apply
method in companion object be viewed as Functor?
2) If functor
is a just function that create an instance from other instance, what is the difference between functor and constructor
Upvotes: 0
Views: 169
Reputation: 6460
First, this is not a definition of anything:
As I know functor is a function returning a module from other module
Such that its signature is * -> *
And in the context of Scala, which modules are you talking about?
If you're talking about mathematical definition of functor, let's first clarify what is a functor in mathematics:
A functor between two categories is a pair of maps: between their objects and their morphisms, such that they satisfy certain axioms (in short that it respects source/target of the morphisms and respects composition).
So first, before talking about any functors, you need to set up some context: a functor between which categories? What are their objects and what are their morphisms?
Then you can define a functor by defining how it maps those objects and how it maps morphisms.
So I can't give any answers to your questions, because 1) a method is just a map between values of two types, where are any categories here? 2) the phrase "functor is a just function" already doesn't make sense.
Now, the usual context in which people speak about functors in programming in general and in Scala in particular is by defining a category which has (Scala) types as its objects and functions as its mophisms.
Then you can consider functors from this category to itself (which are called endofunctors), so in Scala it can be any parametrized type, which you can map over. For example, you can view List[_]
as a functor:
A
to type List[A]
.map
you can map a function A => B
to a function List[A] => List[B]
To be a real functor, it has also satisfy the mentioned axioms, but let's leave them aside for simplicity.
Another example of a category in the context of Scala (or any similar programming language) is one with Scala types (again) as objects and the morphisms being the subtype relation: A :> B
.
Check the linked related questions for more details and examples.
So if you can provide enough of such context, then update your question and ask in the comments if you don't understand something.
Upvotes: 1