Proper implementation for a 2 type parameters Functor in ScalaIs the Scala 2.8 collections library a case of “the longest suicide note in history”?Implicit conversion from String to Int in scala 2.8Simple Scala syntax - trying to define “==” operator - what am I missing?What are the rules for when trait/class type parameters take precedence vs method type parametersScala Implicit Conversion GotchasScala: Is it possible to override val's in the sub-class's constructor?Scala generics not clear to meCalculation Distance Between Pointsimplicit parameters and generic typesParser combinators prevent mapping of strings

Why should someone be willing to write a strong recommendation even if that means losing a undergraduate from their lab?

Writing/buying Seforim rather than Sefer Torah

How to dismiss intrusive questions from a colleague with whom I don't work?

Is it appropriate for a business to ask me for my credit report?

How did Apollo 15's depressurization work?

Do predators tend to have vertical slit pupils versus horizontal for prey animals?

How to get distinct values from an array of arrays in JavaScript using the filter() method?

Is a butterfly one or two animals?

What is a "click" in Greek or Latin?

How could Tony Stark wield the Infinity Nano Gauntlet - at all?

How do I intentionally fragment a SQL Server Index?

How can I pack my food so it doesn't smell?

Land Registry Clause

E: Sub-process /usr/bin/dpkg returned an error code (1) - but how do I find the meaningful error messages in APT's output?

Is there any road between the CA State Route 120 and Sherman Pass Road (Forest Route 22S0) that crosses Yosemite/Serria/Sequoia National Park/Forest?

Is "stainless" a bulk or a surface property of stainless steel?

What is the latest version of SQL Server native client that is compatible with Sql Server 2008 r2

Mixing colours for a symbol

What animal has fat with the highest energy density?

How could China have extradited people for political reason under the extradition law it wanted to pass in Hong Kong?

What professions does medieval village with a population of 100 need?

Count the frequency of integers in an array

Why do some academic journals requires a separate "summary" paragraph in addition to an abstract?

Did the twin engined Lazair ultralight have a throttle for each engine?



Proper implementation for a 2 type parameters Functor in Scala


Is the Scala 2.8 collections library a case of “the longest suicide note in history”?Implicit conversion from String to Int in scala 2.8Simple Scala syntax - trying to define “==” operator - what am I missing?What are the rules for when trait/class type parameters take precedence vs method type parametersScala Implicit Conversion GotchasScala: Is it possible to override val's in the sub-class's constructor?Scala generics not clear to meCalculation Distance Between Pointsimplicit parameters and generic typesParser combinators prevent mapping of strings






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








3















I saw this question several times on SO, but no matter how hard I try, I can't make the following code compile. The goal is to implement an Functor implementation for a simpler Reader (the code is here):



 trait Functor[F[_]] 
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo = Reader[String, Int](_ => 42)

foo.fmap(_ + 1) // does not compile


I tried to bypass the implicit mechanism with the following:



FunctorOps(foo).fmap(_ + 1)


but this outputs the following compilation error:



Error:(82, 23) type mismatch;
found : com.fp.Scratchpad.Reader[String,Int]
required: ?F[?A]
Note that implicit conversions are not applicable because they are ambiguous:
both method ArrowAssoc in object Predef of type [A](self: A)ArrowAssoc[A]
and method Ensuring in object Predef of type [A](self: A)Ensuring[A]
are possible conversion functions from com.fp.Scratchpad.Reader[String,Int] to ?F[?A]
FunctorOps(foo).fmap(_ + 1)


Thank you in advance for your help.



UPDATE



Just to make sure my FunctorOps is right, I created a functor instance for Id:



case class Id[A](value: A)
implicit val idF: Functor[Id] = new Functor[Id]
override def fmap[A, B](fa: Id[A])(f: A => B): Id[B] = Id(f(fa.value))


val id = Id(42)
id.fmap(_ + 1) // compiles


So the problem does not come from the FunctorOps implicit class. I suspect Scala to have a real hard time with type lambdas...



UPDATE 2



I tried to simplify the problem but without success:



 trait Functor[F[_]] 
def map[A, B](x: F[A])(f: A => B): F[B]


implicit class Ops[F[_], A](fa: F[A])(implicit F: Functor[F])
def map[B](f: A => B): F[B] = F.map(fa)(f)


