mert inan
mert inan

Reputation: 1597

functor syntax `map` for function does not work?

I can't seem compile this with cats "org.typelevel" %% "cats-core" % "1.0.0-MF" which was fine with "org.typelevel" %% "cats-core" % "0.9.0",.

import cats._
import cats.instances.all._
import cats.syntax.all._

val func1 = (x: Int) => x.toDouble
val func2 = (y: Double) => y * 2
val func3 = func1.map(func2)
func3(3)

The error is :

Error:(7, 25) value map is not a member of Int => Double
lazy val func3 = func1.map(func2)
                       ^

Upvotes: 2

Views: 219

Answers (1)

Luka Jacobowitz
Luka Jacobowitz

Reputation: 23502

I think what you're looking for might just be function composition. You can do:

val func3 = func1 andThen func2

or

val func3 = func2 compose func1

However if you want to map over functions you need to have partial-unification enabled. The easiest way to do so, is to add the sbt-partial-unification plugin.

If you're on Scala 2.11.9 or newer, you can also simply add the compiler flag:

scalacOptions += "-Ypartial-unification"

With either of those this compiles fine:

val func1 = (x: Int) => x.toDouble
val func2 = (y: Double) => y * 2
val func3 = func1.map(func2)

Upvotes: 2

Related Questions