type FF[A] = ( type F[B] = A => B )

implicit def ff[E]: Functor[FF[E]#F] = new Functor[FF[E]#F]
override def map[A, B](x: E => A)(f: A => B): E => B = e => f(x(e))


val f: String => Int = _ => 42

val value: Functor[FF[String]#F] = ff[String]
val ops = new Ops[FF[String]#F, Int](f)(value)

// These compile
ops.map(_ + 1)("")
value.map(f)(_ + 1)("")

// This not
f.map(_ + 1)









share|improve this question


























  • What is your Scala version? The first one compiles in scastie.scala-lang.org/gpRKdKjNS4KLTAq7Zwptsw. I was actually surprised it seems to work in older versions too, I assumed you need 2.12 or 2.11.11+.

    – Alexey Romanov
    Mar 27 at 16:55











  • I've just checked and Scala version is 2.12.8. Tried to compile with IDEA and sbt :(

    – Francis Toth
    Mar 27 at 23:34











  • Are you running this in a Worksheet?

    – mdm
    Mar 28 at 10:30











  • No, and I also tried to compile this raw sbt

    – Francis Toth
    Mar 28 at 13:17

















3















I saw this question several times on SO, but no matter how hard I try, I can't make the following code compile. The goal is to implement an Functor implementation for a simpler Reader (the code is here):



 trait Functor[F[_]] 
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo = Reader[String, Int](_ => 42)

foo.fmap(_ + 1) // does not compile


I tried to bypass the implicit mechanism with the following:



FunctorOps(foo).fmap(_ + 1)


but this outputs the following compilation error:



Error:(82, 23) type mismatch;
found : com.fp.Scratchpad.Reader[String,Int]
required: ?F[?A]
Note that implicit conversions are not applicable because they are ambiguous:
both method ArrowAssoc in object Predef of type [A](self: A)ArrowAssoc[A]
and method Ensuring in object Predef of type [A](self: A)Ensuring[A]
are possible conversion functions from com.fp.Scratchpad.Reader[String,Int] to ?F[?A]
FunctorOps(foo).fmap(_ + 1)


Thank you in advance for your help.



UPDATE



Just to make sure my FunctorOps is right, I created a functor instance for Id:



case class Id[A](value: A)
implicit val idF: Functor[Id] = new Functor[Id]
override def fmap[A, B](fa: Id[A])(f: A => B): Id[B] = Id(f(fa.value))


val id = Id(42)
id.fmap(_ + 1) // compiles


So the problem does not come from the FunctorOps implicit class. I suspect Scala to have a real hard time with type lambdas...



UPDATE 2



I tried to simplify the problem but without success:



 trait Functor[F[_]] 
def map[A, B](x: F[A])(f: A => B): F[B]


implicit class Ops[F[_], A](fa: F[A])(implicit F: Functor[F])
def map[B](f: A => B): F[B] = F.map(fa)(f)


type FF[A] = ( type F[B] = A => B )

implicit def ff[E]: Functor[FF[E]#F] = new Functor[FF[E]#F]
override def map[A, B](x: E => A)(f: A => B): E => B = e => f(x(e))


val f: String => Int = _ => 42

val value: Functor[FF[String]#F] = ff[String]
val ops = new Ops[FF[String]#F, Int](f)(value)

// These compile
ops.map(_ + 1)("")
value.map(f)(_ + 1)("")

// This not
f.map(_ + 1)









share|improve this question


























  • What is your Scala version? The first one compiles in scastie.scala-lang.org/gpRKdKjNS4KLTAq7Zwptsw. I was actually surprised it seems to work in older versions too, I assumed you need 2.12 or 2.11.11+.

    – Alexey Romanov
    Mar 27 at 16:55











  • I've just checked and Scala version is 2.12.8. Tried to compile with IDEA and sbt :(

    – Francis Toth
    Mar 27 at 23:34











  • Are you running this in a Worksheet?

    – mdm
    Mar 28 at 10:30











  • No, and I also tried to compile this raw sbt

    – Francis Toth
    Mar 28 at 13:17













3












3








3








I saw this question several times on SO, but no matter how hard I try, I can't make the following code compile. The goal is to implement an Functor implementation for a simpler Reader (the code is here):



 trait Functor[F[_]] 
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo = Reader[String, Int](_ => 42)

foo.fmap(_ + 1) // does not compile


I tried to bypass the implicit mechanism with the following:



FunctorOps(foo).fmap(_ + 1)


but this outputs the following compilation error:



Error:(82, 23) type mismatch;
found : com.fp.Scratchpad.Reader[String,Int]
required: ?F[?A]
Note that implicit conversions are not applicable because they are ambiguous:
both method ArrowAssoc in object Predef of type [A](self: A)ArrowAssoc[A]
and method Ensuring in object Predef of type [A](self: A)Ensuring[A]
are possible conversion functions from com.fp.Scratchpad.Reader[String,Int] to ?F[?A]
FunctorOps(foo).fmap(_ + 1)


Thank you in advance for your help.



UPDATE



Just to make sure my FunctorOps is right, I created a functor instance for Id:



case class Id[A](value: A)
implicit val idF: Functor[Id] = new Functor[Id]
override def fmap[A, B](fa: Id[A])(f: A => B): Id[B] = Id(f(fa.value))


val id = Id(42)
id.fmap(_ + 1) // compiles


So the problem does not come from the FunctorOps implicit class. I suspect Scala to have a real hard time with type lambdas...



UPDATE 2



I tried to simplify the problem but without success:



 trait Functor[F[_]] 
def map[A, B](x: F[A])(f: A => B): F[B]


implicit class Ops[F[_], A](fa: F[A])(implicit F: Functor[F])
def map[B](f: A => B): F[B] = F.map(fa)(f)


type FF[A] = ( type F[B] = A => B )

implicit def ff[E]: Functor[FF[E]#F] = new Functor[FF[E]#F]
override def map[A, B](x: E => A)(f: A => B): E => B = e => f(x(e))


val f: String => Int = _ => 42

val value: Functor[FF[String]#F] = ff[String]
val ops = new Ops[FF[String]#F, Int](f)(value)

// These compile
ops.map(_ + 1)("")
value.map(f)(_ + 1)("")

// This not
f.map(_ + 1)









share|improve this question
















I saw this question several times on SO, but no matter how hard I try, I can't make the following code compile. The goal is to implement an Functor implementation for a simpler Reader (the code is here):



 trait Functor[F[_]] 
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo = Reader[String, Int](_ => 42)

foo.fmap(_ + 1) // does not compile


I tried to bypass the implicit mechanism with the following:



FunctorOps(foo).fmap(_ + 1)


but this outputs the following compilation error:



Error:(82, 23) type mismatch;
found : com.fp.Scratchpad.Reader[String,Int]
required: ?F[?A]
Note that implicit conversions are not applicable because they are ambiguous:
both method ArrowAssoc in object Predef of type [A](self: A)ArrowAssoc[A]
and method Ensuring in object Predef of type [A](self: A)Ensuring[A]
are possible conversion functions from com.fp.Scratchpad.Reader[String,Int] to ?F[?A]
FunctorOps(foo).fmap(_ + 1)


Thank you in advance for your help.



UPDATE



Just to make sure my FunctorOps is right, I created a functor instance for Id:



case class Id[A](value: A)
implicit val idF: Functor[Id] = new Functor[Id]
override def fmap[A, B](fa: Id[A])(f: A => B): Id[B] = Id(f(fa.value))


val id = Id(42)
id.fmap(_ + 1) // compiles


So the problem does not come from the FunctorOps implicit class. I suspect Scala to have a real hard time with type lambdas...



UPDATE 2



I tried to simplify the problem but without success:



 trait Functor[F[_]] 
def map[A, B](x: F[A])(f: A => B): F[B]


implicit class Ops[F[_], A](fa: F[A])(implicit F: Functor[F])
def map[B](f: A => B): F[B] = F.map(fa)(f)


type FF[A] = ( type F[B] = A => B )

implicit def ff[E]: Functor[FF[E]#F] = new Functor[FF[E]#F]
override def map[A, B](x: E => A)(f: A => B): E => B = e => f(x(e))


val f: String => Int = _ => 42

val value: Functor[FF[String]#F] = ff[String]
val ops = new Ops[FF[String]#F, Int](f)(value)

// These compile
ops.map(_ + 1)("")
value.map(f)(_ + 1)("")

// This not
f.map(_ + 1)






scala intellij-idea functional-programming






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 28 at 13:41







Francis Toth

















asked Mar 27 at 14:50









Francis TothFrancis Toth

1,0308 silver badges19 bronze badges




1,0308 silver badges19 bronze badges















  • What is your Scala version? The first one compiles in scastie.scala-lang.org/gpRKdKjNS4KLTAq7Zwptsw. I was actually surprised it seems to work in older versions too, I assumed you need 2.12 or 2.11.11+.

    – Alexey Romanov
    Mar 27 at 16:55











  • I've just checked and Scala version is 2.12.8. Tried to compile with IDEA and sbt :(

    – Francis Toth
    Mar 27 at 23:34











  • Are you running this in a Worksheet?

    – mdm
    Mar 28 at 10:30











  • No, and I also tried to compile this raw sbt

    – Francis Toth
    Mar 28 at 13:17

















  • What is your Scala version? The first one compiles in scastie.scala-lang.org/gpRKdKjNS4KLTAq7Zwptsw. I was actually surprised it seems to work in older versions too, I assumed you need 2.12 or 2.11.11+.

    – Alexey Romanov
    Mar 27 at 16:55











  • I've just checked and Scala version is 2.12.8. Tried to compile with IDEA and sbt :(

    – Francis Toth
    Mar 27 at 23:34











  • Are you running this in a Worksheet?

    – mdm
    Mar 28 at 10:30











  • No, and I also tried to compile this raw sbt

    – Francis Toth
    Mar 28 at 13:17
















What is your Scala version? The first one compiles in scastie.scala-lang.org/gpRKdKjNS4KLTAq7Zwptsw. I was actually surprised it seems to work in older versions too, I assumed you need 2.12 or 2.11.11+.

– Alexey Romanov
Mar 27 at 16:55





What is your Scala version? The first one compiles in scastie.scala-lang.org/gpRKdKjNS4KLTAq7Zwptsw. I was actually surprised it seems to work in older versions too, I assumed you need 2.12 or 2.11.11+.

– Alexey Romanov
Mar 27 at 16:55













I've just checked and Scala version is 2.12.8. Tried to compile with IDEA and sbt :(

– Francis Toth
Mar 27 at 23:34





I've just checked and Scala version is 2.12.8. Tried to compile with IDEA and sbt :(

– Francis Toth
Mar 27 at 23:34













Are you running this in a Worksheet?

– mdm
Mar 28 at 10:30





Are you running this in a Worksheet?

– mdm
Mar 28 at 10:30













No, and I also tried to compile this raw sbt

– Francis Toth
Mar 28 at 13:17





No, and I also tried to compile this raw sbt

– Francis Toth
Mar 28 at 13:17












1 Answer
1






active

oldest

votes


















1














UPDATE:

I think that, to have this working, you need to enable some extra options for the compiler in build.sbt:



scalacOptions ++= Seq(
"-Ypartial-unification",
"-language:postfixOps",
"-language:higherKinds",
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked"
)


More info on the partial unification flag and what it solves can be found here.



ORIGINAL ANSWER:
Are you running your code through Worksheet or a Scratch in IDEA? I have noticed that sometimes, especially in these kind of functional programming tasks where there is type inference, implicit resolution and higher kinded type "magic", IDEA's REPLs are not up to the task (but I am not sure why).



This said, I tried to run the following on IDEA:



object TestApp extends App
trait Functor[F[_]]
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo: Reader[String, Int] = Reader[String, Int](s => s.length)

val i = foo.fmap(_ + 1)

println(i.run("Test"))
println(i.run("Hello World"))



And it works fine, printing 5 and 12. Also, as someone else mentioned, your code works on Scastie, which is another syntom of IDEA's acting up.



One final note: you probably already know this, but you can avoid all that type-lambda ugliness using the kind-projector compiler plugin.



Long story short, drop the ReaderF[X] type alias, and make your functor instance look like this:



implicit def readerFunctors[X]: Functor[Reader[X,?]] =
new Functor[Reader[X,?]]
override def fmap[B, C](fa: Reader[X,B])(f: B => C): Reader[X,C] =
Reader(e => f(fa.run(e)))



Which is more readable IMHO.






share|improve this answer



























  • I copy pasted the code on IDEA, and it does not even compile :( Same on sbt. I am using scala 2.12.8 and sbt 1.2.8. I also tried to run this (without the TestApp) in the standard REPL and still no success. What versions do you use?

    – Francis Toth
    Mar 28 at 13:02











  • scalac also found an error at val i = foo.fmap(_ + 1)

    – Francis Toth
    Mar 28 at 13:18











  • ....this is very strange. I use a normal configuration, Scala 2.12.8, nothing fancy or strange...

    – mdm
    Mar 28 at 13:31











  • But either way, I think it's clear now that, whatever the problem is, it's not in the code but in your environment. Which is kind of good news I guess :)

    – mdm
    Mar 28 at 13:32












  • I am still not sure about this to be honest. Could you please give me the version of sbt/scala you are using?

    – Francis Toth
    Mar 28 at 13:33











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380160%2fproper-implementation-for-a-2-type-parameters-functor-in-scala%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














UPDATE:

I think that, to have this working, you need to enable some extra options for the compiler in build.sbt:



scalacOptions ++= Seq(
"-Ypartial-unification",
"-language:postfixOps",
"-language:higherKinds",
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked"
)


More info on the partial unification flag and what it solves can be found here.



ORIGINAL ANSWER:
Are you running your code through Worksheet or a Scratch in IDEA? I have noticed that sometimes, especially in these kind of functional programming tasks where there is type inference, implicit resolution and higher kinded type "magic", IDEA's REPLs are not up to the task (but I am not sure why).



This said, I tried to run the following on IDEA:



object TestApp extends App
trait Functor[F[_]]
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo: Reader[String, Int] = Reader[String, Int](s => s.length)

val i = foo.fmap(_ + 1)

println(i.run("Test"))
println(i.run("Hello World"))



And it works fine, printing 5 and 12. Also, as someone else mentioned, your code works on Scastie, which is another syntom of IDEA's acting up.



One final note: you probably already know this, but you can avoid all that type-lambda ugliness using the kind-projector compiler plugin.



Long story short, drop the ReaderF[X] type alias, and make your functor instance look like this:



implicit def readerFunctors[X]: Functor[Reader[X,?]] =
new Functor[Reader[X,?]]
override def fmap[B, C](fa: Reader[X,B])(f: B => C): Reader[X,C] =
Reader(e => f(fa.run(e)))



Which is more readable IMHO.






share|improve this answer



























  • I copy pasted the code on IDEA, and it does not even compile :( Same on sbt. I am using scala 2.12.8 and sbt 1.2.8. I also tried to run this (without the TestApp) in the standard REPL and still no success. What versions do you use?

    – Francis Toth
    Mar 28 at 13:02











  • scalac also found an error at val i = foo.fmap(_ + 1)

    – Francis Toth
    Mar 28 at 13:18











  • ....this is very strange. I use a normal configuration, Scala 2.12.8, nothing fancy or strange...

    – mdm
    Mar 28 at 13:31











  • But either way, I think it's clear now that, whatever the problem is, it's not in the code but in your environment. Which is kind of good news I guess :)

    – mdm
    Mar 28 at 13:32












  • I am still not sure about this to be honest. Could you please give me the version of sbt/scala you are using?

    – Francis Toth
    Mar 28 at 13:33
















1














UPDATE:

I think that, to have this working, you need to enable some extra options for the compiler in build.sbt:



scalacOptions ++= Seq(
"-Ypartial-unification",
"-language:postfixOps",
"-language:higherKinds",
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked"
)


More info on the partial unification flag and what it solves can be found here.



ORIGINAL ANSWER:
Are you running your code through Worksheet or a Scratch in IDEA? I have noticed that sometimes, especially in these kind of functional programming tasks where there is type inference, implicit resolution and higher kinded type "magic", IDEA's REPLs are not up to the task (but I am not sure why).



This said, I tried to run the following on IDEA:



object TestApp extends App
trait Functor[F[_]]
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo: Reader[String, Int] = Reader[String, Int](s => s.length)

val i = foo.fmap(_ + 1)

println(i.run("Test"))
println(i.run("Hello World"))



And it works fine, printing 5 and 12. Also, as someone else mentioned, your code works on Scastie, which is another syntom of IDEA's acting up.



One final note: you probably already know this, but you can avoid all that type-lambda ugliness using the kind-projector compiler plugin.



Long story short, drop the ReaderF[X] type alias, and make your functor instance look like this:



implicit def readerFunctors[X]: Functor[Reader[X,?]] =
new Functor[Reader[X,?]]
override def fmap[B, C](fa: Reader[X,B])(f: B => C): Reader[X,C] =
Reader(e => f(fa.run(e)))



Which is more readable IMHO.






share|improve this answer



























  • I copy pasted the code on IDEA, and it does not even compile :( Same on sbt. I am using scala 2.12.8 and sbt 1.2.8. I also tried to run this (without the TestApp) in the standard REPL and still no success. What versions do you use?

    – Francis Toth
    Mar 28 at 13:02











  • scalac also found an error at val i = foo.fmap(_ + 1)

    – Francis Toth
    Mar 28 at 13:18











  • ....this is very strange. I use a normal configuration, Scala 2.12.8, nothing fancy or strange...

    – mdm
    Mar 28 at 13:31











  • But either way, I think it's clear now that, whatever the problem is, it's not in the code but in your environment. Which is kind of good news I guess :)

    – mdm
    Mar 28 at 13:32












  • I am still not sure about this to be honest. Could you please give me the version of sbt/scala you are using?

    – Francis Toth
    Mar 28 at 13:33














1












1








1







UPDATE:

I think that, to have this working, you need to enable some extra options for the compiler in build.sbt:



scalacOptions ++= Seq(
"-Ypartial-unification",
"-language:postfixOps",
"-language:higherKinds",
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked"
)


More info on the partial unification flag and what it solves can be found here.



ORIGINAL ANSWER:
Are you running your code through Worksheet or a Scratch in IDEA? I have noticed that sometimes, especially in these kind of functional programming tasks where there is type inference, implicit resolution and higher kinded type "magic", IDEA's REPLs are not up to the task (but I am not sure why).



This said, I tried to run the following on IDEA:



object TestApp extends App
trait Functor[F[_]]
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo: Reader[String, Int] = Reader[String, Int](s => s.length)

val i = foo.fmap(_ + 1)

println(i.run("Test"))
println(i.run("Hello World"))



And it works fine, printing 5 and 12. Also, as someone else mentioned, your code works on Scastie, which is another syntom of IDEA's acting up.



One final note: you probably already know this, but you can avoid all that type-lambda ugliness using the kind-projector compiler plugin.



Long story short, drop the ReaderF[X] type alias, and make your functor instance look like this:



implicit def readerFunctors[X]: Functor[Reader[X,?]] =
new Functor[Reader[X,?]]
override def fmap[B, C](fa: Reader[X,B])(f: B => C): Reader[X,C] =
Reader(e => f(fa.run(e)))



Which is more readable IMHO.






share|improve this answer















UPDATE:

I think that, to have this working, you need to enable some extra options for the compiler in build.sbt:



scalacOptions ++= Seq(
"-Ypartial-unification",
"-language:postfixOps",
"-language:higherKinds",
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked"
)


More info on the partial unification flag and what it solves can be found here.



ORIGINAL ANSWER:
Are you running your code through Worksheet or a Scratch in IDEA? I have noticed that sometimes, especially in these kind of functional programming tasks where there is type inference, implicit resolution and higher kinded type "magic", IDEA's REPLs are not up to the task (but I am not sure why).



This said, I tried to run the following on IDEA:



object TestApp extends App
trait Functor[F[_]]
def fmap[A, B](fa: F[A])(f: A => B): F[B]


implicit class FunctorOps[F[_]: Functor, A](self: F[A])
def fmap[B](f: A => B): F[B] = implicitly[Functor[F]].fmap(self)(f)


case class Reader[A, B](run: A => B)
type ReaderF[X] = ( type L[A] = Reader[X, A] )

implicit def readerFunctors[E]: Functor[ReaderF[E]#L] =
new Functor[ReaderF[E]#L]
override def fmap[A, B](fa: Reader[E, A])(f: A => B): Reader[E, B] =
Reader(e => f(fa.run(e)))


val foo: Reader[String, Int] = Reader[String, Int](s => s.length)

val i = foo.fmap(_ + 1)

println(i.run("Test"))
println(i.run("Hello World"))



And it works fine, printing 5 and 12. Also, as someone else mentioned, your code works on Scastie, which is another syntom of IDEA's acting up.



One final note: you probably already know this, but you can avoid all that type-lambda ugliness using the kind-projector compiler plugin.



Long story short, drop the ReaderF[X] type alias, and make your functor instance look like this:



implicit def readerFunctors[X]: Functor[Reader[X,?]] =
new Functor[Reader[X,?]]
override def fmap[B, C](fa: Reader[X,B])(f: B => C): Reader[X,C] =
Reader(e => f(fa.run(e)))



Which is more readable IMHO.







share|improve this answer














share|improve this answer



share|improve this answer








edited Mar 28 at 13:48

























answered Mar 28 at 10:40









mdmmdm

2,7472 gold badges18 silver badges35 bronze badges




2,7472 gold badges18 silver badges35 bronze badges















  • I copy pasted the code on IDEA, and it does not even compile :( Same on sbt. I am using scala 2.12.8 and sbt 1.2.8. I also tried to run this (without the TestApp) in the standard REPL and still no success. What versions do you use?

    – Francis Toth
    Mar 28 at 13:02











  • scalac also found an error at val i = foo.fmap(_ + 1)

    – Francis Toth
    Mar 28 at 13:18











  • ....this is very strange. I use a normal configuration, Scala 2.12.8, nothing fancy or strange...

    – mdm
    Mar 28 at 13:31











  • But either way, I think it's clear now that, whatever the problem is, it's not in the code but in your environment. Which is kind of good news I guess :)

    – mdm
    Mar 28 at 13:32












  • I am still not sure about this to be honest. Could you please give me the version of sbt/scala you are using?

    – Francis Toth
    Mar 28 at 13:33


















  • I copy pasted the code on IDEA, and it does not even compile :( Same on sbt. I am using scala 2.12.8 and sbt 1.2.8. I also tried to run this (without the TestApp) in the standard REPL and still no success. What versions do you use?

    – Francis Toth
    Mar 28 at 13:02











  • scalac also found an error at val i = foo.fmap(_ + 1)

    – Francis Toth
    Mar 28 at 13:18











  • ....this is very strange. I use a normal configuration, Scala 2.12.8, nothing fancy or strange...

    – mdm
    Mar 28 at 13:31











  • But either way, I think it's clear now that, whatever the problem is, it's not in the code but in your environment. Which is kind of good news I guess :)

    – mdm
    Mar 28 at 13:32












  • I am still not sure about this to be honest. Could you please give me the version of sbt/scala you are using?

    – Francis Toth
    Mar 28 at 13:33

















I copy pasted the code on IDEA, and it does not even compile :( Same on sbt. I am using scala 2.12.8 and sbt 1.2.8. I also tried to run this (without the TestApp) in the standard REPL and still no success. What versions do you use?

– Francis Toth
Mar 28 at 13:02





I copy pasted the code on IDEA, and it does not even compile :( Same on sbt. I am using scala 2.12.8 and sbt 1.2.8. I also tried to run this (without the TestApp) in the standard REPL and still no success. What versions do you use?

– Francis Toth
Mar 28 at 13:02













scalac also found an error at val i = foo.fmap(_ + 1)

– Francis Toth
Mar 28 at 13:18





scalac also found an error at val i = foo.fmap(_ + 1)

– Francis Toth
Mar 28 at 13:18













....this is very strange. I use a normal configuration, Scala 2.12.8, nothing fancy or strange...

– mdm
Mar 28 at 13:31





....this is very strange. I use a normal configuration, Scala 2.12.8, nothing fancy or strange...

– mdm
Mar 28 at 13:31













But either way, I think it's clear now that, whatever the problem is, it's not in the code but in your environment. Which is kind of good news I guess :)

– mdm
Mar 28 at 13:32






But either way, I think it's clear now that, whatever the problem is, it's not in the code but in your environment. Which is kind of good news I guess :)

– mdm
Mar 28 at 13:32














I am still not sure about this to be honest. Could you please give me the version of sbt/scala you are using?

– Francis Toth
Mar 28 at 13:33






I am still not sure about this to be honest. Could you please give me the version of sbt/scala you are using?

– Francis Toth
Mar 28 at 13:33









Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.







Got a question that you can’t ask on public Stack Overflow? Learn more about sharing private information with Stack Overflow for Teams.



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55380160%2fproper-implementation-for-a-2-type-parameters-functor-in-scala%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript